#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct QNode{
ElemType data;
struct QNode *next;
}QNode, *QueuePtr;
//typedef *QNode *QueuePtr;
typedef struct{
QueuePtr front,rear;
}LinkQueue;
void InitQueue(LinkQueue *q)
{
q->front = q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!q->front)
{
exit(0);
}
q->front->next = NULL;
}
void InsertQueue(LinkQueue *q,ElemType e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if(p == NULL)
{
exit(0);
}
p->data = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
}
void DeleteQueue(LinkQueue *q,ElemType *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 DestroyQueue(LinkQueue *q)
{
while(q->front)
{
q->rear = q->front->next;
free(q->front);
q->front = q->rear;
}
}
int main()
{
LinkQueue q;//先要有内存存里面的东西,不要一上了就来指针-_-又傻叉了
InitQueue(&q);
char c;
printf("Please input endup with #");
scanf("%c",&c);
while(c != '#')
{
InsertQueue(&q,c);
scanf("%c",&c);
}
while(q.front != q.rear)
{
DeleteQueue(&q,&c);
printf("%c",c);
}
DestroyQueue(&q);
return 0;
}
队列输入输出
最新推荐文章于 2022-12-09 21:32:37 发布