非伪代码 队列的定义及基本操作

队列的定义及基本操作

记住先进先出就OK

#include <stdio.h>
#include <malloc.h>
//节点   
struct node {
	int data;
	node *next;
};
//链队列 
struct queue{
	node *front;
	node *rear;
};
//初始化
queue* initQueue(){
	queue * q=(queue *)malloc(sizeof(queue));
	q->front=q->rear=NULL;
	return q;
} 

//判断队列是否为空
int isEmpty(queue *q)
{
	return q->front ==NULL;
}

//入队
void insertQueue(queue *q, int a)
{
	node *n=(node* )malloc(sizeof(node ));
	n->data=a;
	n->next=NULL;
	if(q->front ==NULL)
	{
		q->front=n;
		q->rear=n;
	}
	else
	{
		q->rear->next=n;
		q->rear=n;
	}
}
//出队
int deleteQueue(queue *q)
{
	int d;
	node *p;
	if(q->front==NULL)
	{
		return NULL;
	}
	else
	{
		d=q->front->data;
		p=q->front;
		q->front=p->next;
		free(p);
	}
	return 	d;
} 
//队列的大小
int  queuesize(queue *q)
{
	int num=0;
	node *p;
	p=q->front;
	while (p!=NULL)
	{
		num++;
		p=p->next; 
	}
	return num;
 } 
//展示 队列中数据 
void Display(queue *q)
{
	int i=0;
	node *p;
	p=q->front;
	while(p!=NULL)
	{
		printf("第%d个数据是%d\n",i,p->data);
		p=p->next;
		i++;
	}
	
}

int main()
{
	queue *q=initQueue();
	if(isEmpty(q));
	printf("q是个空队列\n");
	insertQueue(q,1);
	insertQueue(q,2);
	insertQueue(q,3);
	insertQueue(q,4);
	insertQueue(q,5);
	
	int num=queuesize(q);
	printf("队列中有%d个元素\n",num);
	Display(q);
	deleteQueue(q);
	Display(q);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值