队列的入队、出队操作

队列的常用操作就是入队和出队,这里入队用函数insert实现,出队用函数del实现,print函数实现队列的遍历做操:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>

using namespace std;
typedef struct node {
	int data;
	struct node *next;
}Node, *PNode;

typedef struct linkqueue {
	PNode first;
	PNode rear;
}Queue, *PQueue;

PQueue insert(PQueue link, int x)
{
	PNode s;
	s = (PNode)malloc(sizeof(node));
	s->data = x;
	s->next = NULL;

	if (link == NULL) {
		link = (PQueue)malloc(sizeof(Queue));
		link->first = s;
		link->rear = s;
		link->rear->next = NULL;
	}
	else {
		link->rear->next = s;
		link->rear = s;
		link->rear->next = NULL;
	}

	return link;
}

PQueue del(PQueue link)
{
	PNode p;
	if (link == NULL) {
		printf("队列为空\n");
		return NULL;
	}

	p = link->first;
	if (link->first != link->rear) {
		link->first = link->first->next;
	}
	else {
		printf("删除完毕\n");
	}
	free(p);
	return link;
}

void print(PQueue link)
{
	PNode p = link->first;
	while (p != NULL) {
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

int main(void)
{
	PQueue linkqueue = NULL;

	linkqueue = insert(linkqueue, 5);
	print(linkqueue);
	linkqueue = insert(linkqueue, 4);
	print(linkqueue);
	linkqueue = insert(linkqueue, 3);
	print(linkqueue);
	linkqueue = insert(linkqueue, 2);
	print(linkqueue);
	linkqueue = insert(linkqueue, 1);
	print(linkqueue);
	linkqueue = del(linkqueue);
	print(linkqueue);
	linkqueue = del(linkqueue);
	print(linkqueue);
	linkqueue = del(linkqueue);
	print(linkqueue);
	linkqueue = del(linkqueue);
	print(linkqueue);
	linkqueue = del(linkqueue);
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值