妙趣横生的算法之队列的操作

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef char QelemType;

typedef struct QNode
{
	QelemType data;
	struct QNode *next;
}QNode,*QueuePtr;

typedef struct
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

//初始化空队列
void initQueue(LinkQueue *q)
{
	q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
	if (!q->front)
	{
		exit(0);
	}
	
	q->rear->next = NULL;
}



//入队操作
void EnQueue(LinkQueue *q,QelemType e)
{
	QueuePtr p;
	p  = (QueuePtr)malloc(sizeof(QNode));
	
	p->next = NULL;
	if (!p)
	{
		exit(0);
	}
	p->data = e;
	
	
	q->rear->next = p;
	q->rear = p;
		
}
void DeQueue(LinkQueue *q,QelemType *e)
{
	QueuePtr p;
	if (q->front==q->rear)
	{
		return;
	}
	p = q->front->next;
	*e = p->data;
	q->front->next = p->next;
	if (q->rear==p)
	{
		q->rear = q->front;
	}
	free(p);
}
void main()
{
	QelemType e;
	LinkQueue q;
	initQueue(&q);
	printf("Please input a string into a queue\n");
	scanf("%c",&e);
	while (e!='#')
	{
		EnQueue(&q,e);
		scanf("%c",&e);
	}
	printf("The string into the queue is\n");
	while(q.front!= q.rear)
	{
		DeQueue(&q,&e);
		printf("%c",e);
	}
	printf("\n");
	getchar();
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值