实验二_栈、队列地实现与应用

合集下载

实验二 栈和队列的基本操作实现及其应用

实验二   栈和队列的基本操作实现及其应用

实验二栈和队列的基本操作实现及其应用一、实验目的1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。

2、会用栈和队列解决简单的实际问题。

二、实验内容(可任选或全做)题目一、试写一个算法,判断依次读入的一个以@为结束符的字符序列,是否为回文。

所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。

相关常量及结构定义:# define STACK_INIT_SIZE 100# define STACKINCREMENT 10# define OK 1# define ERROR 0typedef int SElemType;//栈类型定义typedef struct SqStack{ SElemType *base;SElemType *top;int stacksize;}SqStack;设计相关函数声明:判断函数:int IsReverse()栈:int InitStack(SqStack &S )int Push(SqStack &S, SElemType e )int Pop(SqStack &S,SElemType &e)int StackEmpty(s)题目二、编程模拟队列的管理,主要包括:出队列、入队、统计队列的长度、查找队列某个元素e、及输出队列中元素。

[实现提示]:参考教材循环队列的有关算法,其中后两个算法参考顺序表的实现。

题目三、RailsDescriptionThere is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited thattime. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.InputThe input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0.The last block consists of just one line containing 0.OutputThe output contains the lines corresponding to the lines with permutations in the input.A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition,there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input. Sample Input51 2 3 4 55 4 1 2 366 5 4 3 2 1Sample OutputYesNoYes题目四、Sliding WindowDescriptionAn array of size n≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:The array is [1 3 -1 -3 5 3 6 7], and k is 3.Window position Minimum value Maximum value[1 3 -1] -3 5 3 6 7 -131 [3 -1 -3] 5 3 6 7 -331 3 [-1 -3 5] 3 6 7 -351 3 -1 [-3 5 3] 6 7 -351 3 -1 -3 [5 3 6] 7 361 3 -1 -3 5 [3 6 7]37Your task is to determine the maximum and minimum values in the sliding window at each position.InputThe input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.OutputThere are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.Sample Input8 31 3 -1 -3 5 3 6 7Sample Output-1 -3 -3 -3 3 33 3 5 5 6 7题目五(选作考查串知识)DNA Evolution【Description】Evolution is a seemingly random process which works in a way which resembles certain approaches we use to get approximate solutions to hard combinatorial problems. You are now to do something completely different.Given a DNA string S from the alphabet {A,C,G,T}, find the minimal number of copy operations needed to create another string T. You may reverse the strings you copy, and copy both from S and the pieces of your partial T. You may put these pieces together at any time. You may only copy contiguous parts of your partial T, and all copied strings must be used in your final T.Example: From S= “ACTG” create T= “GTACTAATAAT”1.Get GT......... by copying and reversing "TG" from S.2.Get GT AC... by copying "AC" from S.3.Get GTAC TA….. by copying "TA" from the partial T.4.Get GTACTA AT by copying and reversing "TA" from the partial T.5.Get GTACTAAT AAT by copying "AAT" from the partial T.【Input】The first line of input gives a single integer, 1 ≤k≤10, the number of test cases. Then follow, for each test case, a line with the string S , length of S is less then 19, and a line with the string T , length of T is less then 19.【Output】Output for each test case the number of copy operations needed to create T from S, or "impossible" if it cannot be done.【Sample Input】4ACGTTGCAACACGTTCGATCGAAAAAAAAAAAAAAAAAAAA【Sample output】1impossible46题目六(选作考查数组知识)Magic Squares描述Following the success of the magic cube, Mr. Rubik invented its planar version, called magic squares. This is a sheet composed of 8 equal-sized squares:1 2 3 48 7 6 5In this task we consider the version where each square has a different color. Colors are denoted by the first 8 positive integers. A sheet configuration is given by the sequence of colors obtained by reading the colors of the squares starting at the upper left corner and going in clockwise direction. For instance, the configuration of Figure 3 is given by the sequence (1,2,3,4,5,6,7,8). This configuration is the initial configuration.Three basic transformations, identified by the letters `A', `B' and `C', can be applied to a sheet:∙'A': exchange the top and bottom row,∙'B': single right circular shifting of the rectangle,∙'C': single clockwise rotation of the middle four squares.Below is a demonstration of applying the transformations to the initial squares given above:A:8 7 6 51 2 3 4B:4 1 2 35 8 7 6C:1 72 48 6 3 5All possible configurations are available using the three basic transformations.You are to write a program that computes a minimal sequence of basic transformations that transforms the initial configuration above to a specific target configuration.输入A single line with eight space-separated integers (a permutation of (1..8)) that are the target configuration.输出样例输入2 6 8 4 5 73 1样例输出7BCABCCB三、实验步骤㈠、数据结构与核心算法的设计描述㈡、函数调用及主函数设计(可用函数的调用关系图说明)㈢程序调试及运行结果分析㈣实验总结四、主要算法流程图及程序清单1、主要算法流程图:2、程序清单(程序过长,可附主要部分)//int IsReverse(){ ….while( (e=getchar())!='@'){e 依次入栈、入队 //push(S,e);EnQueue(Q,e);……..}While(!StackEmpty(S)) { pop(S,a);DeQueue(Q,b);If(a!=b) return 0;}return 1;}。

栈和队列的基本操作实现及其应用

栈和队列的基本操作实现及其应用

实验二栈和队列的基本操作实现及其应用一_一、实验目的1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。

一_二、实验内容题目一、试写一个算法,判断依次读入的一个以为结束符的字符序列,是否为回文。

所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。

相关常量及结构定义:#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef int SElemType;typedef struct SqStack{ SElemType *base;SElemType *top;int stacksize;}SqStack;设计相关函数声明:判断函数:int IsReverse()栈:int InitStack(SqStack &S )int Push(SqStack &S, SElemType e )int Pop(SqStack &S,SElemType &e)int StackEmpty(s)一_三、数据结构与核心算法的设计描述1、初始化栈/* 函数功能:对栈进行初始化。

参数:栈(SqStack S)。

成功初始化返回0,否则返回-1 */int InitStack(SqStack &S){S.base=(SElemType *)malloc(10*sizeof(SElemType));if(!S.base) //判断有无申请到空间return -1; //没有申请到内存,参数失败返回-1S.top=S.base;S.stacksize=STACK_INIT_SIZE;S.base=new SElemType;return 0;}2、判断栈是否是空/*函数功能:判断栈是否为空。

参数; 栈(SqStack S)。

栈为空时返回-1,不为空返回0*/int StackEmpty(SqStack S){if(S.top==S.base) return -1;else return 0;}3、入栈/*函数功能:向栈中插入元素。

数据结构实验二-栈和队列的基本操作与应用

数据结构实验二-栈和队列的基本操作与应用

实验报告课程名称_______数据结构实验__________________ 实验项目___ 栈和队列的基本操作与应用____ 实验仪器_____________________________________系别 ___ 计算机学院_______________ 专业 __________________班级/学号______ _________学生姓名_____________________ __实验日期__________________成绩_______________________指导教师____ __________________一、实验内容:本次实验主要内容是表达式求值,主要通过栈和队列来编写程序,需要实现整数运算其中需要实现的功能有加减乘除以及括号的运用,其中包含优先级的判断。

二、设计思想1.优先级中加减、乘除、小括号、以及其他可以分组讨论优先级2.优先级关系用“>”“<”“=”来表示三种关系3.为实现运算符优先使用两个栈:OPTR 运算符栈与OPND操作符栈4.运用入栈出栈优先级比较等方式完成运算三、主要算法框架1.建立两个栈InitStack(&OPTR);InitStack(&OPND);2.Push“#”到 OPTR3.判断优先级做入栈出栈操作If“<” Push(&OPTR, c);If“=” Pop(&OPTR, &x)If“>” Pop(&OPTR, &theta);Pop(&OPND, &b);Pop(&OPND, &a);Push(&OPND, Operate(a, theta, b));四、调试报告遇到的问题与解决1.C语言不支持取地址符,用*S代替&S来编写代码2.一开始没有计算多位数的功能只能计算一位数,在几个中间不含运算符的数字中间做p = p*10+c运算。

实验二 栈和队列的基本操作及其应用

实验二  栈和队列的基本操作及其应用

实验二栈和队列的基本操作及其应用一、实验目的1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应用。

2、掌握栈和队列的特点,即后进先出和先进先出的原则。

3、掌握栈和队列的基本运算,如:入栈与出栈,入队与出队等运算在顺序存储结构和链式存储结构上的实现。

二、实验内容本次实验提供2个题目,每个题目都标有难度系数,*越多难度越大,学生可以根据自己的情况任选一个!题目一:回文判断(*)[问题描述]对于一个从键盘输入的字符串,判断其是否为回文。

回文即正反序相同。

如“abba”是回文,而“abab”不是回文。

[基本要求](1)数据从键盘读入;(2)输出要判断的字符串;(3)利用栈的基本操作对给定的字符串判断其是否是回文,若是则输出“ok”,否则输出“fail”。

程序源代码如下:/**********************************用栈和队列进行回文判断输入字符以@结束***********************************/#include <stdio.h>/*定义一个栈*/typedef struct Stack{int size;char * Base;char * Top;}Stack;/*创建一个栈*/void CreateStack(Stack * S,int size) {S -> size = size;S -> Base = (char *)malloc(size);S -> Top = S -> Base;}/*推入一个元素*/void Push(Stack * S,char c){/*栈满了,不能插入了*/if(S -> Top - S -> Base == S -> size) {printf("Stack is full and can't push!"); return;}else{*(++S -> Top) = c;}}void Pop(Stack * S){/*栈空了*/if(S -> Top == S -> Base){printf("Stack is empty!");return;}else{S -> Top--;}}void main(){Stack S;int Begin;char c;CreateStack(&S,100);Begin = 0;while(1){scanf("%c",&c);if(c == '@')break;if(c == '&' && !Begin){Begin = 1;continue;}if(Begin){if(*(S.Top) == c){Pop(&S);}}elsePush(&S,c);}if(S.Top == S.Base){printf("ok\n");}else{printf("fail\n");}getch();}运行结果如下:图中的“ok”表示是回文,“fail”表现不是回文。

实验二栈队列的实现及应用

实验二栈队列的实现及应用

百度文库-让每个人平等地提升自我实验二栈、队列的实现及应用实验课程名:数据结构与算法专业班级:_ 学号:__________ 姓名: _实验时间: ____ 实验地点:指导教师:冯珊__________一、实验目的1掌握栈和队列的顺序存储结构和链式存储结构,以便在实际背景下灵活运用。

2、掌握栈和队列的特点,即先进后出与先进先出的原则。

3、掌握栈和队列的基本操作实现方法。

/*顺序栈的存储类型*/typedef struct12345远兀1一7U-元谴段囑:>o123R*元元栈書t出一^零遐次:±谨虚123^5I BD 认戯握结IVl 匚on&ol eAp pli cation!\[>ebu g\Con 5 o-leApp li cation1 .exe:1刖人操作谊睪代码(05):2:hEs选的操一兀一b一丁一丁栈?遐次嘆區123455^元元栈S退、灵岀祓SI■i9IIIi主至..T'一兀元栈£12345\Z百度文库-让每个人平等地提升自我P入操隹选择代码(0-5>:4派元素的是;栈化出取示艮i元一一选的操元->入中>c1-苴翻(05):5栈化亍12元元Is务一(2):完成下列程序,该程序实现栈的链式存储结构,构建链栈(栈中的元素依次为China ,Japan, France,India ,Australia ),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。

要求生成链栈时,从键盘上读取数据元素。

(1)源代码:#i nclude<>#in clude<>#in clude<># define OK 1# define ERROR 0 typedefchar DataType;/*链式栈的存储类型*/typedef struct SNode丰事元元桟脅化戎孩遊次池谚區12345元元化圍我岀:取示退费出贅-■■-・鼻■--■*・■■\z0 12 3 4 5 1 元元 岀^?取示 退、艮岀读显元一兀 栈 化 S 岀:^£取示 04:读取钱顶元录5湿示貨中匹券 甬只梟祜富i枪祐皿5用—务二:完成下列程序,该程序实现循环队列的 存储和基本操作,构建循环队列,完成键盘缓冲区的功能,每输入一个字符,链入缓冲区队 列中;每输出一个字符,将该字符从缓冲区中删除。

栈和队列基本操作实验报告

栈和队列基本操作实验报告

实验二堆栈和队列基本操作的编程实现
【实验目的】
堆栈和队列基本操作的编程实现
要求:
堆栈和队列基本操作的编程实现(2学时,验证型),掌握堆栈和队列的建立、进栈、出栈、进队、出队等基本操作的编程实现,存储结构可以在顺序结构或链接结构中任选,也可以全部实现。

也鼓励学生利用基本操作进行一些应用的程序设计。

【实验性质】
验证性实验(学时数:2H)
【实验内容】
内容:
把堆栈和队列的顺序存储(环队)和链表存储的数据进队、出队等运算其中一部分进行程序实现。

可以实验一的结果自己实现数据输入、数据显示的函数。

利用基本功能实现各类应用,如括号匹配、回文判断、事物排队模拟、数据逆序生成、多进制转换等。

【实验分析、说明过程】
【思考问题】
链表存储:通过链表的存储可以实现链表中任意位置的插入元素,删除任意元素,可以实现无序进出。

2.还会有数据移动吗?为什么?
答:栈的顺序存储不会有数据移动,移动的只是指向该数据地址的指针。

3.栈的主要特点是什么?队列呢?
【实验小结】 (总结本次实验的重难点及心得、体会、收获)
【附录-实验代码】。

实验二(1)讲义-栈和队列的应用

实验二(1)讲义-栈和队列的应用

实验2:栈和队列的应用
一、实验目的
1.掌握栈的表示与实现
2.掌握队列的表示与实现
3.掌握栈的入栈、出栈等基本操作
4.掌握队列的入队、出队等基本操作
二、实验内容
1.实现顺序栈各种基本运算的算法,具体操作要求如下:
(1)初始化栈,并判断栈是否为空;
(2)对a,b,c,d,f五个字符元素模拟进栈操作;并判断栈是否为空;
(3)取出栈顶元素;
(4)对a,b,c,d,f五个字符元素做依次出栈操作,并判断栈是否为空;
(5)释放栈。

具体效果如下:
注:若sqstack.cpp文件中的方法不合适,可以作修改。

2.实现链栈各种基本运算的算法
(1)初始化栈,并判断栈是否为空;
(2)对a,b,c,d,f五个字符元素模拟进栈操作;并判断栈是否为空;
(3)取出栈顶元素;
(4)对a,b,c,d,f五个字符元素做依次出栈操作,并判断栈是否为空;
(5)释放栈。

注:若listack.cpp文件中的方法不合适,可以作修改。

三、实验要求
1.独立完成实验程序的编写与调试;
2.实验完成后填写实验报告,学习委员按学号从小到大的顺序提交。

四、思考题
1.读入一个有限大小的整数n,然后按输入次序的相反次序输出各元素的值。

(用顺序栈
实现)
2.利用栈完成数制转换。

任意输入一个十进制数,将其转换成八进制数。

(用顺序栈实
现)。

实验二___栈与队列的一个应用

实验二___栈与队列的一个应用

中国计量学院实验报告实验课程:《算法与数据结构》实验名称:栈与队列的一个应用班级: 14计算机1 实验日期:实验目的及要求:一、实验目的1、了解栈和队列的特性2. 掌握栈和队列的顺序表示及实现3. 掌握栈和队列的链式表示及实现4、学会利用栈或队列去求解实际问题二、实验要求从键盘输入包括任意三种括号(即,圆括号()、方括号[]和花括号{})的四则运算(+、-、*、/)表达式,编程判断该表达式的括号是否匹配。

若匹配则计算出表达式的值;若不匹配,则输出“此表达式括号不匹配”。

实验中,假设:(1) 除括号不匹配外,不存在其它非法表达式的情况(2) 表达式中只出现数值常量,不出现变量或符号常量三、实验角色1、Document Writer: ADT设计及实验所涉资料的整理、录入及排版2、Programmer: 算法设计及实现3、Tester: 确定测试用例并负责程序测试,测试用例不少于15种数据结构及关键算法说明这一部分描述解决问题所用到的数据结构及关键算法,可用伪代码、代码或框图表示,目的是让读者在短时间内清楚地理解作者解决问题的整体思路。

因此,表达方式必须比源代码更通俗易懂。

typedef struct SqStack //栈的顺序存储结构{SElemType *base;SElemType *top;int stacksize;}SqStack;char Precede(char a1 ,char a2)//判定运算符的优先级。

{char r;switch(a2){case'+': //此处由于加减几乎优先级一样,故放在一起case'-':if(a1=='('||a1=='['||a1=='{'||a1=='#')r='<';elser='>';break;case'(':if(a1==')'){cout<<"括号匹配错误!"<<endl;exit(-1);}elser='<';break;}//这里只列举加减,左括号。

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

实验二栈、队列的实现及应用
实验课程名:数据结构与算法
专业班级:学号::
/*构造空顺序栈*/
int InitStack(SqStack *S) //InitStack() sub-function
{
S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if (!S->base)
{
printf("分配空间失败!\n");
return (ERROR);
}
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
printf("栈初始化成功!\n");
return (OK);
} //InitStack() end
/*取顺序栈顶元素*/
int GetTop(SqStack *S, SElemType *e) //GetTop() sub-function
{
if (S->top == S->base)
{
printf("栈为空!\n"); //if empty SqStack
return (ERROR);
}
*e = *(S->top - 1);
return (OK);
} //GetTop() end
/*将元素压入顺序栈*/
int Push(SqStack *S) //Push() sub-function
{
SElemType e;
if (S->top - S->base>S->stacksize)
{
S->base = (SElemType *)realloc(S->base, (S->stacksize +
STACKINCREMENT*sizeof(SElemType)));
if (!S->base)
{
printf("存储空间分配失败!\n");
return (ERROR);
}
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
fflush(stdin);//清除输入缓冲区,否则原来的输入会默认送给变量x
printf("请输入要入栈的元素的值:");
e = getchar();
*S->top++ = e;
return (OK);
} //Push() end
/* 将元素弹出顺序栈*/
int Pop(SqStack *S, SElemType *e) //Pop() sub-function {
if (S->top == S->base)
{
printf("栈为空!\n");
return (ERROR);
}
*e = *--S->top;
return (OK);
} //Pop() end
void display(SqStack *s)
{
if (s->top == s->base)
printf("栈为空!\n");
else{
while (s->top != s->base)
{
s->top = s->top - 1;
printf("%c->", *(s->top));
}
}
printf("\n");
}
int main()
{
int choice;
SElemType e;
SqStack s;
do
{
printf("===============================\n");
printf(" 0:退出\n");
printf(" 1:初始化栈\n");
printf(" 2:入栈\n");
printf(" 3:出栈\n");
printf(" 4:读取栈顶元素\n");
printf(" 5:显示栈中元素\n");
(3)结果分析
顺序表通过设置栈顶运用线性结构实现先进先出功能。

2.任务一(2):完成下列程序,该程序实现栈的链式存储结构,构建链栈(栈中的元素依次为China,Japan,France,India,Australia),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。

要求生成链栈时,从键盘上读取数据元素。

(1)源代码:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
# define OK 1
# define ERROR 0
typedef char DataType;
/* 链式栈的存储类型 */
typedef struct SNode //define structure LinkStack
{ DataType data[20];
struct SNode *next;
}SNode,*LinkStack;
void InitStack_L (LinkStack *top)
{
top = (LinkStack)malloc(sizeof(SNode)) ;
top->next = NULL;
printf("\n\n栈初始化成功!\n\n");
}
case '3':Pop_L(&s, e);break;
case '4':GetTop_L(&s, e);printf("栈顶元素的值是:%s\n",e);break; case '5': printf("栈中元素的值是: ");display(&s);
}
}while(choice);
return 0;
}
(2)运行结果
(3)结果分析
链表通过设置栈顶运用指针实现先进先出功能
3.任务二:完成下列程序,该程序实现循环队列的存储和基本操作,构建循环队列,完成键盘缓冲区的功能,每输入一个字符,链入缓冲区队列中;每输出一个字符,将该字符从缓冲区中删除。

(1)源代码:#include<stdio.h>
#include<stdlib.h>
# define MAXQSIZE 100
printf("请输入要入队的字符或字符串,以'#'结束:");
while((e=getchar())!='#')
{
EnQueue(&Q,e);
}
break;
case 2:
DeQueue(&Q,e);
break;
case 3: display(&Q);
}
}while(choice>0&&choice<=3);
return 0;
}
(2)运行结果
(3)结果分析
循环队列通过设置队首和队尾实现先进后出功能
实验总结:
1.在本次试验中我学会了如何实现的栈的顺序存储以及链式存储。

2.以及懂得了栈的基本特性:仅在表尾进行删除和插入操作、先进后出。

相关文档
最新文档