队列的基本操作

xiaoxiao2021-03-01  12

*队列是只允许在一端进行插入操作,而在另一端进行删除操作的线性表,简称FIFO(First In First Out).允许插入的一端是队尾,允许增添的一端是对头。 **   队列在现实生活中就有很多实际的例子,比如说排队吧,就是一端“插入”,一端“删除”;再比如用键盘进行各种字母或数字的输入,到显示器上如笔记本软件上的输出,其实就是队列的应用。(写这篇博客的时候顺便百度了写博客如何首行缩进,需要shift+space开启全角模式,然后输入空格,然后再转化回来)

对于队列,你需要记住的有

①:队列因为有插入端和删除段,所以需要两个变量front和rear; ②:队列的长度(rear-front+size)%size; ③:队列满:(rear+1)%size=front; ④:队列空:front=rear; ⑤:队列指针的移动:front=(front+1)%size;

顺序存储:

#include<stdio.h> #include<stdlib.h> #include<time.h> typedef int QelemType; typedef struct { QelemType data[10]; int front; int rear; }sqQuene; int creatquene(sqQuene *Q) //创建 { int num; if(Q->rear!=0) Q->rear=0; srand((unsigned)time(NULL)); while((Q->rear+1)!=Q->front) { num=rand()0+1; Q->data[Q->rear]=num; Q->rear=(Q->rear+1); //指针向后移 } } int initquene(sqQuene *Q) //初始化 { Q->front=0; Q->rear=0; return 1; } int quenelength(sqQuene Q) //求队列长度 { return (Q.rear-Q.front+10); } int insertquene(sqQuene *Q,int x) //插入元素 { if(Q->rear+1==Q->front) { return 0; } Q->data[Q->rear]=x; Q->rear=(Q->rear+1); } int deletequene(sqQuene *Q,int *x) //删除元素 { if(Q->front==Q->rear) return 0; else *x=Q->data[Q->front]; Q->front=(Q->front+1); } int creatquene(sqQuene *Q); //创建 int initquene(sqQuene *Q); //初始化 int quenelength(sqQuene Q); //求队列长度 int insertquene(sqQuene *Q,int x); //插入元素 int deletequene(sqQuene *Q,int *x); //删除元素 int main() { int i; int num; int x,y; sqQuene Q; initquene(&Q); creatquene(&Q); printf("1:打印队列\t\t2:增添元素\t\t3:删除元素"); while(scanf("%d",&num)==1) { switch(num) { case 1: for(i=Q.front;i<Q.rear;i++) { printf("%d\t\t",Q.data[i]); } break; case 2: printf("输入增添元素:"); scanf("%d",&x); insertquene(&Q,x); break; case 3: deletequene(&Q,&y); printf("删除元素是%d",y); break; } } }

链式存储

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("-"); }
转载请注明原文地址: https://www.6miu.com/read-3649950.html

最新回复(0)