//链队列的链式存储使用
#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode,*QueuePtr;//构造链结构
typedef struct QueueList {
QueuePtr front, rear;
}QueueList;//;构造链队列的结构
//创建队列
InitQueue(QueueList* q) {
q->front = q->rear = (QueuePtr)malloc(sizeof(LNode));
if (!q->front)
exit(0);
q->front->next = NULL;
}
//入链队列操作
InsertQueue(QueueList* s, ElemType e) {
QueuePtr p;
p = (QueuePtr)malloc(sizeof(LNode));
p->data = e;
p->next = NULL;
s->rear->next = p->next;
s->rear = p;
}
//出链队列操作
OutQueue(QueueList* s, ElemType* e) {
QueuePtr p;
if (s->front == s->rear)
return;
p = s->front->next;
*e = p->data;
s->front = p->next;
if (s->rear == p)
{
s->rear = s->front;
}
free(p);
}
int main(void) {
ElemType e;
QueueList s;
InitQueue(&s);
printf("请输入一串数字来让队列进行储存和打印(输入#结束):\n");
scanf("%c", &e);
if (e != '#') {
printf("链队列入列的数据为:");
}
else {
printf("你输入的数据有误!\n");
return -1;
}
while (e != '#') {
InsertQueue(&s,e);
printf("%c ", e);
scanf("%c", &e);
}
printf("\n链队列出列的数据为:");
while (s.front != s.rear) {
OutQueue(&s, &e);
printf("%c ", e);
}
return 0;
}
数据结构之链队列的链式存储使用
最新推荐文章于 2021-10-24 23:13:23 发布