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

实验二栈和队列的基本操作实现及其应用一、实验目的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、通过几个小代码的编写,熟悉栈和队列2、熟悉VC环境(VC 6或),会在其中编写调试运行c++代码,并理解多文件项目的组织,为以后的实验编程做准备3、初步掌握在VC环境中进行代码的调试二、实验内容在实验题目文档中有4个题目,其中题目1、2、3是关于栈的,题目4是关于队列的,要求一次实验完成所有的题目1.题目一读懂实验题目文档中的Task1中的程序(使用栈进行序列的顺序反转),并编译运行,通过此了解如果要实现一个栈类,里面需要的基本的成员函数。
这个程序在书上也有。
(1)由于程序是用了STL(标准模板库,可以简单的看成是一个函数库,在其中有各种有用的类、函数和算法),栈在其中有实现。
栈在STL中的实现用到了类模板,也就是说其栈是独立于类型的,模板提供参数化类型,也就是能将类型名作为参数传递给接收方来建立类或函数。
比如stack<double> numbers;中就是声明了一个栈,这个栈中存放的数据类型为double。
(2)注意,实验题目文档中的这个程序有点语法错误,要使这个程序能运行,首先要改正语法错误。
另外,如果要使用c++的输入输出需要加上几行语句如下,因为cout和cin 是在命名空间std中的:#include <iostream>using namespace std;2.题目二、题目三这里把题目二和题目三合成了一个题目,在一个程序中完成就可以了。
实现一个自己的简单的栈,并用于替换题目一中对标准模板库中的栈的使用,同时对自己实现的栈的功能进行扩充,添加实现如下几个函数(a) clear (b) full (c) size。
使用新添加的栈函数,显示在进行数字序列反转时输入的十进制数的个数。
注意:实验题目文档中已经把大部分的代码都给出来了。
自己实现的栈不要求用类模板,如果能用,当然更好。
栈可以用链表或者数组实现,这里是用数组实现。
注意:实验题目中给出的仅仅是部分的代码,自己还需要在看懂的前提下,进行修改补充,才可以达到具体的要求,不明白的地方也可以参考书上这一部分。
栈和队列基本操作实验报告

栈和队列基本操作实验报告实验目的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. 栈的实现通过栈的实现,我们可以进行压入和弹出元素的操作。
下面是一段示例代码:通过本次实验,我学会了栈和队列的基本概念和特点,掌握了栈和队列的基本操作,如插入、删除、判空、判满等。
这些基本操作是数据结构中的重要部分,对于算法的实现与性能优化都有很大的帮助。
此外,在实现中,我们还需要注意栈和队列指针的变化,以及对于空栈和空队列的处理。
通过本次实验,我加深了对数据结构的理解,同时也提升了编程能力。
栈和队列基本操作实验报告

实验二堆栈和队列基本操作的编程实现
【实验目的】
堆栈和队列基本操作的编程实现
要求:
堆栈和队列基本操作的编程实现(2学时,验证型),掌握堆栈和队列的建立、进栈、出栈、进队、出队等基本操作的编程实现,存储结构可以在顺序结构或链接结构中任选,也可以全部实现。
也鼓励学生利用基本操作进行一些应用的程序设计。
【实验性质】
验证性实验(学时数:2H)
【实验内容】
内容:
把堆栈和队列的顺序存储(环队)和链表存储的数据进队、出队等运算其中一部分进行程序实现。
可以实验一的结果自己实现数据输入、数据显示的函数。
利用基本功能实现各类应用,如括号匹配、回文判断、事物排队模拟、数据逆序生成、多进制转换等。
【实验分析、说明过程】
【思考问题】
链表存储:通过链表的存储可以实现链表中任意位置的插入元素,删除任意元素,可以实现无序进出。
2.还会有数据移动吗?为什么?
答:栈的顺序存储不会有数据移动,移动的只是指向该数据地址的指针。
3.栈的主要特点是什么?队列呢?
【实验小结】 (总结本次实验的重难点及心得、体会、收获)
【附录-实验代码】。
堆栈和队列的基本操作

堆栈和队列的基本操作堆栈(Stack)和队列(Queue)是两种经典的数据结构,用于解决各种问题。
它们都是线性数据结构,但是在实现和操作上有所不同。
堆栈是一种后进先出(Last In, First Out,LIFO)的数据结构,类似于我们日常生活中的堆盘子的行为。
最新添加的元素放在堆栈的顶部,而访问和删除元素的也是顶部的元素。
堆栈的基本操作有压栈(Push)、弹栈(Pop)、获取栈顶元素(Top)和判断是否为空栈(IsEmpty)。
压栈操作将一个元素添加到堆栈的顶部。
弹栈操作将堆栈顶部的元素移除,并返回该元素的值。
获取栈顶元素操作返回堆栈顶部的元素的值,但不从堆栈中移除。
判断是否为空栈操作检查堆栈是否为空,如果为空则返回真,否则返回假。
堆栈通常使用数组或链表来实现。
使用数组实现的堆栈称为顺序堆栈,而使用链表实现的堆栈称为链式堆栈。
顺序堆栈的优点是访问元素的时间复杂度为O(1),但是它的容量是固定的。
链式堆栈的优点是容量可以动态增长,但是访问元素的时间复杂度为O(n),其中n是堆栈中元素的个数。
队列是一种先进先出(First In, First Out,FIFO)的数据结构,类似于我们日常生活中排队的行为。
元素被添加到队列的尾部,而访问和删除元素是从队列的头部进行的。
队列的基本操作有入队(Enqueue)、出队(Dequeue)、获取队列头元素(Front)和判断是否为空队列(IsEmpty)。
入队操作将一个元素添加到队列的尾部。
出队操作将队列头部的元素移除,并返回该元素的值。
获取队列头元素操作返回队列头部的元素的值,但不从队列中移除。
判断是否为空队列操作检查队列是否为空,如果为空则返回真,否则返回假。
队列通常使用数组或链表来实现。
使用数组实现的队列称为顺序队列,而使用链表实现的队列称为链式队列。
顺序队列的优点是访问元素的时间复杂度为O(1),但是它的容量是固定的。
链式队列的优点是容量可以动态增长,但是访问元素的时间复杂度为O(n),其中n是队列中元素的个数。
栈和队列的操作实验小结

栈和队列的操作实验小结一、实验目的本次实验旨在深入理解和掌握栈和队列这两种基本数据结构的基本操作,包括插入、删除、查找等操作,并通过实际操作加深对这两种数据结构特性的理解。
二、实验原理栈(Stack):栈是一种后进先出(Last In First Out,LIFO)的数据结构,即最后一个进入栈的元素总是第一个出栈。
在计算机程序中,栈常常被用来实现函数调用和递归等操作。
队列(Queue):队列是一种先进先出(First In First Out,FIFO)的数据结构,即第一个进入队列的元素总是第一个出队。
在计算机程序中,队列常常被用来实现任务的调度和缓冲等操作。
三、实验步骤与结果创建一个空栈和一个空队列。
对栈进行入栈(push)和出栈(pop)操作,观察并记录结果。
可以发现,栈的出栈顺序与入栈顺序相反,体现了后进先出的特性。
对队列进行入队(enqueue)和出队(dequeue)操作,观察并记录结果。
可以发现,队列的出队顺序与入队顺序相同,体现了先进先出的特性。
尝试在栈和队列中查找元素,记录查找效率和准确性。
由于栈和队列的特性,查找操作并不像在其他数据结构(如二叉搜索树或哈希表)中那样高效。
四、实验总结与讨论通过本次实验,我更深入地理解了栈和队列这两种数据结构的基本特性和操作。
在实际编程中,我可以根据需求选择合适的数据结构来提高程序的效率。
我注意到,虽然栈和队列在某些操作上可能不如其他数据结构高效(如查找),但它们在某些特定场景下具有无可替代的优势。
例如,在实现函数调用和递归时,栈的特性使得它成为最自然的选择;在实现任务调度和缓冲时,队列的特性使得它成为最佳选择。
我也认识到,不同的数据结构适用于解决不同的问题。
在选择数据结构时,我需要考虑数据的特性、操作的频率以及对时间和空间复杂度的需求等因素。
通过实际操作,我对栈和队列的实现方式有了更深入的理解。
例如,我了解到栈可以通过数组或链表来实现,而队列则可以通过链表或循环数组来实现。
栈出队列公式

栈出队列公式栈和队列是数据结构中常见的两种线性结构。
栈是一种后进先出(Last In, First Out)的数据结构,而队列是一种先进先出(First In, First Out)的数据结构。
在实际应用中,我们有时需要将栈转化为队列,即栈出队列。
本文将介绍栈出队列的公式及其实现方法。
一、栈出队列的公式栈出队列的公式是指将栈转化为队列的操作。
假设我们有一个栈S和一个空队列Q,栈S中的元素依次为S1, S2, S3, ..., Sn。
要将栈S转化为队列Q,我们可以按照以下公式进行操作:1. 将栈S中的元素依次出栈,并将出栈的元素依次入队列Q中,直到栈S为空。
2. 此时队列Q中的元素顺序与栈S中的元素顺序相反。
3. 将队列Q中的元素依次出队列,并将出队列的元素依次入栈S中,直到队列Q为空。
4. 此时栈S中的元素顺序与原栈S中的元素顺序相同,即栈S已经转化为队列Q。
二、栈出队列的实现栈出队列的实现可以借助两个栈来完成。
我们称一个栈为栈A,另一个栈为栈B。
栈A用于入栈操作,栈B用于出栈操作。
1. 将栈S中的元素依次入栈A。
2. 当需要出队列时,先检查栈B是否为空,若为空,则将栈A中的元素依次出栈并入栈B。
3. 将栈B的栈顶元素出栈,即为队列的出队列元素。
4. 当栈A和栈B同时为空时,表示队列为空。
5. 如果需要继续进行入队列操作,可以将元素入栈A。
三、栈出队列的应用场景栈出队列的公式可以应用于许多实际场景中。
例如,我们需要实现一个优先级队列,栈出队列的公式可以帮助我们按照元素的优先级顺序进行操作。
具体实现时,我们可以给每个元素设置一个优先级,并将优先级高的元素先入栈,然后按照栈出队列的公式进行操作,即可实现优先级队列的功能。
另一个应用场景是在算法中,栈出队列的公式可以帮助我们实现栈的排序。
具体实现时,我们可以将需要排序的元素依次入栈A,然后按照栈出队列的公式进行操作,最后栈A中的元素就按照从小到大的顺序排列了。
栈与队列实验报告总结

栈与队列实验报告总结实验报告总结:栈与队列一、实验目的本次实验旨在深入理解栈(Stack)和队列(Queue)这两种基本的数据结构,并掌握其基本操作。
通过实验,我们希望提高自身的编程能力和对数据结构的认识。
二、实验内容1.栈的实现:我们首先使用Python语言实现了一个简单的栈。
栈是一种后进先出(LIFO)的数据结构,支持元素的插入和删除操作。
在本次实验中,我们实现了两个基本的栈操作:push(插入元素)和pop(删除元素)。
2.队列的实现:然后,我们实现了一个简单的队列。
队列是一种先进先出(FIFO)的数据结构,支持元素的插入和删除操作。
在本次实验中,我们实现了两个基本的队列操作:enqueue(在队尾插入元素)和dequeue(从队头删除元素)。
3.栈与队列的应用:最后,我们使用所实现的栈和队列来解决一些实际问题。
例如,我们使用栈来实现一个算术表达式的求值,使用队列来实现一个简单的文本行编辑器。
三、实验过程与问题解决在实现栈和队列的过程中,我们遇到了一些问题。
例如,在实现栈的过程中,我们遇到了一个“空栈”的错误。
经过仔细检查,我们发现是因为在创建栈的过程中没有正确初始化栈的元素列表。
通过添加一个简单的初始化函数,我们解决了这个问题。
在实现队列的过程中,我们遇到了一个“队列溢出”的问题。
这是因为在实现队列时,我们没有考虑到队列的容量限制。
通过添加一个检查队列长度的条件语句,我们避免了这个问题。
四、实验总结与反思通过本次实验,我们对栈和队列这两种基本的数据结构有了更深入的理解。
我们掌握了如何使用Python语言实现这两种数据结构,并了解了它们的基本操作和实际应用。
在实现栈和队列的过程中,我们也学到了很多关于编程的技巧和方法。
例如,如何调试代码、如何设计数据结构、如何优化算法等。
这些技巧和方法将对我们今后的学习和工作产生积极的影响。
然而,在实验过程中我们也发现了一些不足之处。
例如,在实现栈和队列时,我们没有考虑到异常处理和性能优化等方面的问题。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
封面:安徽大学网络工程栈和队列的基本操作的实现______2010\4\12【实验目的】1.理解并掌握栈和队列的逻辑结构和存储结构;2.理解栈和队列的相关基本运算;3.编程对相关算法进行验证。
【实验内容】(一)分别在顺序和链式存储结构上实现栈的以下操作(含初始化,入栈,出栈,取栈顶元素等):1.构造一个栈S,将构造好的栈输出;2.在第1步所构造的栈S中将元素e 入栈,并将更新后的栈S输出;3.在第2步更新后所得到的栈S中将栈顶元素出栈,用变量e返回该元素,并将更新后的栈S输出。
(二)分别在链队列和循环队列上实现以下操作(初始化,入队,出队,取队头元素等):1.构造一个队列Q,将构造好的队列输出;2.在第1步所构造的队列Q中将元素e入队,并将更新后的队列Q输出;3.在第2步更新后所得到的队列Q中将队头元素出队,用变量e返回该元素,并将更新后的队列Q输出。
【要求】1.栈和队列中的元素要从终端输入;2.具体的输入和输出格式不限;3.算法要具有较好的健壮性,对运行过程中的错误操作要做适当处理。
三、实验步骤1.本实验用到的数据结构(1)逻辑结构:线性结构(2)存储结构:程序一、四(顺序存储结构);程序二、三(链式存储结构);2.各程序的功能和算法设计思想程序一:顺序栈# include <stdio.h># include <malloc.h># include <process.h>#define STACKINITISIZE 100# define STACKINCREMENT 10# define OK 1# define ERROR 0# define OVERFLOW -2typedef int SElemtype;typedef int status;typedef struct {SElemtype *base;SElemtype *top;int stacksize;}sqstack;void Initstack (sqstack *s) {(*s).base = (SElemtype *)malloc(STACKINITISIZE * sizeof (SElemtype));if(!(*s).base) exit(OVERFLOW);(*s).top = (*s).base;(*s).stacksize = STACKINITISIZE;}void 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 )exit (OVERFLOW);(*s).top = (*s).base +(*s).stacksize ;(*s).stacksize += STACKINCREMENT;}*(*s).top ++ = e;}status Gettop (sqstack s ) {int e;if (s.top ==s.base )return ERROR;e=*(s.top-1);printf ("栈顶元素是%d\n",e);return OK;}status pop ( sqstack *s ) {int f;if ( (*s).top==(*s).base) return ERROR;f = *(--(*s).top);printf("出栈元素是%d\n",f);return OK;}void stackTraverse(sqstack s ){SElemtype * p =s.base;while (s.top>p)printf ("%d ",*p++);printf("\n");}void main(){int h,k,e,i;sqstack la;printf ("构建一个空栈\n");Initstack (&la);printf("请输入栈内元素的个数\n");scanf ("%d",&k);printf("请输入%d个元素\n",k);for (i=0;i<k;i++){scanf ("%d",&e);push (&la,e);}printf ("\n");printf("输出栈内所有元素\n");stackTraverse (la);fflush (stdin);printf("查找栈顶元素\n");Gettop (la);printf("删除栈顶元素\n");pop (&la);printf("输出栈内所有元素\n");stackTraverse (la);fflush (stdin);printf ("\n");printf ("插入一个元素\n");printf("请输入插入的元素值\n");scanf ("%d",&h);push (&la,h);printf("输出栈内所有元素\n");stackTraverse (la);printf("\n");}功能:实现顺序栈的各种功能,如能建立空栈,实现栈的初始化,插入,删除栈顶元素等操作。
算法设计思想:首先建立一个空栈,再实现栈的初始化,用一个主函数包涵栈的各种操作。
程序调式如下:程序二:链栈// shuju3.cpp : 定义控制台应用程序的入口点。
#include"stdafx.h"#include<malloc.h>#include<stdio.h>#include<process.h># define OK 1# define ERROR 0typedef int status;typedef struct SNode{int data;struct SNode *next;}SNode,*Sqstack;void Createsqstack(Sqstack *l,int n){ int i;Sqstack s;*l=(Sqstack) malloc(sizeof(SNode));(*l)->next=NULL;printf("请输入%d个元素\n",n);for(i=n;i>0;--i){s=(Sqstack) malloc(sizeof(SNode));scanf("%d",&(s->data));s->next=(*l)->next;(*l)->next=s;}}status Getelem(Sqstack *l,int *e){Sqstack s;s=(*l)->next;*e=s->data;printf("头元素是%d\n",*e);return OK;}status insertsqtack(Sqstack l,int e,int n){ Sqstack p,s;int i;p=l;for(i=0;i<n;i++){p=p->next;}s=(Sqstack) malloc (sizeof(SNode));s->data = e;p->next=s;s->next=NULL;return OK;}status Deletesqstack(Sqstack l){int h;Sqstack p,q;p=l;q=p->next;p->next=q->next;h=q->data;printf("删除的元素是%d",h);free(q);return OK;}void sqstackTraverse(Sqstack l){Sqstack p;p=l->next;while(p){printf("%d ",p->data);p=p->next;}}void main (){int i,j,n,k,e;Sqstack la;printf("请输入链栈的长度\n");scanf("%d",&n);printf("建立一个链栈\n");Createsqstack(&la,n);printf("输出各元素\n");printf("la=");sqstackTraverse(la);fflush(stdin);printf("\n");printf("查找栈顶元素\n");Getelem(&la,&e);fflush(stdin);printf("\n");printf("插入新的栈顶元素\n");scanf("%d",&e);insertsqtack(la,e,n);printf("输出各元素\n");printf("la=");sqstackTraverse(la);fflush(stdin);printf("\n");printf("删除栈顶元素\n");Deletesqstack(la);printf("输出各元素\n");printf("la=");sqstackTraverse(la);printf("\n");}功能:实现链栈的基本功能,如初始化,删除,插入,查找栈顶元素等。
算法设计思想:利用单链表的形式建立一个链栈,定义一个结构体,利用指针指向,建立链栈的具有不同功能的函数(删除、插入、查找等),利用主函数合理安排顺序实现链栈操作。
调试情况如下:程序三:链队列// SHUJU.cpp : 定义控制台应用程序的入口点。