C语言栈和队列的基本操作
c语言队列的实现以及操作

c语言队列的实现以及操作摘要: 队列是数据结构中的一种,在实际生活中也有广泛的应用,本文通过介绍c语言的相关基础知识和算法,实现基本的队列结构以及其中的插入,删除,遍历,清空操作。
关键词:C语言;实现;队列;操作队列是数据结构中的一种,它按照先进先出的原则存储数据,可以分为循环队列和非循环队列,本文将结合c语言的基础知识,利用简单的算法实现非循环队列以及其中的插入,删除,遍历,清空操作。
一、实现队列1.声明结构体在使用c语言实现队列时,首先需要声明一个结构体Queue来定义队列,并定义队头和队尾指针,表示队列的入口和出口,以及空间的大小容量maxSize,并且用一个数组data[]来存储数据。
struct Queue{int data[maxSize]; //存储数据int front; //队头int rear; //队尾int maxSize; //队列容量};2.初始化队列在进行队列操作之前,我们需要将队列初始化,将队头队尾指针置于初始位置,表示队列为空,并将队列最大容量maxSize赋值。
void InitQueue(Queue *queue){queue->front = 0;queue->rear = 0;queue->maxSize = maxSize;}3.入队操作在进行入队操作时,我们先判断队列是否已满,如果未满,就将数据入队,并将队尾指针加一,否则返回队列已满的错误信息。
bool EnQueue(Queue *queue,int data){if((queue->rear+1)%queue->maxSize == queue->front) //队列满return false;queue->data[queue->rear] = data;queue->rear = (queue->rear+1)%queue->maxSize;return true;}4.出队操作在进行出队操作时,我们先判断队列是否为空,如果不为空,就将队头指针对应的数据出队,并将队头指针加一,否则返回队列已空的错误信息。
数据结构——用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函数)相当于链栈的什么操作?试分别写出从链栈 中申请一个新结点和归还一个空闲结点的算法。
数据结构(C语言)第3章 栈和队列

Data Structure
2013-8-6
Page 13
栈的顺序存储(顺序栈)
利用一组地址连续的存储单元依次存放自栈底到栈顶的数 据元素。 结构定义: #define STACK_INIT_SIZE 100; // 存储空间初始分配量 #define STACKINCREMENT 10; // 存储空间分配增量 typedef struct { SElemType *base; // 存储空间基址 SElemType *top; // 栈顶指针 int stacksize; // 当前已分配的存储空间,以元素位单位 } SqStack;
解决方案2:
顺序栈单向延伸——使用一个数组来存储两个栈
Data Structure 2013-8-6 Page 21
两栈共享空间 两栈共享空间:使用一个数组来存储两个栈,让一个 栈的栈底为该数组的始端,另一个栈的栈底为该数组 的末端,两个栈从各自的端点向中间延伸。
Data Structure
2013-8-6
链栈需要加头结点吗? 链栈不需要附设头结点。
Data Structure
2013-8-6
Page 27
栈的链接存储结构及实现
Data Structure
2013-8-6
Page 11
GetTop(S, &e) 初始条件:栈 S 已存在且非空。 操作结果:用 e 返回S的栈顶元素。 Push(&S, e) 初始条件:栈 S 已存在。 操作结果:插入元素 e 为新的栈顶元素。 Pop(&S, &e) 初始条件:栈 S 已存在且非空。 操作结果:删除 S 的栈顶元素,并用 e 返回其值。
Data Structure
数据结构(c语言版)课后习题答案完整版

数据结构(c语言版)课后习题答案完整版数据结构(C语言版)课后习题答案完整版一、数据结构概述数据结构是计算机科学中一个重要的概念,用来组织和存储数据,使之可以高效地访问和操作。
在C语言中,我们可以使用不同的数据结构来解决各种问题。
本文将提供完整版本的C语言数据结构的课后习题答案。
二、顺序表1. 顺序表的定义和基本操作顺序表是一种线性表,其中的元素在物理内存中连续地存储。
在C 语言中,我们可以通过定义结构体和使用指针来实现顺序表。
以下是顺序表的一些基本操作的答案:(1)初始化顺序表```ctypedef struct{int data[MAX_SIZE];int length;} SeqList;void InitList(SeqList *L){L->length = 0;}```(2)插入元素到顺序表中```cbool Insert(SeqList *L, int pos, int elem){if(L->length == MAX_SIZE){return false; // 顺序表已满}if(pos < 1 || pos > L->length + 1){return false; // 位置不合法}for(int i = L->length; i >= pos; i--){L->data[i] = L->data[i-1]; // 向后移动元素 }L->data[pos-1] = elem;L->length++;return true;}```(3)删除顺序表中的元素```cbool Delete(SeqList *L, int pos){if(pos < 1 || pos > L->length){return false; // 位置不合法}for(int i = pos; i < L->length; i++){L->data[i-1] = L->data[i]; // 向前移动元素 }L->length--;return true;}```(4)查找顺序表中的元素```cint Search(SeqList L, int elem){for(int i = 0; i < L.length; i++){if(L.data[i] == elem){return i + 1; // 找到元素,返回位置 }}return -1; // 未找到元素}```2. 顺序表习题解答(1)逆置顺序表```cvoid Reverse(SeqList *L){for(int i = 0; i < L->length / 2; i++){int temp = L->data[i];L->data[i] = L->data[L->length - 1 - i]; L->data[L->length - 1 - i] = temp;}}```(2)顺序表元素去重```cvoid RemoveDuplicates(SeqList *L){for(int i = 0; i < L->length; i++){for(int j = i + 1; j < L->length; j++){if(L->data[i] == L->data[j]){Delete(L, j + 1);j--;}}}}```三、链表1. 单链表单链表是一种常见的链式存储结构,每个节点包含数据和指向下一个节点的指针。
《数据结构(C语言)》第3章 栈和队列

栈
❖ 栈的顺序存储与操作 ❖ 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
栈
数据结构第3章栈

13
(4)取栈顶元素操作
Elemtype gettop(sqstack *s) { /*若栈s不为空,则返回栈顶元素*/ If(s->top<0) return NULL; /*栈空*/ return (s->stack[s->top]); }
。
29
算术表达式求值
在计算机中,任何一个表达式都是由: 操作数(operand)、运算符(operator)和 界限符(delimiter)组成的。 其中操作数可以是常数,也可以是变量或常量的 标识符;运算符可以是算术运算体符、关系运算符和 逻辑符;界限符为左右括号和标识表达式结束的结束 符。
30
6
存储结构
栈是一种特殊的线性表,有两种存储方式: 顺序存储结构存储
链式存储结构存储。
7
顺序栈的数组表示
与第二章讨论的一般的顺序存储结构的线性表 一样,利用一组地址连续的存储单元依次存放自 栈底到栈顶的数据元素,这种形式的栈也称为顺 序栈。 使用一维数组来作为栈的顺序存储空间。 设指针top指向栈顶元素的当前位置,以数组 小下标的一端作为栈底。 top=0时为空栈,元素进栈时指针top不断地 加1,当top等于数组的最大下标值时则栈满。
5)假如读出的运算符的优先级不大于运算符栈栈顶运算符
的优先级,则从操作数栈连续退出两个操作数,从运算符栈中 退出一个运算符,然后作相应的运算,并将运算结果压入操作 数栈。此时读出的运算符下次重新考虑(即不读入下一个符号 )。
数据结构c语言版耿国华课后习题答案

数据结构c语言版耿国华课后习题答案数据结构是计算机科学中非常重要的一门课程,它涉及到了计算机程序设计中的数据组织、存储和操作等方面。
而耿国华教授的《数据结构c语言版》是这门课程中的经典教材之一,它通过讲解各种数据结构的原理和实现方法,帮助学生更好地理解和掌握这门课程的知识。
本文将针对《数据结构c语言版》中的一些典型习题进行解答,帮助读者更好地理解和掌握这些知识点。
1. 线性表线性表是数据结构中最基本的一种数据结构,它包含了顺序表和链表两种实现方式。
在习题中,我们需要实现线性表的基本操作,如插入、删除、查找等。
通过编写代码,我们可以更好地理解这些操作的实现原理和思路。
2. 栈和队列栈和队列是线性表的特殊形式,它们分别具有“先进后出”和“先进先出”的特点。
在习题中,我们需要实现栈和队列的基本操作,如入栈、出栈、入队、出队等。
通过编写代码,我们可以更好地理解这些操作的实现方法和应用场景。
3. 树和二叉树树是一种非线性的数据结构,它具有层次关系。
二叉树是树的一种特殊形式,它每个节点最多只有两个子节点。
在习题中,我们需要实现树和二叉树的基本操作,如创建、插入、删除、遍历等。
通过编写代码,我们可以更好地理解这些操作的实现原理和应用场景。
4. 图图是一种非线性的数据结构,它由节点和边组成。
在习题中,我们需要实现图的基本操作,如创建、插入、删除、遍历等。
通过编写代码,我们可以更好地理解这些操作的实现方法和应用场景。
5. 查找和排序查找和排序是数据结构中非常重要的一部分,它们在实际应用中具有广泛的应用。
在习题中,我们需要实现各种查找和排序算法,如顺序查找、二分查找、冒泡排序、快速排序等。
通过编写代码,我们可以更好地理解这些算法的实现原理和性能特点。
通过以上习题的解答,我们可以更好地理解和掌握《数据结构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、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
●实验内容:1.编写函数,采用链式存储实现栈的初始化、入栈、出栈操作。
2.编写函数,采用顺序存储实现栈的初始化、入栈、出栈操作。
3.编写函数,采用链式存储实现队列的初始化、入队、出队操作。
4.编写函数,采用顺序存储实现队列的初始化、入队、出队操作。
5.编写一个主函数,在主函数中设计一个简单的菜单,分别调试上述算法。
●实验目的及要求:1.掌握栈、队列的思想及其存储实现2.掌握栈、队列的常见算法的程序实现●实验结果:#include <stdio.h>#include <malloc.h>#define MAXSIZE 80 typedef struct{int data[80];int top;}SeqStack;typedef struct lnode {int data;struct lnode *next; }StackNode,*LinkStack;{int data[80];int front,rear;int num;}C_SeQueue;typedef struct node{int data;struct node *next;}QNode;typedef struct{QNode *front,*rear;}LQueue;void menu(){printf("\n");printf("\t* ☆.栈的链式存储| ☆.栈的顺序存储*\n");printf("\t* 1.初始化| 5.初始化*\n");printf("\t* 2.判空| 6.判栈空*\n");printf("\t* 3.入栈| 7.入栈*\n");printf("\t* 4.出栈| 8.出栈*\n");printf("\t* ======================================== *\n"); printf("\t* ☆.队列的链式存储| ☆.队列的顺序存储*\n");printf("\t* 9.初始化| 13.建有头结点队*\n");printf("\t* 10.判空| 14.判空*\n");printf("\t* 11.入队| 15.入队*\n");printf("\t* 12.出队| 16.出队*\n");printf("\t* 0,退出| *\n"); printf("\t请选择您所需操作的序号:");}LinkStack Init_LinkStack(){StackNode *L;L=(StackNode*)malloc(sizeof(StackNode));L->data=100;L->next=NULL;return L;}int Empty_LinkStack(LinkStack top){if(top->next==NULL) return 1;else return 0;}LinkStack Push_LinkStack(LinkStack top,int x){StackNode *s;s=(StackNode*)malloc(sizeof(StackNode));s->data=x;s->next=top->next;top->next=s;return top;LinkStack Pop_LinkStack(LinkStack top,int *s) {StackNode *p;int j=0;p=top->next;if(p!=NULL){*s=p->data;top->next=p->next;free(p);}return top;}SeqStack * init_SeqStack(){SeqStack *S;S=(SeqStack*)malloc(sizeof(SeqStack));S->top=-1;return S;}int Empty_SeqStack(SeqStack *S){if(S->top==-1) return 1;else return 0;}int Push_SeqStack(SeqStack *S,int x){if(S->top==80-1){return(0);}else{S->top++;S->data[S->top]=x;return(1);}}int Pop_SeqStack(SeqStack *S,int *p){if(Empty_SeqStack(S)==1){ return 0;}else{*p=S->data[S->top];S->top--;return 1;}}C_SeQueue *init_SeQueue(){C_SeQueue *q;q=(C_SeQueue*)malloc(sizeof(C_SeQueue));q->front=q->rear=80-1;return q;}int In_SeQueue(C_SeQueue *q,int x) {if(q->num==80){ return(-1);}else{q->rear=(q->rear+1)%80;q->data[q->rear]=x;q->num++;return(1);}}int Out_SeQueue(C_SeQueue *q,int *p) {if(q->num==0){return -1;}else{q->front=(q->front+1)%80;*p=q->data[q->front];q->num--;return 1;}}int Empty_SeQueue(C_SeQueue *q) {if(q->num==0) return 1;else return 0;}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;}void In_LQueue(LQueue *q,int x){QNode *p;p=(QNode*)malloc(sizeof(QNode));p->next=NULL;q->rear->next=p;q->rear=p;}int Empty_LQueue(LQueue *q){if(q->front==q->rear) return 1;else return 0;}int Out_LQueue(LQueue *q,int *s){QNode *p;if(Empty_LQueue(q)==1){return 0;}else{p=q->front->next;q->front->next=p->next;*s=p->data;free(p);if(q->front->next==NULL)q->rear=q->front;return 1;}}void main(){int n,m=1;LinkStack L=NULL;SeqStack *S;C_SeQueue *Q;LQueue *LQ=NULL;while(m){menu();scanf("%d",&n);switch(n){case 1: L=Init_LinkStack();break;case 2:{StackNode *p=L->next;int flag;flag=Empty_LinkStack(L);if(flag==0){{printf("%5d",p->data);p=p->next;}}elseprintf("\t栈空!\n");break;}case 3:{LinkStack p;int x;printf("\t请输入一个数:");scanf("%d",&x);L=Push_LinkStack(L,x);p=L->next;printf("\t您输入的一组数是:");while(p){printf("%5d",p->data);p=p->next;}break;}case 4:{int x,*s;LinkStack p;s=&x;L=Pop_LinkStack(L,s);p=L->next;printf("\t您刚才输入的数是:");while(p){printf("%5d",p->data);p=p->next;}printf("%5d\n",x);break;}case 5:S=init_SeqStack();break;case 6:{int i,success;success=Empty_SeqStack(S);if(success!=1){for(i=0;i<=S->top;i++){printf("%5d",S->data[i]);}elseprintf("\t栈空!\n");break;}case 7:{int i,success;int x;printf("\t请输入一个数:");scanf("%d",&x);success=Push_SeqStack(S,x);printf("\t您输入的数是:");if(success==1){for(i=0;i<=S->top;i++){printf("%5d",S->data[i]);}}elseprintf("\t栈满!\n");break;}case 8:{int i,success;int x,*p;p=&x;success=Pop_SeqStack(S,p);printf("\t您刚才输入的数是:");if(success==1){for(i=0;i<=S->top;i++){printf("%5d",S->data[i]);}printf("%5d\n",x);}elseprintf("\t栈空!\n");break;}case 9:Q=init_SeQueue();break; case 10:{int i,flag,number;flag=Empty_SeQueue(Q);number=Q->num;if(flag!=1){printf("\t请输入一个数:%5d",Q->data[i]);}}elseprintf("\t队空!\n");break;}case 11:{int i,flag,number;int x;printf("\t请输入一个数:");scanf("%d",&x);flag=In_SeQueue(Q,x);number=Q->num;printf("\t您输入的一组数是:");if(flag==1){for(i=(Q->front+1)%80;number>0;number--,i=(i+1)%80){printf("%5d",Q->data[i]);}}elseprintf("\t队满!\n");break;}case 12:{int i,flag,number;int x,*p;p=&x;flag=Out_SeQueue(Q,p);number=Q->num;printf("\t您刚才输入的一组数是:");if(flag==1){for(i=(Q->front+1)%80;number>0;number--,i=(i+1)%80){printf("%5d",Q->data[i]);}printf("%5d\n",x);}elseprintf("\t队空!\n");break;}case 13: LQ=Init_LQueue();break;QNode *p;flag=Empty_LQueue(LQ);p=LQ->front->next;if(flag!=1){while(p!=NULL){printf("\t请输入一个数:%5d",p->data);p=p->next;}}elseprintf("\t队空!\n");break;}case 15:{int x;QNode *p;printf("\t请输入一个数:");scanf("%d",&x);In_LQueue(LQ,x);p=LQ->front->next;printf("\t您输入的数是:");while(p!=NULL){printf("%5d",p->data);p=p->next;}break;}case 16:{int flag;int x,*s;QNode *p;s=&x;flag=Out_LQueue(LQ,s);p=LQ->front->next;if(flag==1){printf("\t您刚才输入的数是:%5d",x);while(p!=NULL){printf("%5d",p->data);p=p->next;}}elseprintf("\t队空!\n");}case 0:m=0;}}}。