栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构队列的插入删除练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构队列的插入删除练习

#include
#include
typedef struct queue {
int data;
struct queuenext;
}queue;
//队:FIFO尾插法【尾插头出】
//尾插法要先遍历到尾指针的位置
struct queue
init() {
queue* L = (queue*)malloc(sizeof(queue));
L->data = 0;// 表示当前栈的元素个数,为0时表示空栈
L->next = NULL;
return L;
}
void judje(queue* L){
//第一步判断队列是否为空
queue* un = L->next;
queue* a = L;
if (L->data == 0 || L->next == NULL)
printf(“队列空n”);
else {
printf(“对顶元素是:%dn”, L->next->data);
while(un->next!=NULL)
un = un->next;
printf(“队尾元素是:%dn”, un->data);
}
}
void insQueue(queue* L, int data) {
queue* S = (queue*)malloc(sizeof(queue));
queue* zj=L;
S->data = data;
while (zj->next != NULL)//遍历找最后一个节点
zj = zj->next;
S->next = zj->next;
zj->next = S;
L->data++;
}
void outQueue(queueL){//delete函数
if (L->data == 0 || L->next == NULL)
printf(“队列空,删除失败n”);
else {
queue
un = L->next;
queue* a= L->next;//L是头指针,L->next表示的是第一个元素
printf(“对头元素是:%dn”, a->data);
while(un->next!=NULL)
un = un->next;
printf(“队尾元素是:%dn”, un->data);
printf(“删除队头元素%dn新栈参数:n”, a->data);
L->next = a->next;
L ->data–;
free(a);//vs2022编译器中后面不再调用S结点即可成功执行free
//否则会是:free之前可运行但free之后的代码不能运行
}
}
void show(queue* L) {
queue* beiyong=L->next;
while (beiyong != NULL) {
printf("%d->", beiyong->data);
beiyong = beiyong->next;
}
printf(“NULLn”);
}
int main() {
queue* L = init();
insQueue(L, 1);
insQueue(L, 3);
insQueue(L, 5);
insQueue(L, 7);
insQueue(L, 9);
judje(L);
show(L);
printf(“n");
outQueue(L);
judje(L);
show(L);
printf("
n”);
outQueue(L);
judje(L);
show(L);
printf(“n");
outQueue(L);
judje(L);
show(L);
printf("
n”);
outQueue(L);
judje(L);
show(L);
printf(“n");
outQueue(L);
judje(L);
show(L);
printf("
n”);
queue* S = init();
judje(S);
show(S);
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768843.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号