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

合集下载

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

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

实验二栈和队列的基本操作实现及其应用一、实验目的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.算术表达式求值 实验要求 十进制整数四则运算计算器。

栈与队列的应用

栈与队列的应用

栈与队列的应用栈(Stack)和队列(Queue)是计算机科学中常见的数据结构,它们分别具有先进后出(Last-In-First-Out, LIFO)和先进先出(First-In-First-Out, FIFO)的特性。

这两种数据结构在计算机领域有着广泛的应用,本文将介绍一些栈与队列的常见应用场景。

一、栈的应用1. 括号匹配栈常被用于判断表达式中的括号是否匹配。

通过遍历表达式中的每个字符,将左括号入栈,当遇到右括号时,检查栈顶元素与右括号是否匹配。

若匹配,则出栈;若不匹配,则说明括号不匹配。

2. 浏览器的前进与后退功能在浏览器中,我们可以通过点击前进和后退按钮来在不同的网页之间切换。

这种功能可以使用两个栈来实现:一个栈用于存储用户浏览的历史页面,另一个栈用于存储用户后退的页面。

当用户点击前进按钮时,从后退栈中弹出页面并推入历史页面栈;当用户点击后退按钮时,从历史页面栈中取出页面并推入后退页面栈。

3. 函数调用与递归在程序中,函数的调用是通过栈来实现的。

当一个函数被调用时,系统会将该函数的返回地址和参数等信息压入栈中;当函数执行完毕后,从栈中弹出返回地址,继续执行调用函数的下一条指令。

4. 表达式求值中缀表达式求值通常需要借助栈来实现。

通过将表达式转换成后缀表达式,并使用栈存储运算符和操作数,可以按照规定的优先级进行计算,得到最终的结果。

二、队列的应用1. 打印任务队列在计算机系统中,多个用户同时提交打印任务时,可以使用队列来管理这些任务。

每当有新的任务到达时,将其加入队列尾部,打印机则从队列头部取出任务进行打印。

这样可以保证任务的顺序性,并避免多个任务之间的冲突。

2. 消息队列在分布式系统中,消息队列通常用于解耦不同模块之间的通信。

一个模块可以将消息发送到队列中,而其他模块可以异步地从队列中获取消息并进行相应的处理。

这种方式提高了系统的可伸缩性和稳定性。

3. 广度优先搜索在图论中,广度优先搜索(Breadth-First Search, BFS)可以借助队列来实现。

实验二栈和队列(基本操作)讲解

实验二栈和队列(基本操作)讲解

实验二栈和队列1、实验目的:(1)熟悉栈的特点(先进后出)及栈的基本操作,如入栈、出栈等,掌握栈的基本操作在栈的顺序存储结构和链式存储结构上的实现;(2)熟悉队列的特点(先进先出)及队列的基本操作,如入队、出队等,掌握队列的基本操作在队列的顺序存储结构和链式存储结构上的实现。

2、实验要求:(1)复习课本中有关栈和队列的知识;(2)用C语言完成算法和程序设计并上机调试通过;(3)撰写实验报告,给出算法思路或流程图和具体实现(源程序)、算法分析结果(包括时间复杂度、空间复杂度以及算法优化设想)、输入数据及程序运行结果(必要时给出多种可能的输入数据和运行结果)。

3、实验内容[实验1] 栈的顺序表示和实现实验内容与要求:编写一个程序实现顺序栈的各种基本运算,并在此基础上设计一个主程序,完成如下功能:(1)初始化顺序栈(2)插入元素(3)删除栈顶元素(4)取栈顶元素(5)遍历顺序栈(6)置空顺序栈分析:栈的顺序存储结构简称为顺序栈,它是运算受限的顺序表。

对于顺序栈,入栈时,首先判断栈是否为满,栈满的条件为:p—〉top= =MAXNUM-1,栈满时,不能入栈;否则出现空间溢出,引起错误,这种现象称为上溢。

出栈和读栈顶元素操作,先判栈是否为空,为空时不能操作,否则产生错误.通常栈空作为一种控制转移的条件。

注意:(1)顺序栈中元素用向量存放(2)栈底位置是固定不变的,可设置在向量两端的任意一个端点(3)栈顶位置是随着进栈和退栈操作而变化的,用一个整型量top(通常称top为栈顶指针)来指示当前栈顶位置#include <stdio.h>#include 〈malloc。

h>typedef int SElemType;typedef int Status;#define INIT_SIZE 100#define STACKINCREMENT 10#define Ok 1#define Error 0#define True 1#define False 0typedef struct{SElemType *base;SElemType *top;int stacksize;}SqStack;//初始化栈Status InitStack(SqStack *s){s->base = (SElemType *)malloc(INIT_SIZE * sizeof(SElemType));if(!s->base){puts("存储空间分配失败!”);return Error;}s-〉top = s—>base;s->stacksize = INIT_SIZE;return Ok;}//清空栈Status ClearStack(SqStack *s){s-〉top = s—〉base;return Ok;}//栈是否为空Status StackEmpty(SqStack *s){if(s-〉top == s->base)return True;elsereturn False;}//销毁栈Status Destroy(SqStack *s)free(s->base);s—>base = NULL;s->top = NULL;s-〉stacksize=0;return Ok;}//获得栈顶元素Status GetTop(SqStack *s,SElemType &e){if(s—〉top == s—>base)return Error;e = *(s—〉top - 1);return Ok;}//压栈Status Push(SqStack *s,SElemType e){if(s—>top - s—〉base >= s->stacksize){s—〉base = (SElemType *)realloc(s->base,(s—〉stacksize + STACKINCREMENT) *sizeof(SElemType));if(!s—>base){puts("存储空间分配失败!");return Error;}s-〉top = s—>base + s—>stacksize;s->stacksize += STACKINCREMENT;}*s->top++ = e;return Ok;}//弹栈Status Pop(SqStack *s, SElemType *e){if(s—〉top == s—>base)return Error;-—s-〉top;*e = *(s—〉top);return Ok;}//遍历栈Status StackTraverse(SqStack *s,Status(*visit)(SElemType)) {SElemType *b = s—>base;SElemType *t = s—>top;while(t 〉b)visit(*b++);printf("\n");return Ok;}Status visit(SElemType c){printf("%d ",c);return Ok;}int main(){SqStack a;SqStack *s = &a;SElemType e;InitStack(s);int n;puts("请输入要进栈的个数:”);scanf("%d",&n);while(n—-){int m;scanf("%d", &m);Push(s, m);}StackTraverse(s,visit);puts("”);Pop(s,&e);printf(”%d\n”, e);printf("%d\n”, *s—>top);Destroy(s);return 0;}[实验2] 栈的链式表示和实现实验内容与要求:编写一个程序实现链栈的各种基本运算,并在此基础上设计一个主程序,完成如下功能:(1)初始化链栈(2)链栈置空(3)入栈(4)出栈(5)取栈顶元素(6)遍历链栈分析:链栈是没有附加头结点的运算受限的单链表.栈顶指针就是链表的头指针。

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

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

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

二、设计思想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运算。

实验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、掌握栈和队列的基本操作实现方法。

/*顺序栈的存储类型*/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用—务二:完成下列程序,该程序实现循环队列的 存储和基本操作,构建循环队列,完成键盘缓冲区的功能,每输入一个字符,链入缓冲区队 列中;每输出一个字符,将该字符从缓冲区中删除。

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

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

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

(用顺序栈实
现)。

相关文档
最新文档