当前位置: 首页> 健康> 美食 > 廊坊建站平台_网站推广经验_外贸网站推广seo_网站怎么接广告

廊坊建站平台_网站推广经验_外贸网站推广seo_网站怎么接广告

时间:2025/7/8 21:15:47来源:https://blog.csdn.net/2401_83816455/article/details/143274384 浏览次数:0次
廊坊建站平台_网站推广经验_外贸网站推广seo_网站怎么接广告

队列是线性结构。

队列特点:先进先出/后进后出

用不带头单链表实现:

函数:

void InitQueue(Q* cat)
{assert(cat);cat->phead = cat->ptail = NULL;cat->size = 0;
}
void QueuePush(Q* cat, Type x)
{assert(cat);QN* newnode = (QN*)malloc(sizeof(QN));if (newnode == NULL){perror("malloc fail!");exit(1);}newnode->data = x;//newnode->next = NULL;if (cat->phead == NULL){cat->phead = cat->ptail = newnode;}else{cat->ptail->next = newnode;cat->ptail = cat->ptail->next;}cat->size++;
}
bool QueueEmpty(Q* cat)
{assert(cat);return cat->phead == NULL && cat->ptail == NULL;
}
void QueuePop(Q* cat)
{assert(cat);assert(!QueueEmpty(cat));//只有一个元素if (cat->phead == cat->ptail){free(cat->phead);cat->phead = cat->ptail = NULL;}else{QN* tmp = cat->phead->next;free(cat->phead);cat->phead = tmp;}--cat->size;
}
//取队头数据
Type QueueFront(Q* cat)
{assert(cat);assert(!QueueEmpty(cat));return cat->phead->data;
}
//取队尾数据
Type QueueBack(Q* cat)
{assert(cat);assert(!QueueEmpty(cat));return cat->ptail->data;
}
//队列中有效数据个数
int QueueSize(Q* cat)
{assert(cat);/*int size = 0;QN* tmp = cat->phead;while(tmp){size++;tmp = tmp->next;}return size;*/return cat->size;
}
//打印队列void Print(Q* cat)
{assert(cat);assert(!QueueEmpty(cat));QN* tmp = cat->phead;while (tmp != cat->ptail){printf("%d ", tmp->data);tmp = tmp->next;}printf("%d\n", cat->ptail->data);
}
//销毁队列
void QueueDestroy(Q* cat)
{assert(cat);assert(!QueueEmpty(cat));QN* tmp = cat->phead;while (tmp){//优化前/*QN* n = tmp->next; 电脑可能在此处报错访问越界(因为队尾数据后的数据未开辟空间,所以访问会越界,此时用优化后的代码free(tmp);tmp = n;*///优化后if (tmp == cat->ptail){free(tmp);return;}else{QN* n = tmp->next;free(tmp);tmp = n;}}cat->phead = cat->ptail = NULL;cat->size = 0;
}

头文件:

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
//USE LIST ACHIEVE
typedef int Type;
typedef struct QueueNode {Type data;struct QueueNode* next;
}QN;
typedef struct Queue {QN* phead;QN* ptail;int size;
}Q;
void InitQueue(Q* cat);
void QueuePush(Q* cat, Type x);
bool QueueEmpty(Q* cat);
void QueuePop(Q* cat);
Type QueueFront(Q* cat);
Type QueueBack(Q* cat);
int QueueSize(Q* cat);
void Print(Q* cat);
void QueueDestroy(Q* cat);

代码测试:

int main()
{Q dog;InitQueue(&dog);QueuePush(&dog,1);QueuePush(&dog,2);QueuePush(&dog,3);QueuePush(&dog,4);Print(&dog);QueuePop(&dog);Print(&dog);printf("size: %d", QueueSize(&dog));QueueDestroy(&dog);return 0;
}

结果:

77ea7fd3b079425dbf03d4233c5af0e4.png

谢谢观看!

 

 

 

 

关键字:廊坊建站平台_网站推广经验_外贸网站推广seo_网站怎么接广告

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: