队列

来源:互联网 发布:婴儿床什么牌子好 知乎 编辑:程序博客网 时间:2024/05/22 12:17
//链队列#include <stdio.h>#include <stdlib.h>#define MAXSIZE 30typedef struct node{char data;struct node *next;}QNode;typedef struct{QNode *front, *rear;  //头尾指针}LQueue;void Init_LQueue(LQueue **q){QNode *p;p = (QNode *)malloc(sizeof(QNode));      //申请队列的头节点(*q) = (LQueue *)malloc(sizeof(LQueue)); //申请带头尾指针的节点p->next = NULL;(*q)->front = p;(*q)->rear = p;}int Empty_LQueue(LQueue *q)                  //队列空{if (q->front == q->rear){return 1;}else{return 0;}}void In_LQueue(LQueue *q, char x){QNode *p;p = (QNode *)malloc(sizeof(QNode));p->data = x;p->next = NULL;q->rear->next = p;q->rear = p;                              //使队尾指针指向新的队尾节点*p}void Out_LQueue(LQueue *q, char *x)           //出队{QNode *p;if (Empty_LQueue(q)){printf("Empty!\n");return ;}else{p = q->front->next;                    //p 指向第一个节点*x = p->data;q->front->next = p->next;              //把第二个节点作为第一个节点free(p);if (q->front->next == NULL)         //出队后队列为空{q->rear = q->front;}}}void Print(LQueue *q){QNode *p;p = q->front->next;while (p != NULL){printf("%4c", p->data);p = p->next;}printf("\n");}int main(){LQueue *q;char x, *y = &x;Init_LQueue(&q);if (Empty_LQueue(q)){printf("Empty!\n");}printf("Input any string:\n");scanf("%c", &x);while (x != '\n'){In_LQueue(q, x);scanf("%c", &x);}printf("Output element of Queue:\n");Print(q);printf("Output Queue:\n");Out_LQueue(q, y);printf("Element of Output Queue is %c\n", *y);Print(q);return 0;}

原创粉丝点击