#include "stdafx.h"
#include <windows.h>
#include <ctype.h>
typedef char ElemType;
typedef struct _QNode
{
ElemType data;
struct _QNode *next;
}QNode, *QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue( LinkQueue *q )
{
q->front = q->rear = (QueuePtr)malloc(sizeof(QNode));
if ( !q->front )
{
exit(0);
}
q->front->next = NULL;
}
int EnQueue( LinkQueue *q, ElemType e )
{
QueuePtr p;
p = ( QueuePtr )malloc( sizeof(QNode) );
if ( !q->front )
{
exit( 0 );
}
p->data = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
int OutQueue( LinkQueue *q, ElemType* e )
{
QueuePtr p;
if ( q->front == q->rear ) /*队列为空,返回*/
{
printf("the queue is null \n ");
return 0;
}
p = q->front->next;
*e = p->data;
q->front->next = p->next;
if (q->rear == p )
{
q->rear = q->front; /*如果队头就是队尾,则修改队尾指针*/
}
free( p );
}
int main()
{
ElemType e;
LinkQueue q;
InitQueue(&q); /*初始化一个队列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){ /*将队列中的元素出队列,并打印在屏幕上*/
OutQueue(&q,&e);
printf("%c",e);
}
printf("\n");
}
Queue 链表
最新推荐文章于 2022-11-23 14:25:04 发布