C语言栈和队列综合应用
- 格式:docx
- 大小:16.70 KB
- 文档页数:9
数据结构实验三栈和队列的应用数据结构实验三:栈和队列的应用在计算机科学领域中,数据结构是组织和存储数据的重要方式,而栈和队列作为两种常见的数据结构,具有广泛的应用场景。
本次实验旨在深入探讨栈和队列在实际问题中的应用,加深对它们特性和操作的理解。
一、栈的应用栈是一种“后进先出”(Last In First Out,LIFO)的数据结构。
这意味着最后进入栈的元素将首先被取出。
1、表达式求值在算术表达式的求值过程中,栈发挥着重要作用。
例如,对于表达式“2 + 3 4”,我们可以通过将操作数压入栈,操作符按照优先级进行处理,实现表达式的正确求值。
当遇到数字时,将其压入操作数栈;遇到操作符时,从操作数栈中弹出相应数量的操作数进行计算,将结果压回操作数栈。
最终,操作数栈中的唯一值就是表达式的结果。
2、括号匹配在程序代码中,检查括号是否匹配是常见的任务。
可以使用栈来实现。
遍历输入的字符串,当遇到左括号时,将其压入栈;当遇到右括号时,弹出栈顶元素,如果弹出的左括号与当前右括号类型匹配,则继续,否则表示括号不匹配。
3、函数调用和递归在程序执行过程中,函数的调用和递归都依赖于栈。
当调用一个函数时,当前的执行环境(包括局部变量、返回地址等)被压入栈中。
当函数返回时,从栈中弹出之前保存的环境,继续之前的执行。
递归函数的执行也是通过栈来实现的,每次递归调用都会在栈中保存当前的状态,直到递归结束,依次从栈中恢复状态。
二、队列的应用队列是一种“先进先出”(First In First Out,FIFO)的数据结构。
1、排队系统在现实生活中的各种排队场景,如银行排队、餐厅叫号等,可以用队列来模拟。
新到达的顾客加入队列尾部,服务完成的顾客从队列头部离开。
通过这种方式,保证了先来的顾客先得到服务,体现了公平性。
2、广度优先搜索在图的遍历算法中,广度优先搜索(BreadthFirst Search,BFS)常使用队列。
从起始节点开始,将其放入队列。
利⽤栈和队列判断字符是否是回⽂(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. 打印队列:在打印机等资源共享环境中,通常会使用打印队列来管理多个打印请求。
每当用户提交一个打印请求时,将该请求加入打印队列的末尾,打印机从队列的头部取出请求进行打印。
这样可以保证每个用户的打印请求按照提交的顺序进行处理。
综上所述,栈和队列在不同的应用场景中发挥着重要作用。
栈和队列的应用栈和队列是计算机科学中非常重要的数据结构,它们在各种应用中被广泛使用。
本文将探讨栈和队列的应用,并讨论它们在不同场景下的具体用途。
一、栈的应用1. 浏览器的前进后退功能在使用浏览器时,我们可以通过点击前进按钮或后退按钮来切换网页。
这种功能实际上是由一个栈来实现的。
当我们访问新的网页时,当前页面被推入栈中,当我们点击后退按钮时,栈顶的页面被弹出并显示在浏览器中。
2. 函数调用栈在编写程序时,函数的调用和返回也是通过栈来管理的。
每当一个函数被调用时,相关的信息(例如参数、返回地址等)会被推入栈中,当函数执行完毕后,这些信息会从栈中弹出,程序会回到函数调用的地方继续执行。
3. 括号匹配在编写编译器或表达式计算器时,需要检查括号是否正确匹配。
这个问题可以使用栈来解决。
遍历表达式时,遇到左括号将其推入栈中,遇到右括号时,若栈顶元素是对应的左括号,则将栈顶元素弹出,继续处理下一个字符;若栈为空或栈顶元素不是对应的左括号,则括号不匹配。
二、队列的应用1. 消息队列消息队列是一种在分布式系统中实现异步通信的机制。
它常用于解耦系统中的组件,例如,一个组件将消息发送到队列中,而另一个组件则从队列中接收消息并处理。
这种方式可以提高系统的可伸缩性和可靠性。
2. 打印队列在打印机系统中,多个任务需要按照先后顺序进行打印。
这时可以使用队列来管理打印任务的顺序。
每当一个任务到达时,将其加入到队列的末尾,打印机从队列的头部取出任务进行打印,直到队列为空。
3. 广度优先搜索广度优先搜索(BFS)是一种常用的图搜索算法,它使用队列来辅助实现。
在BFS中,首先将起始节点加入队列中,然后依次将与当前节点相邻且未访问过的节点入队,直到遍历完所有节点。
结论栈和队列作为常用的数据结构,在计算机科学中有着广泛的应用。
本文只介绍了它们部分的应用场景,实际上它们还可以用于解决其他许多问题,如迷宫路径搜索、计算器计算等。
因此,了解和熟练运用栈和队列是程序员和计算机科学家的基本素养之一。
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,。
实验报告——栈和队列的应用第一篇:实验报告——栈和队列的应用实验5 栈和队列的应用目的和要求:(1)熟练栈和队列的基本操作;(2)能够利用栈与队列进行简单的应用。
一、题目题目1.利用顺序栈和队列,实现一个栈和一个队列,并利用其判断一个字符串是否是回文。
所谓回文,是指从前向后顺读和从后向前倒读都一样的字符串。
例如,a+b&b+a等等。
题目2.假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。
跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。
若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。
现要求写一算法模拟上述舞伴配对问题,并实现。
题目3.打印机提供的网络共享打印功能采用了缓冲池技术,队列就是实现这个缓冲技术的数据结构支持。
每台打印机具有一个队列(缓冲池),用户提交打印请求被写入到队列尾,当打印机空闲时,系统读取队列中第一个请求,打印并删除之。
请利用队列的先进先出特性,完成打印机网络共享的先来先服务功能。
题目4.假设以数组Q[m]存放循环队列中的元素, 同时设置一个标志tag,以tag == 0和tag == 1来区别在队头指针(front)和队尾指针(rear)相等时,队列状态为“空”还是“满”。
试编写与此结构相应的插入(enqueue)和删除(dlqueue)算法。
题目5.利用循环链队列求解约瑟夫环问题。
请大家从本组未讨论过的五道题中选择一道,参照清华邓俊辉老师MOOC视频及课本相关知识,编写相应程序。
选择题目3:打印机提供的网络共享打印功能采用了缓冲池技术,队列就是实现这个缓冲技术的数据结构支持。
二、程序清单//Ch3.cpp #include #include #include“ch3.h” template void LinkedQueue::makeEmpty()//makeEmpty//函数的实现{ LinkNode*p;while(front!=NULL)//逐个删除队列中的结点{p=front;front=front->link;delete p;} };template bool LinkedQueue::put_in(T&x){//提交命令函数if(front==NULL){//判断是否为空front=rear=new LinkNode;//如果为空,新结点为对头也为对尾front->data=rear->data=x;if(front==NULL)//分配结点失败return false;} else{rear->link=new LinkNode;//如不为空,在链尾加新的结点rear->link->data=x;if(rear->link==NULL)return false;rear=rear->link;} return true;};template bool LinkedQueue::carry_out()//执行命令函数 { if(IsEmpty()==true)//判断是否为空{return false;} cout<data<LinkNode*p=front;front=front->link;//删除以执行的命令,即对头修改delete p;//释放原结点return true;};void main()//主函数 { LinkedQueue q;//定义类对象char flag='Y';//标志是否输入了命令const int max=30;//一次获取输入命令的最大个数while(flag=='Y')//循环{ int i=0;char str[max];//定义存储屏幕输入的命令的数组gets(str);//获取屏幕输入的命令while(str[i]!=''){q.put_in(str[i]);//调用提交命令函数,将每个命令存入队列中i++;}for(int j=0;j<=i;j++){if(q.IsEmpty()==true)//判断是否为空,为空则说明没有可执行的命令{cout<cin>>flag;continue;//为空跳出for循环为下次输入命令做好准备}q.carry_out();//调用执行命令的函数,将命令打印并删除}三、程序调试过程中所出现的错误无。
停车场模拟-栈和队列综合应用假设停车场是一个宽度只能停放一辆汽车的狭长通道,只有一个大门可供汽车进出。
汽车在停车场内按到达的先后顺序依次排列,若车场内已停满汽车,则新到达的汽车只能在门外的便道上等候(假设便道无限长),一旦停车场内有汽车离场,则排在便道上的第一辆汽车即可进入;当停车场内某辆汽车要离场时(假设只有进入停车场的汽车才能离场),由于停车场是狭长的通道,在它之后进场的汽车必须先退出车场让路,待离场汽车离开停车场后,让路的汽车再按原次序进入车场。
请编写程序模拟对上述停车场的管理。
注意: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;}}}。