C语言栈和队列综合应用

合集下载

数据结构——用C语言描述(第3版)教学课件第3章 栈和队列

数据结构——用C语言描述(第3版)教学课件第3章 栈和队列

if(S->top==-1) /*栈为空*/
return(FALSE);
else
{*x = S->elem[S->top];
return(TRUE);
}
返回主目录}[注意]:在实现GetTop操作时,也可将参数说明SeqStack *S 改为SeqStack S,也就是将传地址改为传值方式。传 值比传地址容易理解,但传地址比传值更节省时间、 空间。
返回主目录
算法:
void BracketMatch(char *str) {Stack S; int i; char ch; InitStack(&S); For(i=0; str[i]!='\0'; i++) {switch(str[i])
{case '(': case '[': case '{':
3.1.3 栈的应用举例
1. 括号匹配问题
思想:在检验算法中设置一个栈,若读入的是左括号, 则直接入栈,等待相匹配的同类右括号;若读入的是 右括号,且与当前栈顶的左括号同类型,则二者匹配, 将栈顶的左括号出栈,否则属于不合法的情况。另外, 如果输入序列已读尽,而栈中仍有等待匹配的左括号, 或者读入了一个右括号,而栈中已无等待匹配的左括 号,均属不合法的情况。当输入序列和栈同时变为空 时,说明所有括号完全匹配。
return(TRUE);
}
返回主目录
【思考题】
如果将可利用的空闲结点空间组织成链栈来管理,则申 请一个新结点(类似C语言中的malloc函数)相当于链 栈的什么操作?归还一个无用结点(类似C语言中的 free函数)相当于链栈的什么操作?试分别写出从链栈 中申请一个新结点和归还一个空闲结点的算法。

数据结构实验三栈和队列的应用

数据结构实验三栈和队列的应用

数据结构实验三栈和队列的应用数据结构实验三:栈和队列的应用在计算机科学领域中,数据结构是组织和存储数据的重要方式,而栈和队列作为两种常见的数据结构,具有广泛的应用场景。

本次实验旨在深入探讨栈和队列在实际问题中的应用,加深对它们特性和操作的理解。

一、栈的应用栈是一种“后进先出”(Last In First Out,LIFO)的数据结构。

这意味着最后进入栈的元素将首先被取出。

1、表达式求值在算术表达式的求值过程中,栈发挥着重要作用。

例如,对于表达式“2 + 3 4”,我们可以通过将操作数压入栈,操作符按照优先级进行处理,实现表达式的正确求值。

当遇到数字时,将其压入操作数栈;遇到操作符时,从操作数栈中弹出相应数量的操作数进行计算,将结果压回操作数栈。

最终,操作数栈中的唯一值就是表达式的结果。

2、括号匹配在程序代码中,检查括号是否匹配是常见的任务。

可以使用栈来实现。

遍历输入的字符串,当遇到左括号时,将其压入栈;当遇到右括号时,弹出栈顶元素,如果弹出的左括号与当前右括号类型匹配,则继续,否则表示括号不匹配。

3、函数调用和递归在程序执行过程中,函数的调用和递归都依赖于栈。

当调用一个函数时,当前的执行环境(包括局部变量、返回地址等)被压入栈中。

当函数返回时,从栈中弹出之前保存的环境,继续之前的执行。

递归函数的执行也是通过栈来实现的,每次递归调用都会在栈中保存当前的状态,直到递归结束,依次从栈中恢复状态。

二、队列的应用队列是一种“先进先出”(First In First Out,FIFO)的数据结构。

1、排队系统在现实生活中的各种排队场景,如银行排队、餐厅叫号等,可以用队列来模拟。

新到达的顾客加入队列尾部,服务完成的顾客从队列头部离开。

通过这种方式,保证了先来的顾客先得到服务,体现了公平性。

2、广度优先搜索在图的遍历算法中,广度优先搜索(BreadthFirst Search,BFS)常使用队列。

从起始节点开始,将其放入队列。

利用栈和队列判断字符是否是回文(C语言)

利用栈和队列判断字符是否是回文(C语言)

利⽤栈和队列判断字符是否是回⽂(C语⾔)// File Name: palindrome.h//// Destination:利⽤栈和队列判断字符串是否是回⽂//#ifndef PALINDROME#define PALINDROME#include <stdio.h>// 链式队列结构的定义typedef char ElemType;typedef struct Node{char data; // 元素数据struct Node *next;// 链式队列中结点元素的指针}QNode,*QueuePtr;typedef struct{QueuePtr front;// 队列头指针QueuePtr rear;// 队列尾指针}LinkQueue;// 栈结构的定义typedef struct Stack{ElemType *base;ElemType *top;int stacksize;}SqStack;// 链式队列的基本操作bool InitQueue(LinkQueue *Q);bool EnQueue(LinkQueue *Q, ElemType e);bool DeQueue(LinkQueue *Q, ElemType *e);// 栈的基本操作bool InitStack(SqStack *S);bool Push(SqStack *S, ElemType e);bool Pop(SqStack *S, ElemType *e);#endif// File Name: palindrome.cpp//// Destination:利⽤栈和队列判断字符串是否是回⽂#include <stdlib.h>#include <malloc.h>#include "palindrome.h"const int STACK_INIT_SIZE = 100; // 初始分配的长度const int STACKINCREMENT = 10; // 分配内存的增量//操作⽬的:初始化队列//初始条件:⽆//操作结果:构造⼀个空的队列//函数参数://LinkQueue *Q 待初始化的队列//返回值:// bool 操作是否成功------------------------------------------------------------*/bool InitQueue(LinkQueue *Q){Q->front = Q->rear = (QueuePtr)malloc(sizeof (QNode)); if (!Q->front){exit(0);}Q->front->next = NULL;return true;}//操作⽬的:在队列末尾插⼊元素e//初始条件:队列Q已存在//操作结果:插⼊元素e作为队列新的尾结点//函数参数://LinkQueue *Q 队列Q//ElemType e 待插⼊的数据元素//返回值://bool 操作是否成功bool EnQueue(LinkQueue *Q, ElemType e){QueuePtr p = (QueuePtr)malloc(sizeof(QNode));if (!p){exit(0);}p->data = e;p->next = NULL;Q->rear->next = p;Q->rear = p;return true;}//操作⽬的:删除链式队列的头结点//初始条件:队列Q已存在//操作结果:删除链式队列的头结点//函数参数://LinkQueue *Q 队列Q//ElemType *e 待插⼊的数据元素//返回值://bool 操作是否成功bool DeQueue(LinkQueue *Q, ElemType *e){if (Q->front == Q->rear) //空队列{return false;}QueuePtr p = Q->front->next;//p指向头结点的下个位置 *e = p->data;Q->front->next = p->next;if (Q->rear == p){Q->rear = Q->front;}free(p);return true;}//操作⽬的:初始化栈//初始条件:⽆//操作结果:构造⼀个空的栈//函数参数:// SqStack *S 待初始化的栈//返回值:// bool 操作是否成功bool InitStack(SqStack *S){S->base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));if (S->base == NULL){return false;}S->top = S->base;S->stacksize = STACK_INIT_SIZE;return true;}//操作⽬的:压栈——插⼊元素e为新的栈顶元素//初始条件:栈S已存在//操作结果:插⼊数据元素e作为新的栈顶//函数参数://SqStack *S 栈S//ElemType e 待插⼊的数据元素//返回值:// bool 操作是否成功bool Push(SqStack *S, ElemType e){if (S->top - S->base >= S->stacksize){S->base = (ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType)); if (!S->base){return false;}S->top = S->base + S->stacksize;S->stacksize += STACKINCREMENT;}*(S->top++) = e;return true;}//操作⽬的:弹栈——删除栈顶元素//初始条件:栈S已存在且⾮空//操作结果:删除S的栈顶元素,并⽤e返回其值//函数参数://SqStack *S 栈S//ElemType *e 被删除的数据元素值//返回值://bool 操作是否成功bool Pop(SqStack *S, ElemType *e){if (S->top == S->base)return false;*e = (*--S->top);return true;}// File Name: main.cpp//// Destination:利⽤栈和队列判断字符串是否是回⽂//------------------------------------------------------#include <stdio.h>#include <stdlib.h>#include "palindrome.h"int main (){//声明⼀个栈⼀个队列SqStack S;LinkQueue L;//初始化⼀个栈⼀个队列InitStack (&S);InitQueue (&L);char c[30];ElemType a,b;printf("请输⼊要判断的字符,以@结束(最多30个字符):"); scanf("%s",c);int i = 0;int flag1 = 0;int flag2 = 0;while (c[i] != '@'){Push(&S,c[i]); //⼊栈EnQueue(&L,c[i]);//⼊队列flag1++;i++;}while (!(S.top == S.base)){Pop(&S,&b);//出栈DeQueue(&L,&a);//出队列if (a==b){flag2++;}else{break;}}if (flag1 == flag2){printf("\n是回⽂!\n\n");}else{printf("\n不是回⽂!\n\n");}system("pause");return 0;}。

数据结构中的栈与队列的应用场景

数据结构中的栈与队列的应用场景

数据结构中的栈与队列的应用场景栈与队列是数据结构中常见的两种基本数据类型,它们在不同的应用场景中发挥着重要作用。

下面将分别介绍栈和队列的应用场景。

栈的应用场景:1. 编辑器的撤销操作:在编辑器中,撤销(undo)操作是一个常见需求。

撤销操作通常是按照用户操作的反序执行,因此可以使用栈来存储每一次的操作,当用户执行撤销操作时,从栈中弹出最近的操作并执行对应的反操作。

2. 后退按钮的实现:在浏览器中,后退按钮用于返回上一个访问的网页。

通过使用栈来存储用户的访问记录,每当用户访问一个新的页面时,将该页面的地址压入栈中。

当用户点击后退按钮时,从栈中弹出最近访问的页面地址并跳转到该页面。

3. 函数调用与返回:在程序中,函数的调用和返回通常遵循“后进先出”的原则,即后调用的函数先返回。

因此,可以使用栈来实现函数调用与返回的过程。

每当一个函数被调用时,将该函数的执行环境(包括参数、局部变量等)压入栈中;当函数执行完毕后,从栈中弹出该函数的执行环境,恢复上一个函数的执行。

队列的应用场景:1. 消息队列:在分布式系统和异步通信中,消息队列用于解耦发送方和接收方之间的耦合性。

发送方将消息发送到队列的末尾,接收方从队列的头部获取消息进行处理。

消息队列可以实现异步处理、削峰填谷等功能,常见的消息队列系统有RabbitMQ和Kafka等。

2. 操作系统中的进程调度:在操作系统中,进程调度用于控制多个进程的执行顺序。

常见的调度算法中,有使用队列来实现的先来先服务(FCFS)调度算法和轮转调度算法。

进程按照到达时间的顺序加入队列,在CPU空闲时,从队列的头部取出一个进程执行。

3. 打印队列:在打印机等资源共享环境中,通常会使用打印队列来管理多个打印请求。

每当用户提交一个打印请求时,将该请求加入打印队列的末尾,打印机从队列的头部取出请求进行打印。

这样可以保证每个用户的打印请求按照提交的顺序进行处理。

综上所述,栈和队列在不同的应用场景中发挥着重要作用。

《数据结构(C语言)》第3章 栈和队列

《数据结构(C语言)》第3章 栈和队列
Data structures

❖ 栈的顺序存储与操作 ❖ 1.顺序栈的定义
(1) 栈的静态分配顺序存储结构描述 ② top为整数且指向栈顶元素 当top为整数且指向栈顶元素时,栈空、入栈、栈满 及出栈的情况如图3.2所示。初始化条件为 S.top=-1。
(a) 栈空S.top==-1 (b) 元素入栈S.stack[++S.top]=e (c) 栈满S.top>=StackSize-1 (d) 元素出栈e=S.stack[S.top--]
/*栈顶指针,可以指向栈顶
元素的下一个位置或者指向栈顶元素*/
int StackSize; /*当前分配的栈可使用的以 元素为单位的最大存储容量*/
}SqStack;
/*顺序栈*/
Data structures

❖ 栈的顺序存储与操作 ❖ 1.顺序栈的定义
(2) 栈的动态分配顺序存储结构描述 ① top为指针且指向栈顶元素的下一个位置 当top为指针且指向栈顶元素的下一个位置时,栈空 、入栈、栈满及出栈的情况如图3.3所示。初始化条 件为S.top=S.base。
…,n-1,n≥0} 数据关系:R={< ai-1,ai>| ai-1,ai∈D,i=1,2
,…,n-1 } 约定an-1端为栈顶,a0端为栈底 基本操作:
(1) 初始化操作:InitStack(&S) 需要条件:栈S没有被创建过 操作结果:构建一个空的栈S (2) 销毁栈:DestroyStack(&S) 需要条件:栈S已经被创建 操作结果:清空栈S的所有值,释放栈S占用的内存空间
return 1;
}
Data structures

栈、队列及其应用

栈、队列及其应用

线性表的应用
• 4、
一元多项式加减运算 • 【问题描述】 • 给定一个一元n次多项式p和一个一元m次多项式Q,求它们的和与 差。
• 【算法分析】 • 选方法②。遍历两个单链表.根据指数和系数进行相应的加减,生 成一个新链表系数为0的结点删除掉(或不生成这种结点),输出该链 表。
特殊线性结构——栈
栈的存储方式
• 链接存储:即链栈,目的是提高空间利用 率,但编程复杂度提高了。 • type link=^node; • node=record • data:element; • next:link; • end; • Var top:link;
栈基本操作的实现
• • • • • • • • • • • 顺序存储栈的操作 栈的初始化 Procedure inistack(var s:stack); begin s.top=0 end; 判断栈空 function sempty(s:stack):boolean; begin sempty:=(s.top=0) end; 入栈 Procedure push(var s:stack; x:selement); begin if s.top=smaxsize then writeln(‘overflow’) else begin s.top:=s.top+1; s.data[s.top] end;
• • •
②用数组实现 【算法分析】 在组织数据时,也可以考虑只记录仍在圈中的猴子的情况。用一个 线性表按编号由小到大依次记录圈中所有猴子的编号,每当有猴子出 圈时,即从线性表中删除对应元素,表中元素减少一个。程序中用变 量rest表示圈中剩余的猴子数,即线性表中元素的总数。
• ③用单向循环链表实现 • 【算法分析】结点的数据域为猴子的编号,指针域指向下一个猴子。 报数实际上是计数,只要设一个计数器就可以了。当计数器由0变化 到n时,删除该结点,计数器回0继续计数(或者用求余运算)。直到链 表中剩下一个结点。

栈和队列的应用

栈和队列的应用

栈和队列的应用栈和队列是计算机科学中非常重要的数据结构,它们在各种应用中被广泛使用。

本文将探讨栈和队列的应用,并讨论它们在不同场景下的具体用途。

一、栈的应用1. 浏览器的前进后退功能在使用浏览器时,我们可以通过点击前进按钮或后退按钮来切换网页。

这种功能实际上是由一个栈来实现的。

当我们访问新的网页时,当前页面被推入栈中,当我们点击后退按钮时,栈顶的页面被弹出并显示在浏览器中。

2. 函数调用栈在编写程序时,函数的调用和返回也是通过栈来管理的。

每当一个函数被调用时,相关的信息(例如参数、返回地址等)会被推入栈中,当函数执行完毕后,这些信息会从栈中弹出,程序会回到函数调用的地方继续执行。

3. 括号匹配在编写编译器或表达式计算器时,需要检查括号是否正确匹配。

这个问题可以使用栈来解决。

遍历表达式时,遇到左括号将其推入栈中,遇到右括号时,若栈顶元素是对应的左括号,则将栈顶元素弹出,继续处理下一个字符;若栈为空或栈顶元素不是对应的左括号,则括号不匹配。

二、队列的应用1. 消息队列消息队列是一种在分布式系统中实现异步通信的机制。

它常用于解耦系统中的组件,例如,一个组件将消息发送到队列中,而另一个组件则从队列中接收消息并处理。

这种方式可以提高系统的可伸缩性和可靠性。

2. 打印队列在打印机系统中,多个任务需要按照先后顺序进行打印。

这时可以使用队列来管理打印任务的顺序。

每当一个任务到达时,将其加入到队列的末尾,打印机从队列的头部取出任务进行打印,直到队列为空。

3. 广度优先搜索广度优先搜索(BFS)是一种常用的图搜索算法,它使用队列来辅助实现。

在BFS中,首先将起始节点加入队列中,然后依次将与当前节点相邻且未访问过的节点入队,直到遍历完所有节点。

结论栈和队列作为常用的数据结构,在计算机科学中有着广泛的应用。

本文只介绍了它们部分的应用场景,实际上它们还可以用于解决其他许多问题,如迷宫路径搜索、计算器计算等。

因此,了解和熟练运用栈和队列是程序员和计算机科学家的基本素养之一。

c语言堆栈和队列函数大全

c语言堆栈和队列函数大全

C语言堆栈和队列函数大全一.顺序栈1.宏定义#include<stdio.h>#include<stdlib.h>#define MAXSIZE ****#define datatype ****2.结构体typedef struct{datatype data[MAXSIZE];int top;}Seqstack;3.基本函数Seqstack *Init_Seqstack()/*置空栈函数(初始化)1.先决条件:无;2.函数作用:首先建立栈空间,然后初始化栈顶指针,返回栈s的地址*/{Seqstack *s;s=(Seqstack *)malloc(sizeof(Seqstack));s->top=-1;return s;}int Empty_Seqstack(Seqstack *s) /*判栈空函数1.先决条件:初始化顺序栈;2.函数作用:判断栈是否为空,空返回1,不空返回0*/ {if(s->top==-1) return 1;else return 0;}int Push_Seqstack(Seqstack *s,datatype x) /*入栈函数1.先决条件:初始化顺序栈2.函数作用:将数据x入栈,栈满则不能,成功返回1,因栈满失败返回0*/ {if(s->top==MAXSIZE-1)return 0;s->top=s->top+1;s->data[s->top]=x;return 1;}int Pop_Seqstack(Seqstack *s,datatype *x) acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtainedafter weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples ofash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,/*出栈函数1.先决条件:初始化顺序栈2.函数作用:从栈中出一个数据,并将其存放到x中,成功返回1,因栈空失败返回0*/{if(s->top==-1)return 0;*x=s->data[s->top];s->top--;return 1;}int Top_Seqstack(Seqstack *s,datatype *x)/*取栈顶元素函数1.先决条件:初始化顺序栈2.函数作用:取栈顶元素,并把其存放到x中,成功返回1,因栈空失败返回0*/{if(s->top==-1)return 0;*x=s->data[s->top];return 1;}int Printf_Seqstack(Seqstack *s) /*遍历顺序栈函数1.先决条件:初始化顺序栈2.函数作用:遍历顺序栈,成功返回1*/ {int i,j=0;for(i=s->top;i>=0;i--){printf("%d ",s->data[i]);/*因datatype不同而不同*/j++;if(j%10==0)printf("\n");}printf("\n");return 1;}int Conversation_Seqstack(int N,int r) /*数制转换函数(顺序栈)1.先决条件:具有置空栈,入栈,出栈函数2.函数作用:将N转换为r进制的数*/{Seqstack *s;datatype x;printf("%d转为%d进制的数为:",N,r);/*以后可以删除去*/s=Init_Seqstack();do{Push_Seqstack(s,N%r);N=N/r;acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectivelyadequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible mustfirst wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,}while(N);while(Pop_Seqstack(s,&x)){if(x>=10)/*为了能转为十进制以上的*/printf("%c",x+55);elseprintf("%d",x);}free(s);/*释放顺序栈*/printf("\n");return 1;}4.主函数int main(){Seqstack *s;int choice;datatype x;do{printf("************************************************************ ****\n");printf("1.置空栈 2.判栈空 3.入栈 4.出栈 5.取栈顶元素 6.遍历 7.退出\n");printf("************************************************************ ****\n");printf("请输入选择(1~7):");scanf("%d",&choice);getchar();switch(choice){case 1:s=Init_Seqstack();if(s)printf("置空栈成功!\n");break;case 2:if(Empty_Seqstack(s))printf("此为空栈.\n");elseprintf("此不为空栈.\n");;break;case 3:printf("请输入一个整数:");scanf("%d",&x);if(Push_Seqstack(s,x))printf("入栈成功.\n");elseprintf("栈已满,无法入栈.\n");;break;case 4:if(Pop_Seqstack(s,&x)) acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible must first wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water.Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing, printf("出栈成功,出栈元素为:%d\n",x);elseprintf("出栈失败,因栈为空.\n");break;case 5:if(Top_Seqstack(s,&x))printf("取栈顶元素成功,栈顶元素为:%d\n",x);elseprintf("取栈顶元素失败,因栈为空.\n");break;case 6:Printf_Seqstack(s);break;case 7:printf("谢谢使用!\n");break;default :printf("输入错误,请重新输入!\n");break;}}while(choice!=7);return 0;}二.链栈1.宏定义#include<stdio.h>#include<stdlib.h>#define datatype ****2.结构体typedef struct snode{datatype data;struct snode *next;}Stacknode,*Linkstack;3.基本函数Linkstack Init_Linkstack()/*初始化栈函数1.先决条件:无2.函数作用:初始化链栈,返回top地址*/ { Linkstack top;top=(Linkstack)malloc(sizeof(Stacknode));top->next=NULL;return top;}int Empty_Linkstack(Linkstack top) /*判栈空函数1.先决条件:初始化链栈2.函数作用:判断栈是否为空,空返回1,不空返回0*/{if(top->next==NULL)acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles fordetermination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible mustfirst wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,return 1;else return 0;}int Push_Linkstack(Linkstack top,datatype x) /*入栈函数1.先决条件:初始化链栈2.函数作用:将数据x入栈,成功返回1,失败返回0*/ { Stacknode *p;p=(Stacknode *)malloc(sizeof(Stacknode));p->data=x;p->next=top->next;top->next=p;return 1;}int Pop_Linkstack(Linkstack top,datatype *x) /*出栈函数1.先决条件:初始化链栈2.函数作用:若栈空退出,若没空则将数据出栈,并将其存放到x中,成功返回1,因栈空失败返回0*/{if(top->next==NULL)return 0;Stacknode *p=top->next;*x=p->data;top->next=p->next;free(p);return 1;}int Top_Linkstack(Linkstack top,datatype *x) /*取栈顶元素函数1.先决条件:初始化链栈2.函数作用:取栈顶元素并放到x中,成功返回1,因栈空失败返回0*/{if(top->next==NULL)return 0;*x=top->next->data;return 1;}int Printf_Linkstack(Linkstack top) /*遍历链栈函数1.先决条件:初始化链栈2.函数作用:遍历链栈,成功返回1*/ {Stacknode *p=top->next;int j=0;while(p){printf("%d ",p->data);/*因datatype不同而不同*/j++;if(j%10==0)acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashingfurnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible mustfirst wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,printf("\n");p=p->next;}printf("\n");return 1;}int Conversation_Linkstack(int N,int r)/*数制转换函数(链栈)1.先决条件:具有置空栈,入栈,出栈函数2.函数作用:将N转换为r进制的数*/{Linkstack top;datatype x;printf("%d转为%d进制的数为:",N,r);/*以后可以删除去*/top=Init_Linkstack();do{Push_Linkstack(top,N%r);N=N/r;}while(N);while(Pop_Linkstack(top,&x)){if(x>=10)/*为了能转为十进制以上的*/printf("%c",x+55);elseprintf("%d",x);}printf("\n");free(top);/*释放栈顶空间*/return 1;}4.主函数int main(){Linkstack top;int choice;datatype x;do{printf("************************************************************ ****\n");printf("1.置空栈 2.判栈空 3.入栈 4.出栈 5.取栈顶元素 6.遍历 7.退出\n");printf("************************************************************ ****\n");printf("请输入选择(1~7):");acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. This value should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible mustfirst wash with boiling dilute hydrochloric acid, then wash with a lotof water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,scanf("%d",&choice);getchar();switch(choice){case 1:top=Init_Linkstack();if(top)printf("置空栈成功!\n");break;case 2:if(Empty_Linkstack(top))printf("此为空栈.\n");elseprintf("此不为空栈.\n");;break;case 3:printf("请输入一个整数:");scanf("%d",&x);if(Push_Linkstack(top,x))printf("入栈成功.\n");elseprintf("栈已满,无法入栈.\n");;break;case 4:if(Pop_Linkstack(top,&x))printf("出栈成功,出栈元素为:%d\n",x);elseprintf("出栈失败,因栈为空.\n");break;case 5:if(Top_Linkstack(top,&x))printf("取栈顶元素成功,栈顶元素为:%d\n",x);elseprintf("取栈顶元素失败,因栈为空.\n");break;case 6:Printf_Linkstack(top);break;case 7:printf("谢谢使用!\n");break;default :printf("输入错误,请重新输入!\n");break;}}while(choice!=7);return 0;}二.队列1.宏定义2.结构体3.基本函数4.主函数acidity, mL.; M--calibration of the molar concentration of sodium hydroxide standard solution, moI/L; V--amount of the volume of sodium hydroxide standard solution, Ml; M--the weight of the sample, g. Such as poor meets the requirements, take the arithmetic mean of the second determination as a result. Results one decimal. 6, allowing differential analyst simultaneously or in quick succession for the second determination, the absolute value of the difference of the results. Thisvalue should be no more than 1.0. 1, definitions and principles for determination of ash in starches, starch and ash: starch samples of ash the residue obtained after weight. Original sample residue weight of sample weight or weight expressed as a percentage of the dry weight of the sample. Samples of ash at 900 ? high temperature until ashing sample ... The Crucible: determination of Platinum or other conditions of the affected material, capacity of 50mL. Dryer: has effectively adequate drying agent and-perforated metal plate or porcelain. Ashing furnaces: device for controlling and regulating temperature, offers 900 incineration temperature of 25 c. Analytical balance. Electric hot plate or Bunsen. 3, crucible of analysis steps preparation: Crucible mustfirst wash with boiling dilute hydrochloric acid, then wash with a lot of water and then rinse with distilled water. Wash the Crucible within ashing furnace, heated at 900 to 25 ? 30min, and in the desiccator to cool to room temperature and then weighing,。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

停车场模拟-栈和队列综合应用假设停车场是一个宽度只能停放一辆汽车的狭长通道,只有一个大门可供汽车进出。

汽车在停车场内按到达的先后顺序依次排列,若车场内已停满汽车,则新到达的汽车只能在门外的便道上等候(假设便道无限长),一旦停车场内有汽车离场,则排在便道上的第一辆汽车即可进入;当停车场内某辆汽车要离场时(假设只有进入停车场的汽车才能离场),由于停车场是狭长的通道,在它之后进场的汽车必须先退出车场让路,待离场汽车离开停车场后,让路的汽车再按原次序进入车场。

请编写程序模拟对上述停车场的管理。

注意:1.车辆信息自定义2.程序功能需使用栈和队列的基本操作实现3.栈使用顺序存储结构,队列使用链式存储结构4.要求能够模拟车辆进场、车辆离开及查看停车场的信息#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAXSIZE 3//类型定义typedef int datatype;// 1)顺序栈typedef struct{datatype data_1[MAXSIZE];int top;}SeqStack;// 2)链式队列typedef struct node{datatype data_2;struct node *next;}QNode;typedef struct{QNode *front,*rear;}LQueue;//顺序栈的基本操作//1.置空栈SeqStack *Init_SeqStack(){SeqStack *s;s=(SeqStack *)malloc(sizeof(SeqStack));s->top=-1;return s;}//2.入栈int Push_SeqStack(SeqStack *s,datatype x) {if(s->top==MAXSIZE-1)return 0; //栈满不能入栈else{s->top++;s->data_1[s->top]=x;return 1; //入栈成功返回1}}//3.判空栈int Empty_SeqStack(SeqStack *s){if(s->top==-1)return 1;elsereturn 0;}//4.出栈int Pop_SeqStack(SeqStack *s,datatype x) {if(Empty_SeqStack(s)==-1)return 0; //栈空不能出栈else{x=s->data_1[s->top];s->top--;return 1;}}//5.取出栈顶元素datatype Top_SeqStack(SeqStack *s){if(Empty_SeqStack(s)==-1)printf("SeqStack is empty\n"); //空栈elsereturn s->data_1[s->top];}//链式队列的基本操作//1.创建一个带头结点的空队列LQueue *Init_LQueue(){LQueue *q;QNode *p;q=(LQueue *)malloc(sizeof(LQueue));p=(QNode *)malloc(sizeof(QNode));p->next=NULL;q->front=q->rear=p;return q;}//2.入队列void In_LQueue(LQueue *q,datatype x){QNode *p;p=(QNode *)malloc(sizeof(QNode));p->data_2=x;p->next=NULL;q->rear->next=p;q->rear=p;}//3.判队列空int Empty_LQueue(LQueue *q){if(q->front==q->rear)return 0;elsereturn 1;}//4.出队列int Out_LQueue(LQueue *q){QNode *p;if(Empty_LQueue(q)==0){printf("队列空\n");return 0;}else{p=q->front->next;q->front->next=p->next;free(p);if(q->front->next==NULL)q->rear=q->front; //如果只有一个元素时,出队后队空,此时还需要修改队尾指针return 1;}}//功能实现//1.插入停车信息void Insert_stop(SeqStack *s,LQueue *q){int i;datatype x;printf("Enter Car No:");scanf("%d",&x);i=Push_SeqStack(s,x);if(i==0) //如果栈满,返回值为0,表示停车位已满{In_LQueue(q,x);printf("car %d is waiting\n",q->rear->data_2);}if(i==1) //如果入栈成功,返回值为1,表示车进入成功{printf("car %d is entring\n",s->data_1[s->top]);}}//车辆离开停车位void leaving(SeqStack *s,LQueue *q){datatype x;SeqStack *t=Init_SeqStack();int i,j,k=0;if(s->top==-1) //如果车位是空的printf("parking is empty\n");else //如果车位不是空的{printf("Leave car NO:");scanf("%d",&x);if(s->data_1[s->top]==x) //如果要离开的车是最后一辆进来的车{i=Pop_SeqStack(s,s->data_1[s->top]); //最后一辆车离开if(q->front->next){i=Push_SeqStack(s,q->front->next->data_2); //等待的车进去printf("Car %d is entring\n",q->front->next->data_2);j=Out_LQueue(q);}printf("Car %d is leaving\n",x);}else //如果要离开的车不是最后一辆进来的车{while(s->data_1[s->top]!=x){printf("Car %d is giving way\n",s->data_1[s->top]);i=Push_SeqStack(t,s->data_1[s->top]); //车退到临时停车位j=Pop_SeqStack(s,s->data_1[s->top]); //删除停车位中退到临时停车位的车辆信息if(s->top==-1) //如果没有查找到要离开的车辆信息{printf("Input error\n");k=1;break;}}if(k==0) //如果找到了要离开的{j=Pop_SeqStack(s,s->data_1[s->top]); //需要离开的那一辆车离开printf("Car %d is leaving\n",x);}if(s->data_1[s->top]!=x) //退到临时停车位的车辆重新回到停车位内{while(t->top!=-1){printf("Car %d is reentring\n",t->data_1[t->top]);i=Push_SeqStack(s,t->data_1[t->top]);j=Pop_SeqStack(t,t->data_1[t->top]);}}if(q->front->next&&k!=1) //候车区的车进去如果;k==1说明没有查到要离开的车,候车区的车不会进入{i=Push_SeqStack(s,q->front->next->data_2);printf("Car %d is entring\n",q->front->next->data_2);j=Out_LQueue(q);}}}}//车辆信息void print_Car(SeqStack *s,LQueue *q){int i;QNode *p;printf("Car is parking:\n");for(i=0;i<=s->top;i++){printf("\t%d",s->data_1[i]);}printf("\nCar is waiting:\n");p=q->front->next;while(p!=NULL){printf("\t%d",p->data_2);p=p->next;}printf("\n");}//退出void Destroy_SeqStack(SeqStack **s){if(*s)free(*s);*s=NULL;}void Destroy_LNode(LQueue *q){QNode *t1,*t2;t1=q->front->next;while(t1){t2 = t1;t1 = t1->next;free(t2);}if(t1==NULL)printf("销毁成功,请退出\n");elseprintf("销毁失败\n");}//main函数void printChoice(){printf("\nOptions:\n");printf("\t1.Car come\n");printf("\t2.Car leave\n");printf("\t3.Show cars\n");printf("\t4.Exit\n");}int main(){SeqStack *s=Init_SeqStack();LQueue *q=Init_LQueue();int choice=-1;while(1){printChoice();scanf("%d",&choice);switch(choice){case 1:Insert_stop(s,q);break;case 2:leaving(s,q);break;case 3:print_Car(s,q);break;case 4:Destroy_SeqStack(&s);Destroy_LNode(q);exit(0);break;default:printf("input error\n");break;}}}。

相关文档
最新文档