*队列是只允许在一端进行插入操作,而在另一端进行删除操作的线性表,简称FIFO(First In First Out).允许插入的一端是队尾,允许增添的一端是对头。 ** 队列在现实生活中就有很多实际的例子,比如说排队吧,就是一端“插入”,一端“删除”;再比如用键盘进行各种字母或数字的输入,到显示器上如笔记本软件上的输出,其实就是队列的应用。(写这篇博客的时候顺便百度了写博客如何首行缩进,需要shift+space开启全角模式,然后输入空格,然后再转化回来)
①:队列因为有插入端和删除段,所以需要两个变量front和rear; ②:队列的长度(rear-front+size)%size; ③:队列满:(rear+1)%size=front; ④:队列空:front=rear; ⑤:队列指针的移动:front=(front+1)%size;
https://blog.csdn.net/y_universe/article/details/78702504
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; }LinkQNode; typedef struct { LinkQNode *front; LinkQNode *rear; }LinkQueue; void InitLinkQueue(LinkQueue *Q); int IsLQEmpty(LinkQueue *Q); int EnLinkQueue(LinkQueue *Q, int x); int DeLinkQueue(LinkQueue *Q, int *x); int GetLQHead(LinkQueue *Q, int *x); void print_hyphen(int n); int main(void) { LinkQueue LQ; InitLinkQueue(&LQ); if(LQ.front) printf("队列初始化成功!\n"); else{ printf("队列初始化失败!\n"); exit(1); } int end = 0; int ope; int n; while(!end){ print_hyphen(15); printf("\n"); printf("请输入指令来执行操作\n"); print_hyphen(15); printf("\n"); printf("1、入队\n2、出队\n3、取队头元素\n4、判断队列是否为空\n5、退出\n"); print_hyphen(15); printf("\n"); printf("输入要使用的功能的序号: "); scanf("%d", &ope); getchar(); switch(ope){ case 1: printf("请输入要入队的数据: "); scanf("%d", &n); EnLinkQueue(&LQ, n); break; case 2: if(!DeLinkQueue(&LQ, &n)) printf("队列为空!\n"); else printf("出队的元素为: %d\n", n); break; case 3: if(!GetLQHead(&LQ, &n)) printf("队列为空!\n"); else printf("出队的元素为: %d\n", n); break; case 4: if(IsLQEmpty(&LQ)) printf("队列为空!\n"); else printf("队列不为空! \n"); break; case 5: printf("再见!\n"); end = 1; break; default: printf("无此序号,请重新输入!\n"); } } return 0; } void InitLinkQueue(LinkQueue *Q) { Q->front = (LinkQNode*)malloc(sizeof(LinkQNode)); Q->rear = Q->front; Q->front->next = NULL; } int IsLQEmpty(LinkQueue *Q) { if(Q->front == Q->rear) return 1; else return 0; } int EnLinkQueue(LinkQueue *Q, int x) { LinkQNode *NewNode; NewNode = (LinkQNode*)malloc(sizeof(LinkQNode)); if(NewNode != NULL){ NewNode->data = x; NewNode->next = NULL; Q->rear->next = NewNode; Q->rear = NewNode; return 1; } else return 0; } int DeLinkQueue(LinkQueue *Q, int *x) { LinkQNode *p; if(Q->front == Q->rear) return 0; p = Q->front->next; Q->front->next = p->next; if(Q->rear == p) Q->rear = Q->front; *x = p->data; free(p); return 1; } int GetLQHead(LinkQueue *Q, int *x) { LinkQNode *p; if(Q->front == Q->rear) return 0; *x = Q->front->next->data; return 1; } void print_hyphen(int n) { while(n--) printf("-"); }