队列的线性存储结构

这个程序花了我非常大的时间,结果证明书本上的例子没有我这个好,嘿嘿。。。屌炸天
#include <STDIO.H>
#include <STDLIB.H>
typedef int ElemType;
typedef struct LQNode
{
	ElemType data;
	struct LQNode *next;
}LqueueNode;
typedef struct
{
	LqueueNode *rear;
	LqueueNode *front;
}LinkQueue;
//初始化
void InitLQueue(LinkQueue *Q)
{
	Q->rear=NULL;
	Q->front=NULL;
}
//入队
int EnLQueue(LinkQueue *Q,ElemType x)
{
	//把数据原色x值插入不带头节点的链队列Q的队尾,入队成功则返回1,否则返回0
	LqueueNode *p;
	p=(LqueueNode *)malloc (sizeof(LqueueNode));
	p->data=x;
	p->next=NULL;
	if(Q->rear==NULL)
		Q->rear=Q->front=p;
	else
	{
		Q->rear->next=p;
		Q->rear=p;
	}
	return 1;
}
//出队
int DelQueue(LinkQueue *Q,ElemType *x)
{
	//删除队列的队头元素,用x返回其值
	LqueueNode *q;
	if(Q->front==NULL){
		printf("Queue is empty!");
		return 0;
	}	
	*x=Q->front->data;
	q=Q->front;
	Q->front=Q->front->next;
	if(Q->front==NULL)
		Q->rear=NULL;
	free(q);
	return 1;	
}
void print(LinkQueue Q)
{
	//显示队列中的所有元素
	printf("The queue elements are:\n");
	while(Q.front!=Q.rear)
	{
		printf("%d",Q.front->data);
		Q.front=Q.front->next;
	} 
	printf("%d",Q.rear->data);
	printf("\n");
	/*	if(Q->front==Q->rear)
	printf("空队列,没有元素");
	else{
	printf("该循环队列各元素依次为:");
	for(int k=0;k<QueueLength(Q);k++)
	{
	printf("%d",Q->front->data);
	Q->front=Q->front->next;
	}
}*/
}

void main()
{
	LinkQueue LQ;
	int j,x;
	InitLQueue(&LQ);
	printf("Input a integer\n");
	scanf("%d",&j);
	while(j!=0)
	{
		if(j%2==1)
			EnLQueue(&LQ,j);
		else
			DelQueue(&LQ,&x);
		print(LQ);
		printf("Input a integer\n");
		scanf("%d",&j);		
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值