队列的链式存储相关操作

#include <stdio.h>
#include <stdlib.h>

enum s
{
	QUEUE_EMPTY,
	QUEUE_NOEMPTY,
	IN_OK,
	OUT_OK,
	OUT_NO
};
struct node
{
	int data;
	struct node *next;
};
struct Queue
{
	struct node *front;
	struct node *rear;
};
void init_queue(struct Queue *s)
{
	s->front = NULL;
	s->rear = NULL;
}

int empty_queue(struct Queue *s)
{
	if(s->rear == NULL)
	{
		printf("queue is empty!\n");
		return QUEUE_EMPTY;
	}
	return QUEUE_NOEMPTY;
}

int in_queue(struct Queue *s,int num)
{
	struct node *p = (struct node*)malloc(sizeof(struct node));
	if(p == NULL)
	{
		printf("malloc fail!\n");
		return -1;
	}
	p->data = num;
	p->next = NULL;
	if(s->rear == NULL)
	{
		s->front = p;
		s->rear = p;
	}
	else
	{
		s->rear->next = p;
		s->rear = p;
		return IN_OK;
	}
}

int out_queue(struct Queue *s,int *num)
{
	if(empty_queue(s) == QUEUE_EMPTY)
	{
		return OUT_NO;
	}
	struct node *p = s->front;
	*num = p->data;
//	printf("%d",*num);
	s->front = p->next;
	if(s->front == NULL)
	{
		s->rear = NULL;
	}
	free(p);
	p = NULL;
	return OUT_OK;
}
int main()
{
	struct Queue *s = (struct Queue*)malloc(sizeof(struct Queue));
	int i = 0;
	int num = 0;
	init_queue(s);

	for(i = 0;i < 9;i++)
	{
		in_queue(s,i+1);
		printf("第[%d]: %din queue\n",i+1,s->rear->data);
	}
	for(i = 0;i < 9;i++)
	{
		if(empty_queue(s) != QUEUE_EMPTY)
		{
			out_queue(s,&num);
			printf("%d\n",num);
		}
		else
		{
			printf("out error!\n");
		}
	}
	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值