实验二 栈和队列

合集下载

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

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

实验二栈和队列的基本操作实现及其应用一、实验目的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;}。

实验二栈和队列应用

实验二栈和队列应用

数据结构
实验题目
9.简单背包问题 问题描述 设一个背包所允许的重量是M,假设有N件物品, 物品的重量分别是Wi,可以任意挑选物品将背包装满。 实验要求 设计程序实现将给定背包装满的可能解。 1)采用栈等数据结构。 2)应用穷举法回溯策略求解。 3)尝试采用递归和非递归算法求解。
数据结构
实验题目
7.八皇后问题 问题描述 设一个8×8的棋盘里放置8个皇后,要求在每行、 每列、没斜线只允许放置1个皇后。 实验要求 设计实现所有可能解的程序。 1)采用栈等数据结构。 2)应用穷举法回溯策略求解。 3)尝试采用递归和非递归算法求解。
数据结构
实验题目
8.马踏棋盘问题 问题描述 中国象棋中的“马”走子的规则是:马走日字形。 实验要求 设计实现求象棋盘中的某一点出发踏遍棋盘所有点 的程序。 1)采用栈等数据结构。 2)应用穷举法回溯策略求解。 3)尝试求解所有出发点的可能 解。
数据结构
实验题目
3.运动员混合双打组合 问题描述 设有M个男羽毛球运动员和N个女羽毛球运动员,现 进行男女混合双打组合K轮配对。男女运动员分别编号 排队在等候队列,按顺序依次从男女运动员中各出队1 人组合配对。本轮没成功配对者等待下一轮次配对。
数据结构
实验题目
3.运动员混合双打组合 实验要求 设计程序模拟完成运动员组合配对过程。 1)采用队列等数据结构。 2)输出每轮的配对信息。
数据结构实验
LOGO
数据结构
实验二
实验题目
栈和队列应用
1.算术表达式求值 问题描述 由输入的四则算术表达式字符串,动态生成算术表 达式所对应的后缀式,通过后缀式求值并输出。
数据结构 实验题目
1.算术表达式求值 实验要求 十进制整数四则运算计算器。

数据结构栈和队列实验报告

数据结构栈和队列实验报告

数据结构栈和队列实验报告实验报告:数据结构栈和队列一、实验目的1.了解栈和队列的基本概念和特点;2.掌握栈和队列的基本操作;3.掌握使用栈和队列解决实际问题的方法。

二、实验内容1.栈的基本操作实现;2.队列的基本操作实现;3.使用栈和队列解决实际问题。

三、实验原理1.栈的定义和特点:栈是一种具有后进先出(LIFO)特性的线性数据结构,不同于线性表,栈只能在表尾进行插入和删除操作,称为入栈和出栈操作。

2.队列的定义和特点:队列是一种具有先进先出(FIFO)特性的线性数据结构,不同于线性表,队列在表头删除元素,在表尾插入元素,称为出队和入队操作。

3.栈的基本操作:a.初始化:建立一个空栈;b.入栈:将元素插入栈的表尾;c.出栈:删除栈表尾的元素,并返回该元素;d.取栈顶元素:返回栈表尾的元素,不删除。

4.队列的基本操作:a.初始化:建立一个空队列;b.入队:将元素插入队列的表尾;c.出队:删除队列表头的元素,并返回该元素;d.取队头元素:返回队列表头的元素,不删除。

四、实验步骤1.栈的实现:a.使用数组定义栈,设置栈的大小和栈顶指针;b.实现栈的初始化、入栈、出栈和取栈顶元素等操作。

2.队列的实现:a.使用数组定义队列,设置队列的大小、队头和队尾指针;b.实现队列的初始化、入队、出队和取队头元素等操作。

3.使用栈解决实际问题:a.以括号匹配问题为例,判断一个表达式中的括号是否匹配;b.使用栈来实现括号匹配,遍历表达式中的每个字符,遇到左括号入栈,遇到右括号时将栈顶元素出栈,并判断左右括号是否匹配。

4.使用队列解决实际问题:a.以模拟银行排队问题为例,实现一个简单的银行排队系统;b.使用队列来模拟银行排队过程,顾客到达银行时入队,处理完业务后出队,每个顾客的业务处理时间可以随机确定。

五、实验结果与分析1.栈和队列的基本操作实现:a.栈和队列的初始化、入栈/队、出栈/队以及取栈顶/队头元素等操作均能正常运行;b.栈和队列的时间复杂度均为O(1),操作效率很高。

实验2 - 栈和队列的应用

实验2 - 栈和队列的应用

实验二栈和队列的应用——数值转换器
实验目的:
本实验的目的是使学生深入了解栈和队列的特征,以便在实际问题背景下灵活运用它们;同时还将巩固这两种结构的构造方法,熟练掌握顺序存储映像和链式映像中各类基本操作的实现。

实验要求:
从键盘输入一个10进制数,编程将其转换成16进制数,并输出。

要求实现下列函数:
(1) 实现链栈的各个基本操作函数;
(2) Transfer( )函数实现进制转换工作;
(3) main( )函数进行调用。

[实现提示]
输入:10进制整数
输出:16进制数(由数字0~9和字符A~F组成)
[测试数据]
由学生自己确定,注意边界数据。

程序运行结果:
程序源码:(后付纸)
实验心得体会:十进制转化为十六进制的方法选用了十进制数除以十六取余,因为最先求出来的余数是个位,然后是百位千位等,所以又用到了栈,使得计数单位最大的最先输出。

判断了如果从11到15就用A-B替换。

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

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

实验二栈和队列的基本操作及其应用一、实验目的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. 加深对数据结构的理解,提高编程能力。

实验原理1. 栈(Stack)是一种“先进后出”(Last In First Out,简称LIFO)的数据结构,类似于一摞盘子,最先放入的盘子在最底下,最后放入的盘子在最上面,取出盘子时也是从上往下取出。

2. 队列(Queue)是一种“先进先出”(First In First Out,简称FIFO)的数据结构,类似于排队,先来的人先进队列,后来的人排在后面,服务员按照队列顺序依次为每个人提供服务。

实验内容1. 栈的实现栈的基本操作包括入栈(push)、出栈(pop)、判空(empty)、栈顶(top)。

本次实验选择使用顺序栈来实现,并通过代码模拟栈的基本操作。

在顺序栈的实现中,需要设置栈顶指针(top)、初始化栈、入栈、出栈、判空、判满等操作,其中最关键的是入栈和出栈操作。

入栈操作:在栈顶指针(top)的位置加1,将元素e放到栈顶```void push(SqStack &s, ElemType e){if (s.top == MAXSIZE - 1) // 栈满return;s.top++; // 栈顶指针加1s.data[s.top] = e; // 将元素e放到栈顶return;}```出队操作:将队头元素弹出,队头指针(front)加1实验结果1. 栈的实现通过栈的实现,我们可以进行压入和弹出元素的操作。

下面是一段示例代码:通过本次实验,我学会了栈和队列的基本概念和特点,掌握了栈和队列的基本操作,如插入、删除、判空、判满等。

这些基本操作是数据结构中的重要部分,对于算法的实现与性能优化都有很大的帮助。

此外,在实现中,我们还需要注意栈和队列指针的变化,以及对于空栈和空队列的处理。

通过本次实验,我加深了对数据结构的理解,同时也提升了编程能力。

实验二(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.利用栈完成数制转换。

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

(用顺序栈实
现)。

栈和队列实验报告总结

栈和队列实验报告总结

栈和队列实验报告总结一、实验目的本次实验的主要目的是掌握栈和队列这两种数据结构的基本概念和操作方法,以及了解它们在计算机程序设计中的应用。

二、实验原理1. 栈栈是一种后进先出(LIFO)的数据结构,它可以看作是一种线性表。

栈顶指针指向栈顶元素,每次插入或删除元素时,都会改变栈顶指针的位置。

常见的操作有入栈(push)、出栈(pop)、取栈顶元素(top)等。

2. 队列队列是一种先进先出(FIFO)的数据结构,它也可以看作是一种线性表。

队头指针指向队头元素,队尾指针指向队尾元素。

常见的操作有入队(enqueue)、出队(dequeue)、取队头元素(front)等。

三、实验内容与步骤1. 栈本次实验中我们需要完成以下操作:① 初始化一个空栈;② 将10个整数依次压入栈中;③ 弹出3个整数并输出;④ 将5个整数依次压入栈中;⑤ 弹出所有整数并输出。

具体步骤如下:Step 1:定义一个Stack类,并在其中定义初始化、入栈、出栈、取栈顶元素等方法;Step 2:在主函数中创建一个Stack对象,并调用初始化方法;Step 3:使用循环将10个整数依次压入栈中;Step 4:使用循环弹出3个整数并输出;Step 5:使用循环将5个整数依次压入栈中;Step 6:调用出栈方法弹出所有整数并输出。

2. 队列本次实验中我们需要完成以下操作:① 初始化一个空队列;② 将10个整数依次加入队列中;③ 弹出3个整数并输出;④ 将5个整数依次加入队列中;⑤ 弹出所有整数并输出。

具体步骤如下:Step 1:定义一个Queue类,并在其中定义初始化、入队、出队、取队头元素等方法;Step 2:在主函数中创建一个Queue对象,并调用初始化方法;Step 3:使用循环将10个整数依次加入队列中;Step 4:使用循环弹出3个整数并输出;Step 5:使用循环将5个整数依次加入队列中;Step 6:调用出队方法弹出所有整数并输出。

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

实验二栈和队列
一、实验目的
1、掌握栈的结构特性及其入栈,出栈操作;
2、掌握队列的结构特性及其入队、出队的操作,掌握循环队列的特点及其操作。

二、实验预习
说明以下概念
1、顺序栈:
2、链栈:
3、循环队列:
4、链队
三、实验内容和要求
1、阅读下面程序,将函数Push和函数Pop补充完整。

要求输入元素序列1 2 3 4 5 e,运行结果如下所示。

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define STACK_INT_SIZE 10 /*存储空间初始分配量*/
#define STACKINCREMENT 5 /*存储空间分配增量*/
typedef int ElemType; /*定义元素的类型*/
typedef struct{
ElemType *base;
ElemType *top;
int stacksize; /*当前已分配的存储空间*/
}SqStack;
int InitStack(SqStack *S); /*构造空栈*/
int push(SqStack *S,ElemType e); /*入栈*/
int Pop(SqStack *S,ElemType *e); /*出栈*/
int CreateStack(SqStack *S); /*创建栈*/
void PrintStack(SqStack *S); /*出栈并输出栈中元素*/
int InitStack(SqStack *S){
S->base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType)); if(!S->base) return ERROR;
S->top=S->base;
S->stacksize=STACK_INT_SIZE;
return OK;
}/*InitStack*/
int Push(SqStack *S,ElemType e){
}/*Push*/
int Pop(SqStack *S,ElemType *e){
}/*Pop*/
}
/*CreateStack*/
int CreateStack(SqStack *S){
int e;
if(InitStack(S))
printf("Init Success!\n");
else
{
printf("Init Fail!\n");
return ERROR;
}
printf("input data:(Terminated by inputing a character)\n"); while(scanf("%d",&e))
Push(S,e);
Return OK;
}
void PrintStack(SqStack *S){
ElemType e;
while(Pop(S,&e))
printf("%3d",e);
}/*Pop_and_Print*/
int main(){
SqStack ss;
printf("\n 1-createStack\n");
CreateStack(&ss);
printf("\n2-Pop&Print\n");
PrintStack(&ss);
return 0;
}
●算法分析:输入元素序列1 2 3 4 5,为什么输出序列为5 4 3 2 1?体现了栈的什么
特性?
2、在第1题的程序中,编写一个十进制转换为二进制的数制转换算法函数(要求利用栈来实现),并验证其正确性。

●实现代码
●验证
3、阅读并运行程序,并分析程序功能。

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define M 20
#define elemtype char
typedef struct
{
elemtype stack[M];
int top;
}
stacknode;
void init(stacknode *st);
void push(stacknode *st,elemtype x);
void pop(stacknode *st);
void init(stacknode *st)
{
st->top=0;
}
void push(stacknode *st,elemtype x)
{
if(st->top==M)
printf("the stack is overflow!\n"); else
{
st->top=st->top+1;
st->stack[st->top]=x;
}
}
void pop(stacknode *st)
{
if(st->top>0) st->top--;
else printf(“Stack is Empty!\n”);
}
int main()
{
char s[M];
int i;
stacknode *sp;
printf("create a empty stack!\n");
sp=malloc(sizeof(stacknode));
init(sp);
printf("input a expression:\n");
gets(s);
for(i=0;i<strlen(s);i++)
{
if(s[i]=='(')
push(sp,s[i]);
if(s[i]==')')
pop(sp);
}
if(sp->top==0)
printf("'('match')'!\n");
else
printf("'('not match')'!\n");
return 0;
}
●输入:2+((c-d)*6-(f-7)*a)/6
●运行结果:
●输入:a-((c-d)*6-(s/3-x)/2
●运行结果:
●程序的基本功能:
以下为选做实验:
4、设计算法,将一个表达式转换为后缀表达式,并按照后缀表达式进行计算,得出表达式得结果。

●实现代码
5、假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾结点(不设队头指针),试编写相应的置空队列、入队列、出队列的算法。

实现代码:
四、实验小结
五、教师评语。

相关文档
最新文档