隊列輸入輸出

#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章