https://blog.csdn.net/bLank_Lzy/article/details/111056940?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163921171216780366551906%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163921171216780366551906&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-111056940.pc_search_insert_es_download&utm_term=%E5%BE%AA%E7%8E%AF%E9%98%9F%E5%88%97%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4%E7%9A%84%E5%8A%A8%E6%80%81%E5%9B%9E%E6%94%B6%E6%96%B9%E6%B3%95+&spm=1018.2226.3001.4187
本文章在上述文章的代码基础上进行修改
1.循环队列的空间增长:空间增长的含义就是重新定义一个结构体指针,结构体指针开辟的空间更大,然后将原值赋入新的指针中
前提:队列的空间已满 1.front这种情况下,rear的下一位空间是front,这个时候就要将front右边的值全部移动至空间的最左边,将中间的空间空出来。
2.循环队列的空间回收:空间回收的道理与空间增长同理
前提:当剩余空间的的值达到回收值 1.front当这种情况时闲余的空间会在队列的中部,这个时候就要将front右边的部分移动至左边,将尾部的空间空出,然后回收。
源代码:
#include#include # define QUEUE_INIT_SIZE 5 //队列存储空间的初始分配量 # define QUEUE_INCREMENT 5 //队列存储空间的分配增量 # define QUEUE_FREESIZE 10 //队列存储空间的回收预定值 # define PERCENT 0.5 //队列闲置空间的回收比例 typedef struct //循环队列类型 { int* element = NULL; //一个的数组头指针 int front; //队头指针,若队列非空,指向队列头元素 int rear; //队尾指针,若队列非空,指向队尾元素的下一个位置 int QueueSize; //当前分配的存储空间(以sizeof(ElementType)为单位) ,队列中多余空出来的空间? int QueueLength; //队列长度(有数据的) }SeqQueue; bool InitQueue(SeqQueue* Q) { free(Q->element); Q->element = (int*)malloc(QUEUE_INIT_SIZE * sizeof(int)); Q->front = Q->rear =-1; Q->QueueSize = QUEUE_INIT_SIZE; Q->QueueLength = 0; return true; } int ENQueue(SeqQueue* Q,int e)//入队 { int* New_base; if(Q->QueueSize <= Q->QueueLength )//判断是否队满 { New_base = (int*)malloc( (Q->QueueSize + QUEUE_INCREMENT) * sizeof(int)); for(int i=0;i QueueSize;i++){ //把值复制过去 New_base[i] = Q->element[i]; } Q->element = New_base; Q->QueueSize += QUEUE_INCREMENT; if (Q->rear <= Q->front) { int x=0; int i = Q->QueueSize -QUEUE_INCREMENT - 1; int j = Q->QueueSize - 1; for (; i >= Q->front; i--, j--){ Q->element[j] = Q->element[i]; x++; } Q->front=Q->QueueSize-x; } } Q->rear = (Q->rear+1) % Q->QueueSize; Q->element[Q->rear] = e; //真正的入队 Q->QueueLength++; return 0; } void DeleteQueue(SeqQueue* Q)//出队 { int i, j, cnt; int hsgs = int(QUEUE_FREESIZE * PERCENT); //回收 int* newbase; Q->front = (Q->front+1) % Q->QueueSize; printf("出队%d后,front=%d",Q->element[Q->front],Q->front); printf("======================n"); Q->QueueLength--; if ( Q->QueueSize - Q->QueueLength >= QUEUE_FREESIZE ) { if (Q->front > Q->rear) { for (i = Q->QueueSize - hsgs - 1, j = Q->QueueSize - 1; j >= Q->front; i--, j--) Q->element[i] = Q->element[j]; cnt = Q->QueueSize - Q->front; Q->front = Q->QueueSize - hsgs - cnt; } else{ if (Q->QueueSize -(Q->rear )< hsgs) { for (i = 0, j = Q->front+1; j <=Q->rear; i++, j++) Q->element[i] = Q->element[j]; Q->front = -1; Q->rear = Q->front + Q->QueueLength; } } newbase = (int*)realloc(Q->element, (Q->QueueSize - hsgs) * sizeof(int)); Q->element = newbase; Q->QueueSize = Q->QueueSize - hsgs; } } void meun(){ printf("===========================n"); printf("| 1. 入队 |n"); printf("| 2. 出队 |n"); printf("| 3. 查看队列 |n"); printf("| 0. 退出 |n"); printf("===========================n"); } int main() { int n ; int x; SeqQueue Q; InitQueue(&Q); while(true){ int select; meun(); printf("请选择: n"); scanf("%d",&select); switch(select){ case 1: printf("你要入队几个数据: n"); scanf("%d",&n); printf("请输入这些数据: n"); for (int i = 0; i < n; i++)// 入队 { scanf("%d",&x); ENQueue(&Q, x); } printf("* 初始:n") ; printf("队列长度:%dn", Q.QueueLength); printf("空闲空间长度:%dn",Q.QueueSize - Q.QueueLength); break; case 2: while(1){ printf("有%d个数据n",Q.QueueLength); printf("你要出队几个数据:n"); scanf("%d",&n); if(n<=Q.QueueLength) break; else printf("errorn"); } for(int i=0;i Q.front){ for(int x=Q.front;x 该代码在一些特定的条件下会有些问题!!!



