C语言堆栈和队列函数大全
- 格式:pdf
- 大小:115.18 KB
- 文档页数:7
c语言队列函数
C语言中,队列(Queue)是一种常用的数据结构,它遵循“先
进先出”(First In First Out,FIFO)的原则。
利用队列可以轻松
实现多种算法,如广度优先搜索、线程池等。
在C语言中,我们可以通过数组或链表来实现队列。
以下是几个常用的队列函数:
1. void initQueue(Queue *q)
这个函数用于初始化一个队列。
它接受一个指向Queue结构体的指针作为参数,将队首指针和队尾指针都初始化为0。
2. int isEmpty(Queue *q)
这个函数用于判断一个队列是否为空。
它接受一个指向Queue结构体的指针作为参数,如果队首指针等于队尾指针,则返回1,否则返回0。
3. int isFull(Queue *q, int max_size)
这个函数用于判断一个队列是否已满。
它接受一个指向Queue结构体的指针和队列的最大容量作为参数,如果队尾指针等于最大容量,则返回1,否则返回0。
4. int enqueue(Queue *q, int data)
这个函数用于向队列尾部添加元素。
它接受一个指向Queue结构体的指针和要添加的数据作为参数,如果队列已满,则返回0,否则将数据添加到队列尾部,并返回1。
5. int dequeue(Queue *q)
这个函数用于从队列头部删除元素。
它接受一个指向Queue结构体的指针作为参数,如果队列为空,则返回0,否则将队首元素删除,并返回该元素的值。
以上是几个常用的C语言队列函数,它们可以帮助我们轻松地实现队列数据结构。
queue函数队列(Queue)是计算机科学中常用的一种数据结构。
它是一种“先进先出”(FIFO)的存储结构,即第一个进入的元素将是最先移除的元素。
队列有多种形式,一种是普通的队列,另一种则是环形队列,也称循环队列。
Queue数是用来操作队列的一组函数。
它们的功能包括把元素加入队列、移出队列、查看队列的头部元素、查看队列的尾部元素、查看队列的长度等功能。
这些函数可以用来实现高效率的队列操作,从而较容易地实现一些常见的数据结构操作,如排序、查找、插入等。
常用的queue函数有:enQueue、deQueue、front、rear、length、isEmpty等。
其中,enQueue数用来向队列中添加元素;deQueue数用来从队列中移出元素;front数和 rear数分别用来返回队列中头部和尾部的元素;length数用来查看当前队列元素的数量;isEmpty数用来检查队列是否为空。
Queue数可以使用多种不同的实现方式。
C语言中常用的实现方案有数组实现法和链表实现法。
数组实现法是最简单的 queue数实现方式,它使用一个定长的数组 goal[maxSize]存储元素,并用两个变量 front rear表示目前数组的头部和尾部的位置。
当元素被入队时, rear指向下一个元素的存储位置;当元素被移出队列时,front指向元素的存储位置。
链表实现法的 queue数也非常容易理解。
它使用一个包含头结点和尾结点的双向循环链表来存储元素,使用两个指针指示头部位置以及尾部位置,以便快速找到队列中特定位置的元素。
入队操作主要是向尾结点插入新的结点;出队操作主要是从头结点删除结点。
队列函数不仅在计算机科学中应用广泛,而且在实际应用中也有着广泛的应用。
它可以用来解决一些常见的问题,如缓冲处理、多任务调度和算法测试等问题。
比如,在多任务调度中,可以使用队列来维护任务列表,每次通过出队操作找出最近要完成的任务;在算法测试中,可以使用队列来实现BFS(广度优先搜索)算法和DFS(深度优先搜索)算法。
栈和队列是信息学竞赛中经常涉及的数据结构,它们在算法和程序设计中有着广泛的应用。
掌握栈和队列的基本原理和操作方法,对于参加信息学竞赛的同学来说是非常重要的。
本文将深入探讨栈和队列的相关知识点,帮助大家更好地理解和掌握这两种数据结构。
一、栈的定义与特点栈是一种先进后出(LIFO)的数据结构,它的特点是只允许在栈顶进行插入和删除操作。
栈可以用数组或链表来实现,常见的操作包括压栈(push)、出栈(pop)、获取栈顶元素(top)等。
栈的应用非常广泛,比如在计算机程序中,函数的调用和返回值的存储就是通过栈来实现的。
二、栈的基本操作1. 压栈(push):将元素压入栈顶2. 出栈(pop):将栈顶元素弹出3. 获取栈顶元素(top):返回栈顶元素的值,但不把它从栈中移除4. 判空:判断栈是否为空5. 获取栈的大小:返回栈中元素的个数三、栈的应用1. 括号匹配:利用栈来检查表达式中的括号是否匹配2. 表达式求值:利用栈来实现中缀表达式转换为后缀表达式,并进行求值3. 迷宫求解:利用栈来实现迷宫的路径搜索4. 回溯算法:在深度优先搜索和递归算法中,通常会用到栈来保存状态信息四、队列的定义与特点队列是一种先进先出(FIFO)的数据结构,它的特点是只允许在队尾进行插入操作,在队首进行删除操作。
队列同样可以用数组或链表来实现,常见的操作包括入队(enqueue)、出队(dequeue)、获取队首元素(front)、获取队尾元素(rear)等。
队列在计算机领域也有着广泛的应用,比如线程池、消息队列等都可以用队列来实现。
五、队列的基本操作1. 入队(enqueue):将元素插入到队列的末尾2. 出队(dequeue):从队列的头部删除一个元素3. 获取队首元素(front):返回队列的头部元素的值4. 获取队尾元素(rear):返回队列的尾部元素的值5. 判空:判断队列是否为空6. 获取队列的大小:返回队列中元素的个数六、队列的应用1. 广度优先搜索算法(BFS):在图的搜索中,通常会用队列来实现BFS算法2. 线程池:利用队列来实现任务的调度3. 消息队列:在分布式系统中,常常会用队列来进行消息的传递4. 最近最少使用(LRU)缓存算法:利用队列实现LRU缓存淘汰在信息学竞赛中,栈和队列的相关题目经常出现,并且有一定的难度。
C语言常用函数包括:1.printf函数:用于在控制台输出数据。
2.scanf函数:用于从控制台读取用户输入的数据。
3.strlen函数:用于计算字符串的长度。
4.strcmp函数:用于比较两个字符串的大小。
5.strcpy函数:用于将一个字符串复制到另一个字符串中。
6.strcat函数:用于将一个字符串连接到另一个字符串的末尾。
7.strchr函数:用于查找字符串中指定字符的位置。
8.strstr函数:用于查找字符串中指定子串的位置。
9.atoi函数:用于将字符串转换为整数。
10.atof函数:用于将字符串转换为浮点数。
11.malloc函数:用于动态分配内存空间。
12.free函数:用于释放动态分配的内存空间。
13.memcpy函数:用于将一段内存区域的数据复制到另一段内存区域。
14.memset函数:用于将一段内存区域的数据设置为指定的值。
15.abs函数:用于计算整数的绝对值。
16.rand函数:用于生成随机数。
17.srand函数:用于设置随机数生成器的种子。
18.time函数:用于获取当前的系统时间。
19.localtime函数:用于将时间戳转换为本地时间。
20.strtol函数:用于将字符串转换为长整型数。
21.strtod函数:用于将字符串转换为双精度浮点数。
22.fprintf函数:用于将数据格式化输出到文件中。
23.fscanf函数:用于从文件中读取格式化的数据。
24.fgets函数:用于从文件中读取一行数据。
25.fputs函数:用于将数据写入文件中。
26.fopen函数:用于打开文件。
27.fclose函数:用于关闭文件。
28.feof函数:用于判断文件是否已经到达文件末尾。
29.ferror函数:用于判断文件操作是否发生错误。
30.fprintf函数:用于将数据格式化输出到文件中。
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,。
用堆栈实现四则运算c语言堆栈是一种常见的数据结构,它符合先进后出的原则。
在四则运算中,我们可以借助堆栈这种数据结构实现运算,方便高效,不易出错。
堆栈的实现包括两个基本操作:Push(入栈)和Pop(出栈)。
我们可以以此设计四则运算。
首先,我们需要将输入的四则运算表达式转换成后缀表达式。
后缀表达式也叫逆波兰表达式,相对于中缀表达式而言,运算符在后面,操作数在前面,这样方便计算机进行读取和计算。
例如:中缀表达式:5+3*2后缀表达式:5 3 2 * +将中缀表达式转换成后缀表达式,我们需要用到堆栈。
具体的实现方法是,从左向右遍历表达式,如果是数字,则直接输出;如果是符号,则将其与堆栈顶的符号进行比较,如果优先级高就入栈,否则不断将符号出栈并输出,直到当前符号优先级大于堆栈顶符号优先级,最后将当前符号入栈。
例如:表达式:5+3*2堆栈操作:1.将5输出,堆栈为空2.遇到+号,入栈3.将3输出,堆栈顶为+号4.遇到*号,入栈5.将2输出,堆栈顶为*号6.输出*号,堆栈顶为+号7.输出+号,堆栈为空得到后缀表达式:5 3 2 * +有了后缀表达式,我们可以用堆栈进行计算。
具体方法是,从左向右遍历后缀表达式,如果是数字则入栈,如果是符号则将栈顶两个数字出栈并进行计算,将结果入栈,最终得到最终的计算结果。
例如:后缀表达式:5 3 2 * +堆栈操作:1.将5入栈2.将3入栈3.遇到*号,出栈3和2,进行计算得到6,将6入栈4.将栈顶元素5出栈5.遇到+号,出栈6和5,进行计算得到11,将11入栈得到计算结果:11通过堆栈实现四则运算,可以有效简化我们的计算流程,避免复杂的优先级判断和计算错误。
同时,堆栈为我们提供了一种更加高效的数据结构,不仅在四则运算中可以发挥作用,在其他应用中也很常见。
当然,在实际应用中,我们需要考虑到多种情况的处理,例如负数、小数、括号等,以及错误处理等细节问题,才能保证算法的正确性和可靠性。
功能: 异常终止一个进程用法: void abort(void)函数名: abs功能: 求整数的绝对值用法: int abs(int i)函数名: absread, abswirte功能: 绝对磁盘扇区读、写数据用法: int absread(int drive, int nsects, int sectno, void *buffer) int abswrite(int drive, int nsects, in tsectno, void *buffer函数名: access功能: 确定文件的访问权限用法: int access(const char *filename, int amode)函数名: acos功能:反余弦函数用法: double acos(double x)函数名: allocmem功能: 分配DOS存储段用法:int allocmem(unsigned size, unsigned *seg)函数名: arc功能: 画一弧线用法:void far arc(int x, int y, int stangle, int endangle, int radius)函数名: asctime功能: 转换日期和时间为ASCII码用法:char *asctime(const struct tm *tblock)函数名: asin功能:反正弦函数用法: double asin(double x)函数名: assert功能: 测试一个条件并可能使程序终止用法:void assert(int test)函数名: atan功能: 反正切函数用法: double atan(double x)功能: 计算Y/X的反正切值用法: double atan2(double y, double x)函数名:atexit功能: 注册终止函数用法: int atexit(atexit_t func)函数名: atof功能: 把字符串转换成浮点数用法:double atof(const char *nptr)函数名: atoi功能: 把字符串转换成长整型数用法: int atoi(const char *nptr)函数名: atol功能: 把字符串转换成长整型数用法: long atol(const char *nptr)函数名: bar功能: 画一个二维条形图用法: void far bar(int left, int top, int right, int bottom)函数名: bar3d功能: 画一个三维条形图用法:void far bar3d(int left, int top, int right, int bottom,int depth, int topflag)函数名: bdos功能: DOS系统调用用法: int bdos(int dosfun, unsigned dosdx, unsigned dosal)函数名:bdosptr功能:DOS系统调用用法: int bdosptr(int dosfun, void *argument, unsigned dosal)函数名:bioscom功能: 串行I/O通信用法:int bioscom(int cmd, char abyte, int port)函数名:biosdisk功能: 软硬盘I/O用法:int biosdisk(int cmd, int drive, int head, int track, int sectorint nsects, void *buffer)函数名:biosequip功能: 检查设备用法:int biosequip(void)函数名:bioskey功能: 直接使用BIOS服务的键盘接口用法:int bioskey(int cmd)函数名:biosmemory功能: 返回存储块大小用法:int biosmemory(void)函数名:biosprint功能: 直接使用BIOS服务的打印机I/O用法:int biosprint(int cmd, int byte, int port)函数名:biostime功能: 读取或设置BIOS时间用法: long biostime(int cmd, long newtime)函数名: brk功能: 改变数据段空间分配用法:int brk(void *endds)函数名:bsearch功能: 二分法搜索用法:void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *))函数名: cabs功能: 计算复数的绝对值用法: double cabs(struct complex z);函数名:calloc功能:分配主存储器用法:void *calloc(size_t nelem, size_t elsize);函数名: ceil功能: 向上舍入用法: double ceil(double x);函数名: cgets功能: 从控制台读字符串用法: char *cgets(char *str)函数名:chdir功能: 改变工作目录用法: int chdir(const char *path);函数名:_chmod, chmod功能: 改变文件的访问方式用法: int chmod(const char *filename, int permiss);函数名:chsize功能: 改变文件大小用法: int chsize(int handle, long size);函数名: circle功能: 在给定半径以(x, y)为圆心画圆用法: void far circle(int x, int y, int radius);函数名: cleardevice功能: 清除图形屏幕用法: void far cleardevice(void);函数名:clearerr功能: 复位错误标志用法:void clearerr(FILE *stream);函数名: clearviewport功能: 清除图形视区用法: void far clearviewport(void);函数名:_close, close功能: 关闭文件句柄用法:int close(int handle);函数名: clock功能:确定处理器时间用法: clock_t clock(void);函数名:closegraph功能: 关闭图形系统用法: void far closegraph(void);函数名:clreol功能: 在文本窗口中清除字符到行末用法:void clreol(void)函数名:clrscr功能: 清除文本模式窗口用法:void clrscr(void);函数名: coreleft功能: 返回未使用内存的大小用法:unsigned coreleft(void);函数名: cos功能: 余弦函数用法:double cos(double x);函数名:cosh功能: 双曲余弦函数用法: dluble cosh(double x);函数名: country功能: 返回与国家有关的信息用法: struct COUNTRY *country(int countrycode, struct country *country); 函数名: cprintf功能: 送格式化输出至屏幕用法:int cprintf(const char *format[, argument, ...]);函数名: cputs功能: 写字符到屏幕用法: void cputs(const char *string);函数名: _creat creat功能: 创建一个新文件或重写一个已存在的文件用法: int creat (const char *filename, int permiss)函数名:creatnew功能: 创建一个新文件用法:int creatnew(const char *filename, int attrib);函数名: cscanf功能: 从控制台执行格式化输入用法:int cscanf(char *format[,argument, ...]);函数名: ctime功能: 把日期和时间转换为字符串用法:char *ctime(const time_t *time);功能: 设置Ctrl-Break处理程序用法: void ctrlbrk(*fptr)(void);函数名: delay功能: 将程序的执行暂停一段时间(毫秒)用法: void delay(unsigned milliseconds);函数名: delline功能: 在文本窗口中删去一行用法: void delline(void);函数名:detectgraph功能: 通过检测硬件确定图形驱动程序和模式用法: void far detectgraph(int far *graphdriver, int far *graphmode); 函数名: difftime功能: 计算两个时刻之间的时间差用法: double difftime(time_t time2, time_t time1);函数名: disable功能: 屏蔽中断用法:void disable(void);函数名: div功能: 将两个整数相除, 返回商和余数用法:div_t (int number, int denom);函数名: dosexterr功能: 获取扩展DOS错误信息用法:int dosexterr(struct DOSERR *dblkp);函数名: dostounix功能: 转换日期和时间为UNIX时间格式用法: long dostounix(struct date *dateptr, struct time *timeptr);函数名: drawpoly功能: 画多边形用法: void far drawpoly(int numpoints, int far *polypoints);函数名:dup功能: 复制一个文件句柄用法: int dup(int handle);函数名:dup2功能: 复制文件句柄用法: int dup2(int oldhandle, int newhandle);功能: 把一个浮点数转换为字符串用法: char ecvt(double value, int ndigit, int *decpt, int *sign);函数名: ellipse功能: 画一椭圆用法:void far ellipse(int x, int y, int stangle, int endangle,int xradius, int yradius);函数名: enable功能: 开放硬件中断用法: void enable(void);函数名: eof功能: 检测文件结束用法: int eof(int *handle);函数名: exec...功能: 装入并运行其它程序的函数用法: int execl(char *pathname, char *arg0, arg1, ..., argn, NULL); int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,char *envp[]);int execlp(char *pathname, char *arg0, arg1, .., NULL);int execple(char *pathname, char *arg0, arg1, ..., NULL,char *envp[]);int execv(char *pathname, char *argv[]);int execve(char *pathname, char *argv[], char *envp[]);int execvp(char *pathname, char *argv[]);int execvpe(char *pathname, char *argv[], char *envp[]);函数名:exit功能: 终止程序用法: void exit(int status);函数名: exp功能: 指数函数用法: double exp(double x);函数名: gcvt功能: 把浮点数转换成字符串用法: char *gcvt(double value, int ndigit, char *buf);函数名: geninterrupt功能: 产生一个软中断函数名: getarccoords功能: 取得最后一次调用arc的坐标用法: void far getarccoords(struct arccoordstype far *arccoords); 函数名: getaspectratio功能: 返回当前图形模式的纵横比用法: void far getaspectratio(int far *xasp, int far *yasp);函数名: getbkcolor功能: 返回当前背景颜色用法: int far getbkcolor(void);函数名: getc功能: 从流中取字符用法: int getc(FILE *stream);函数名: getcbrk功能: 获取Control_break设置用法: int getcbrk(void);函数名: getch功能: 从控制台无回显地取一个字符用法: int getch(void);函数名: getchar功能: 从stdin流中读字符用法: int getchar(void);函数名: getche功能: 从控制台取字符(带回显)用法: int getche(void);函数名: getcolor功能: 返回当前画线颜色用法: int far getcolor(void);函数名: getcurdir功能: 取指定驱动器的当前目录用法: int getcurdir(int drive, char *direc);函数名: getcwd功能: 取当前工作目录用法: char *getcwd(char *buf, int n);函数名: getdate功能: 取DOS日期函数名: getdefaultpalette功能: 返回调色板定义结构用法: struct palettetype *far getdefaultpalette(void);函数名: getdisk功能: 取当前磁盘驱动器号用法: int getdisk(void);函数名: getdrivername功能: 返回指向包含当前图形驱动程序名字的字符串指针用法: char *getdrivename(void);函数名: getdta功能: 取磁盘传输地址用法: char far *getdta(void);函数名: getenv功能: 从环境中取字符串用法: char *getenv(char *envvar);函数名: getfat, getfatd功能: 取文件分配表信息用法: void getfat(int drive, struct fatinfo *fatblkp);函数名: getfillpattern功能: 将用户定义的填充模式拷贝到内存中用法: void far getfillpattern(char far *upattern);函数名: getfillsettings功能: 取得有关当前填充模式和填充颜色的信息用法: void far getfillsettings(struct fillsettingstype far *fillinfo); 函数名: getftime功能: 取文件日期和时间用法: int getftime(int handle, struct ftime *ftimep);函数名: getgraphmode功能: 返回当前图形模式用法: int far getgraphmode(void);函数名: getftime功能: 取文件日期和时间用法: int getftime(int handle, struct ftime *ftimep);函数名: getgraphmode功能: 返回当前图形模式用法: int far getgraphmode(void);函数名: getimage功能: 将指定区域的一个位图存到主存中用法: void far getimage(int left, int top, int right, int bottom,void far *bitmap);函数名: getlinesettings功能: 取当前线型、模式和宽度用法: void far getlinesettings(struct linesettingstype far *lininfo): 函数名: getmaxx功能: 返回屏幕的最大x坐标用法: int far getmaxx(void);函数名: getmaxy功能: 返回屏幕的最大y坐标用法: int far getmaxy(void);函数名: getmodename功能: 返回含有指定图形模式名的字符串指针用法: char *far getmodename(int mode_name);函数名: getmoderange功能: 取给定图形驱动程序的模式范围用法: void far getmoderange(int graphdriver, int far *lomode,int far *himode);函数名: getpalette功能: 返回有关当前调色板的信息用法: void far getpalette(struct palettetype far *palette);函数名: getpass功能: 读一个口令用法: char *getpass(char *prompt);函数名: getpixel功能: 取得指定像素的颜色用法: int far getpixel(int x, int y);函数名: gets功能: 从流中取一字符串用法: char *gets(char *string);函数名: gettext功能: 将文本方式屏幕上的文本拷贝到存储区用法: int gettext(int left, int top, int right, int bottom, void *destin);函数名: gettextinfo功能: 取得文本模式的显示信息用法: void gettextinfo(struct text_info *inforec);函数名: gettextsettings功能: 返回有关当前图形文本字体的信息用法: void far gettextsettings(struct textsettingstype far *textinfo); 函数名: gettime功能: 取得系统时间用法: void gettime(struct time *timep);函数名: getvect功能: 取得中断向量入口用法: void interrupt(*getvect(int intr_num));函数名: getverify功能: 返回DOS校验标志状态用法: int getverify(void);函数名: getviewsetting功能: 返回有关当前视区的信息用法: void far getviewsettings(struct viewporttype far *viewport); 函数名: getw功能: 从流中取一整数用法: int getw(FILE *strem);函数名: getx功能: 返回当前图形位置的x坐标用法: int far getx(void);函数名: gety功能: 返回当前图形位置的y坐标用法: int far gety(void);函数名: gmtime功能: 把日期和时间转换为格林尼治标准时间(GMT)用法: struct tm *gmtime(long *clock);函数名: gotoxy功能: 在文本窗口中设置光标用法: void gotoxy(int x, int y);函数名: gotoxy功能: 在文本窗口中设置光标用法: void gotoxy(int x, int y);函数名: graphdefaults功能: 将所有图形设置复位为它们的缺省值用法: void far graphdefaults(void);函数名: grapherrormsg功能: 返回一个错误信息串的指针用法: char *far grapherrormsg(int errorcode);函数名: graphresult功能: 返回最后一次不成功的图形操作的错误代码用法: int far graphresult(void);函数名: _graphfreemem功能: 用户可修改的图形存储区释放函数用法: void far _graphfreemem(void far *ptr, unsigned size);函数名: _graphgetmem功能: 用户可修改的图形存储区分配函数用法: void far *far _graphgetmem(unsigned size);函数名: harderr功能: 建立一个硬件错误处理程序用法: void harderr(int (*fptr)());函数名: hardresume功能: 硬件错误处理函数用法: void hardresume(int rescode);函数名: highvideo功能: 选择高亮度文本字符用法: void highvideo(void);函数名: hypot功能: 计算直角三角形的斜边长用法: double hypot(double x, double y);函数名: imagesize功能: 返回保存位图像所需的字节数用法: unsigned far imagesize(int left, int top, int right, int bottom); 函数名: initgraph功能: 初始化图形系统用法: void far initgraph(int far *graphdriver, int far *graphmode函数名: inport功能: 从硬件端口中输入用法: int inp(int protid);函数名: insline功能: 在文本窗口中插入一个空行用法: void insline(void);函数名: installuserdriver功能: 安装设备驱动程序到BGI设备驱动程序表中用法: int far installuserdriver(char far *name, int (*detect)(void));函数名: installuserfont功能: 安装未嵌入BGI系统的字体文件(CHR)用法: int far installuserfont(char far *name);函数名: int86功能: 通用8086软中断接口用法: int int86(int intr_num, union REGS *inregs, union REGS *outregs) 函数名: int86x功能: 通用8086软中断接口用法: int int86x(int intr_num, union REGS *insegs, union REGS *outregs, 函数名: intdos功能: 通用DOS接口用法: int intdos(union REGS *inregs, union REGS *outregs);函数名: intdosx功能: 通用DOS中断接口用法: int intdosx(union REGS *inregs, union REGS *outregs,struct SREGS *segregs);函数名: intr功能: 改变软中断接口用法: void intr(int intr_num, struct REGPACK *preg);函数名: ioctl功能: 控制I/O设备用法: int ioctl(int handle, int cmd[,int *argdx, int argcx]);函数名: isatty功能: 检查设备类型用法: int isatty(int handle);函数名: itoa功能: 把一整数转换为字符串用法: char *itoa(int value, char *string, int radix);函数名: kbhit功能: 检查当前按下的键用法: int kbhit(void);函数名: keep功能: 退出并继续驻留用法: void keep(int status, int size);函数名: kbhit功能: 检查当前按下的键用法: int kbhit(void);函数名: keep功能: 退出并继续驻留用法: void keep(int status, int size);函数名: labs用法: long labs(long n);函数名: ldexp功能: 计算value*2的幂用法: double ldexp(double value, int exp);函数名: ldiv功能: 两个长整型数相除, 返回商和余数用法: ldiv_t ldiv(long lnumer, long ldenom);函数名: lfind功能: 执行线性搜索用法: void *lfind(void *key, void *base, int *nelem, int width,int (*fcmp)());函数名: line功能: 在指定两点间画一直线用法: void far line(int x0, int y0, int x1, int y1);函数名: linerel功能: 从当前位置点(CP)到与CP有一给定相对距离的点画一直线用法: void far linerel(int dx, int dy);函数名: localtime功能: 把日期和时间转变为结构用法: struct tm *localtime(long *clock);函数名: lock功能: 设置文件共享锁用法: int lock(int handle, long offset, long length);函数名: log功能: 对数函数ln(x)用法: double log(double x);函数名: log10功能: 对数函数log用法: double log10(double x);函数名: longjump功能: 执行非局部转移用法: void longjump(jmp_buf env, int val);函数名: lowvideo功能: 选择低亮度字符用法: void lowvideo(void);函数名: lrotl, _lrotl功能: 将无符号长整型数向左循环移位用法: unsigned long lrotl(unsigned long lvalue, int count);unsigned long _lrotl(unsigned long lvalue, int count);函数名: lsearch功能: 线性搜索用法: void *lsearch(const void *key, void *base, size_t *nelem,size_t width, int (*fcmp)(const void *, const void *));函数名: lseek功能: 移动文件读/写指针用法: long lseek(int handle, long offset, int fromwhere);main()主函数每一C 程序都必须有一main() 函数, 可以根据自己的爱好把它放在程序的某个地方。
c语言队列库函数C语言队列库函数队列是一种常见的数据结构,它具有先进先出(FIFO)的特点。
在C 语言中,我们可以使用队列库函数来实现队列的操作。
下面是一些常用的队列库函数:1. void *malloc(size_t size)该函数用于动态分配内存空间,返回值为指向分配内存的指针。
在队列中,我们需要动态分配内存来存储队列元素。
2. void free(void *ptr)该函数用于释放动态分配的内存空间,参数为指向要释放的内存的指针。
在队列中,当队列元素出队时,需要释放其占用的内存空间。
3. void *calloc(size_t nmemb, size_t size)该函数用于动态分配内存空间,并将其初始化为0,返回值为指向分配内存的指针。
在队列中,我们可以使用该函数来初始化队列。
4. void *realloc(void *ptr, size_t size)该函数用于重新分配内存空间,参数为指向要重新分配的内存的指针和新的内存大小。
在队列中,当队列元素入队时,如果队列已满,需要重新分配内存空间。
5. int printf(const char *format, ...)该函数用于输出格式化的字符串,参数为格式化字符串和可变参数列表。
在队列中,我们可以使用该函数来输出队列元素。
6. int scanf(const char *format, ...)该函数用于输入格式化的数据,参数为格式化字符串和可变参数列表。
在队列中,我们可以使用该函数来输入队列元素。
7. void *memcpy(void *dest, const void *src, size_t n)该函数用于将源内存区域的内容复制到目标内存区域,参数为目标内存指针、源内存指针和要复制的字节数。
在队列中,我们可以使用该函数来复制队列元素。
8. void *memmove(void *dest, const void *src, size_t n)该函数用于将源内存区域的内容移动到目标内存区域,参数为目标内存指针、源内存指针和要移动的字节数。
c语⾔stack(栈)和heap(堆)的使⽤详解⼀、预备知识—程序的内存分配⼀个由C/C++编译的程序占⽤的内存分为以下⼏个部分1、栈区(stack)—由编译器⾃动分配释放,存放函数的参数值,局部变量的值等。
其操作⽅式类似于数据结构中的栈。
2、堆区(heap)—⼀般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收。
注意它与数据结构中的堆是两回事,分配⽅式倒是类似于链表。
3、全局区(静态区)(static)—全局变量和静态变量的存储是放在⼀块的,初始化的全局变量和静态变量在⼀块区域,未初始化的全局变量和未初始化的静态变量在相邻的另⼀块区域。
程序结束后由系统释放。
4、⽂字常量区—常量字符串就是放在这⾥的。
程序结束后由系统释放。
5、程序代码区—存放函数体的⼆进制代码。
⼆、例⼦程序复制代码代码如下://main.cppint a=0; //全局初始化区char *p1; //全局未初始化区main(){intb;栈char s[]="abc"; //栈char *p2; //栈char *p3="123456"; //123456\0在常量区,p3在栈上。
static int c=0; //全局(静态)初始化区p1 = (char*)malloc(10);p2 = (char*)malloc(20); //分配得来得10和20字节的区域就在堆区。
strcpy(p1,"123456"); //123456\0放在常量区,编译器可能会将它与p3所向"123456"优化成⼀个地⽅。
}三、堆和栈的理论知识2.1申请⽅式stack:由系统⾃动分配。
例如,声明在函数中⼀个局部变量int b;系统⾃动在栈中为b开辟空间heap:需要程序员⾃⼰申请,并指明⼤⼩,在c中⽤malloc函数如p1=(char*)malloc(10);在C++中⽤new运算符如p2=(char*)malloc(10);但是注意p1、p2本⾝是在栈中的。
c语言的常用函数c语言是一门流行的多用途的编程语言,几乎在所有的领域都有应用。
在把它应用到实际项目中时,需要熟悉c语言常用函数。
本文介绍c语言中105个常用函数,包括数据类型转换、输入输出等函数,以及字符串处理、文件处理、学习内存管理、数学函数、定时器等函数,可以帮助程序员们更加熟悉c语言中的常用函数,从而更好地实现编程项目。
一、数据类型转换函数1、atoi()一个字符串转换成一个整数。
2、atof()一个字符串转换成浮点数。
3、atol()一个字符串转换成长整数。
4、strtod()一个字符串转换成双精度浮点数。
5、strtol()一个字符串转换成长整数。
6、strtoul()字符串转换成无符号长整数。
7、itoa()整数转换为字符串。
8、ftoa()浮点数转换为字符串。
9、ltoa()长整数转换为字符串。
二、输入输出函数1、printf()格式化的数据输出到标准输出设备。
2、scanf() 从标准输入设备读取格式化的数据。
3、fprintf()格式化的数据写入指定的文件。
4、fscanf() 从指定的文件读取格式化的数据。
5、sprintf()格式化的数据存储到字符串变量中。
6、sscanf() 从字符串变量中读取格式化的数据。
三、字符串处理函数1、strlen()算字符串长度。
2、strcpy() 从源字符串复制到目标字符串。
3、strcat()源字符串添加到目标字符串的末尾。
4、strcmp()较两个字符串。
5、strncpy() 从源字符串复制到目标字符串,长度不超过指定的长度。
6、strncat()源字符串添加到目标字符串的末尾,长度不超过指定的长度。
7、strncmp()较两个字符串,长度不超过指定的长度。
8、strstr()到第一个字符串中出现第二个字符串的位置。
9、strchr()到第一个字符串中出现字符c的位置。
10、strrchr()到第一个字符串中最后一次出现字符c的位置。
1、用循环链表作为存储结构的队列就就是循环队列()错误2、线性链表中各个链结点之间的地址不一定要连续。
()正确3、一个任意串就是其自身的子串()正确4、AOV网是个带权有向图()错误5、一组权值,可以唯一构造出一棵哈夫曼树。
()错误6、队列的插入操作是在队尾。
()正确7、邻接表是图的一种链式存储结构()正确8、由权值为3,6,7,2,5的叶子结点生成一棵哈夫曼树,它的带权路径长度为51()正确9、若某堆栈的输入序列为1,2.3.4,则4,3,1,2不可能就是堆栈的输出序列之一()正确10、在链队列中,即使不设置尾指针也能进行入队操作()错误11、哈夫曼树一定就是完全二叉树()错误12符号p->next出现在表达式中表示p所指的结点的内容()错误12、从源点到终点的最短路径是唯一的()错误13、队列在数据中的存储原则是后进先出()错误14、邻接表只能用于存储有向图,而邻接矩阵则可存储有向图和无向图()错误16.给定一组权值,可以唯一构造出一棵哈夫曼树()错误17、程序越短,程序运行的时间就越少()错误18、线性表的逻辑顺序与物理顺序总就是一致的()错误19、堆栈、队列与数组的逻辑结构都就是线性表结构()正确20、带头节点的单链表head为空的判定条件是head==NULL()错误1、循环队列的特点之一是不会产生()假溢出2、链栈与顺序栈相比,比较明显的优点是()不会出现上溢的情况3、若需要利用形参直接访问实参,则应把形参变量说明为()参数引用4、在一棵深度为h的具有n个元素的二叉排序树中,查找所有元素的最长查找长度为()h5、在单链表中,存储每个结点需要有两个域,一个是数据域,另一个是指针域,它指向该结点的()直接后继6、在查找过程中,若同时还要做增、删工作,这种查找则称为()动态查找7、假定一个顺序队列的队首与队尾指针分别为front与rear,存放该队列的数组长度为N,则判断队空的条件为()Front==rear8、适于对动态查找表进行高效率查找的组织结构就是()二叉排序树9.线性表是()一个有限序列,可以为空10.链表具有的特点是()可随机访问任一元素11、在一棵二叉树中,第5层上的结点数最多为()1612、静态查找表与动态查找表两者的根本差别在于()施加的操作不同13、下列陈述中正确的是()二叉树中最多只有两颗子树,并且有左右之分14、算法在发生非法操作时可以做出处理的特性称为()健壮性15、使用折半查找,线性表必须()以顺序方式存储,且元素已元素已按值排好序16、存储时,结点的地址()连续与否均可17、链表不具有的特点是()随机访问18、一个栈的入栈序列是a , b , c ,d , e,则栈的不可能的输出序列是()d,c,e,a,b19、二维数组A按行顺序存储.其中每个元素占1个存储单元。
附录A实验报告课程:数据结构(c语言)实验名称:栈和队列系别:数字媒体技术实验日期: 11月15号专业班级:组别:姓名:学号:实验报告内容验证性实验一、预习准备:实验目的:1. 掌握栈的顺序表示、链表表示以及相应操作的实现。
特别注意栈空和栈满的条件;2. 掌握队列的顺序表示、链表表示以及相应操作的实现。
特别是循环队列中队头与队尾指针的变化情况;实验环境:Widows操作系统、VC6.0实验原理:1.定义:栈:只允许在一端插入和删除的线性表,允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom)。
队列: 是只允许在一端删除,在另一端插入的顺序表,允许删除的一端叫做队头(front),允许插入的一端叫做队尾(rear)。
2.特点:栈:后进先出(LIFO)队列:先进先出(FIFO, First In First Out)93. 表示:栈:(1)栈的数组表示—顺序栈(2)栈的链接表示—链式栈队列:(1)队列的顺序存储结构表示——循环队列(2)队列的链式表示—链队列实验内容和要求:分别使用顺序循环队列和堆栈以及链式队列和堆栈编写程序:判断一个字符序列是否是回文。
回文是指一个字符序列以中间字符为基准,两边字符完全相同。
如:“ABCDEDCBA”。
字符串长度小于等于80,用于判断回文的字符串不包括字符串的结束标记符。
基本要求:(1)字符序列可由用户从键盘随意输入;(2)可以连续测试多个字符序列,由用户决定退出测试程序;算法思想:判断回文的算法思想是:把字符串中的字符逐个分别存入队列和堆栈中,然后逐个出队列和退栈并比较出队列的数据元素和退栈的数据元素是否相等,若全部相等则该字符序列为回文,否则就不是回文。
基本操作:回文判断操作主要包括入栈和入队列、退栈和出队列操作。
在对堆栈以及队列进行操作之前,必须对队列以及堆栈进行初始化。
若使用链式堆栈和链式队列,操作结束后必须销毁链表。
二、实验过程:程序流程图:队列实验中的关键语句:(1) 构造空顺序栈算法Status InitStack ( SqStack &S ) {S.base = ( SElemType * ) malloc ( ST ACK_INIT_SIZE * sizeof ( SElemType ) );if ( ! S.base ) exit ( OVERFLOW );S.stacksize = ST ACK_INIT_SIZE;return OK;} // InitStack(2) 顺序栈出栈算法Status Pop ( SqStack &S, SElemType &e ) {if ( S.top = = S.base ) return ERROR;e = *--S.top; return OK;} // Pop(3)(4) 将元素压入顺序栈算法Status Push ( SqStack &S, SElemType e ){if ( S.top - S.base >= S.stacksize ) { S.base = ( SElemType * ) realloc ( S.base, ( S.stacksixe + ST ACKINCREMENT* sizeof ( SElemType ) );if ( ! S.base ) exit ( OVERFLOW );S.top = S.base + S.stacksize;S.stacksize += ST ACKINCREMENT;}*S.top ++= e;return OK;} // Push(4)在顺序队列尾插入新元素算法Status EnQueue ( SqQueue &Q; QElemType e ) {if ( ( Q.rear + 1 ) % MAXQSIZE = = Q.front )return ERRORQ.base[ Q.rear ] = e;Q.rear = ( Q.rear + 1 ) % MAXQSIZE;return OK;} // EnQueue(5)在顺序队列头删除旧元素算法Status DeQueue ( SqQueue &Q, QElemType &e ) {if ( Q.front = = Q.rear ) return ERROR;e = Q.base [ Q.front ]; Q.front = ( Q.front + 1 ) % MAXQSIZE; return OK;} // DeQueue(6)在链式队列尾插入新元素算法Status EnQueue ( LinkQueue &Q; QElemType e ) {p = ( QueuePtr ) malloc ( sizeof ( QNode ) );if ( ! p ) exit ( OVERFLOW ); p->data = e;p->next = NULL;Q.rear->next = p;Q.rear = p;return OK;} // EnQueue(7)在链式队列头删除旧元素算法Status DeQueue ( LinkQueue &Q, QElemType &e ) {if ( Q.front = = Q.rear ) return ERROR;p = Q.front->next;e = p->data;Q.front->next = p->next;if ( Q.rear = = p ) Q.rear = Q.front;free ( p );return OK;} // DeQueue编写及调试程序中遇到的问题及解决方法:(1)没有注意到可以验证多次问题。