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

数据结构回文实验报告1. 实验目的本实验旨在通过使用数据结构中的栈和队列的知识,设计并实现一个回文判断程序,以检测给定的字符串是否为回文。
2. 实验原理回文是指正读和反读都相同的字符串。
在本实验中,我们将使用栈和队列来判断给定字符串是否为回文。
具体步骤如下:2.1 将字符串加入栈和队列将给定的字符串依次加入栈和队列中,保持顺序一致。
2.2 从栈和队列中弹出字符从栈和队列中分别弹出字符,并进行比较。
2.3 判断是否为回文如果所有字符都一一相等,那么该字符串就是回文。
否则,不是回文。
3. 实验步骤接下来,我们将按照上述原理,逐步进行回文判断的实验。
3.1 导入所需库由于本实验仅使用了基本的数据结构,无需导入额外的库或模块。
3.2 创建栈和队列首先,我们需要创建栈和队列的数据结构。
栈可以通过使用列表来实现,而队列则可以通过使用双端队列来实现。
# 创建栈stack = []# 创建队列from collections import dequequeue = deque()3.3 输入字符串接下来,我们需要从用户获取一个待判断的字符串。
# 获取待判断的字符串string = input("请输入待判断的字符串:")3.4 将字符串加入栈和队列将输入的字符串依次加入栈和队列中。
# 将字符串加入栈和队列for char in string:stack.append(char)queue.append(char)3.5 从栈和队列中弹出字符并比较从栈和队列中分别弹出字符,并进行比较,直到栈或队列为空。
is_palindrome =Truewhile len(stack) >0and len(queue) >0:stack_char = stack.pop()queue_char = queue.popleft()if stack_char != queue_char:is_palindrome =Falsebreak3.6 输出判断结果根据比较结果,输出判断字符串是否为回文。
实验报告——栈和队列的应用

实验5 栈和队列的应用目的和要求:(1)熟练栈和队列的基本操作;(2)能够利用栈与队列进行简单的应用。
一、题目题目1.利用顺序栈和队列,实现一个栈和一个队列,并利用其判断一个字符串是否是回文。
所谓回文,是指从前向后顺读和从后向前倒读都一样的字符串。
例如,a+b&b+a等等。
题目2. 假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。
跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。
若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。
现要求写一算法模拟上述舞伴配对问题,并实现。
题目3. 打印机提供的网络共享打印功能采用了缓冲池技术,队列就是实现这个缓冲技术的数据结构支持。
每台打印机具有一个队列(缓冲池),用户提交打印请求被写入到队列尾,当打印机空闲时,系统读取队列中第一个请求,打印并删除之。
请利用队列的先进先出特性,完成打印机网络共享的先来先服务功能。
题目4. 假设以数组Q[m]存放循环队列中的元素, 同时设置一个标志tag,以tag == 0和tag == 1来区别在队头指针(front)和队尾指针(rear)相等时,队列状态为“空”还是“满”。
试编写与此结构相应的插入(enqueue)和删除(dlqueue)算法。
题目5. 利用循环链队列求解约瑟夫环问题。
请大家从本组未讨论过的五道题中选择一道,参照清华邓俊辉老师MOOC视频及课本相关知识,编写相应程序。
选择题目3:打印机提供的网络共享打印功能采用了缓冲池技术,队列就是实现这个缓冲技术的数据结构支持。
二、程序清单//Ch3.cpp#include<iostream.h>#include<stdio.h>#include"ch3.h"template <class T>void LinkedQueue<T>::makeEmpty()//makeEmpty//函数的实现{LinkNode<T>*p;while(front!=NULL)//逐个删除队列中的结点{p=front;front=front->link;delete p;}};template <class T>bool LinkedQueue<T>::put_in(T&x){//提交命令函数if(front==NULL){//判断是否为空front=rear=new LinkNode<T>;//如果为空,新结点为对头也为对尾front->data=rear->data=x;if(front==NULL) //分配结点失败return false;}else{rear->link=new LinkNode<T>;//如不为空,在链尾加新的结点rear->link->data=x;if(rear->link==NULL)return false;rear=rear->link;}return true;};template <class T>bool LinkedQueue<T>::carry_out()//执行命令函数{if(IsEmpty()==true)//判断是否为空{ return false; }cout<<front->data<<" ";//输出链尾的数据,代表执行打印命令LinkNode<T>*p=front;front=front->link;//删除以执行的命令,即对头修改delete p; //释放原结点return true;};void main() //主函数{LinkedQueue<char> q;//定义类对象char flag='Y'; //标志是否输入了命令const int max=30;//一次获取输入命令的最大个数while(flag=='Y') //循环{int i=0;char str[max]; //定义存储屏幕输入的命令的数组gets(str); //获取屏幕输入的命令while(str[i]!='\0'){q.put_in(str[i]);//调用提交命令函数,将每个命令存入队列中i++;}for(int j=0;j<=i;j++){if(q.IsEmpty()==true) //判断是否为空,为空则说明没有可执行的命令{cout<<"已经没有可执行的命令!是否继续输入命令(Y|N)?"<<endl;cin>>flag;continue; //为空跳出for循环为下次输入命令做好准备}q.carry_out();//调用执行命令的函数,将命令打印并删除}三、程序调试过程中所出现的错误无。
用栈实现回文判断的算法

用栈实现回文判断的算法回文是指正读和反读都相同的字符串或序列,如"level"、"madam"等。
判断一个字符串是否为回文是编程中常见的问题,本文将介绍如何利用栈来实现这一功能。
栈是一种特殊的线性数据结构,具有后进先出(Last-In-First-Out,LIFO)的特点。
栈可以通过压栈(push)和弹栈(pop)操作来实现数据的存储和访问。
以字符串为例,我们可以通过将字符串中的每个字符依次入栈,然后再依次出栈,得到一个与原字符串相反的字符串。
如果这两个字符串相等,那么原字符串就是回文。
具体实现时,我们可以使用一个辅助栈来完成入栈和出栈操作。
首先,将原字符串的每个字符依次入栈,然后依次出栈并拼接到一个新的字符串中。
最后,将新的字符串与原字符串进行比较,如果相等,则原字符串是回文。
下面是用栈实现回文判断的算法的详细步骤:1. 创建一个空栈和一个空字符串。
2. 遍历原字符串的每个字符:- 将当前字符入栈。
3. 弹栈并将弹出的字符拼接到新字符串中,直到栈为空。
4. 将新字符串与原字符串进行比较:- 如果相等,则原字符串是回文;- 如果不相等,则原字符串不是回文。
下面是用栈实现回文判断的算法的Python代码实现:```pythondef is_palindrome(s):stack = []new_s = ""for c in s:stack.append(c)while stack:new_s += stack.pop()return new_s == s# 测试print(is_palindrome("level")) # 输出 Trueprint(is_palindrome("hello")) # 输出 False```通过上述算法的实现,我们可以用栈来判断一个字符串是否为回文。
算法的时间复杂度为O(n),其中n是字符串的长度。
实验二 栈和队列的基本操作实现及其应用

实验二栈和队列的基本操作实现及其应用一、实验目的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;}。
C语言用栈和队列实现的回文检测功能示例

C语⾔⽤栈和队列实现的回⽂检测功能⽰例本⽂实例讲述了C语⾔⽤栈和队列实现的回⽂功能。
分享给⼤家供⼤家参考,具体如下:#include<stdio.h>#include<malloc.h>//内存分配头⽂件#include<math.h>//在math.h中已定义OVERFLOW的值为3#define SIZE 100#define STACKINCREMENT 10#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0typedef int Status;typedef struct //栈的结构体{char a;} SElemType;typedef struct{SElemType *base;SElemType *top;int stacksize;} SqStack;typedef struct //QNode //队列的结构体{char b;struct QNode * next;} QNode,*QueuePtr;typedef struct // 链队列类型{QueuePtr front; // 队头指针QueuePtr rear; // 队尾指针} LinkQueue;//定义全局变量SqStack S;SElemType e;LinkQueue Q;QueuePtr p;char f;//栈操作Status InitStack(SqStack *S){S->base=(SElemType *)malloc(SIZE*sizeof(SElemType));if(!S->base) exit(OVERFLOW);S->top=S->base;S->stacksize=SIZE;return OK;}Status Push(SqStack *S,SElemType e){if(S->top-S->base>=S->stacksize){S->base=(SElemType *)malloc((S->stacksize+STACKINCREMENT)*sizeof(SElemType));if(!S->base) exit(OVERFLOW);S->top=S->base+S->stacksize;S->stacksize+=STACKINCREMENT;}*S->top++=e;return OK;}Status Stackempty(SqStack S)//栈是否为空{if(S.top==S.base)return TRUE;elsereturn FALSE;}Status Pop(SqStack *S,SElemType *e){if(S->top==S->base) return ERROR;*e=*--S->top;return OK;}Status StackLength(SqStack S)//求栈的长度{return (S.top-S.base);}//队列操作Status InitQueue(LinkQueue *Q){Q->front=(QueuePtr)malloc(sizeof(QNode));Q->rear=Q->front;if(!Q->front) exit(OVERFLOW);Q->front->next=NULL;return OK;}Status EnQueue(LinkQueue *Q,char f){p=(QueuePtr)malloc(sizeof(QNode));if(!p) exit(OVERFLOW);p->b=f;p->next=NULL;Q->rear->next=p;Q->rear=p;return OK;}Status DeQueue(LinkQueue *Q,char *f){if(Q->front==Q->rear) return ERROR;p=Q->front->next;*f=p->b;Q->front->next=p->next;if(Q->rear==p)Q->rear=Q->front;free(p);return OK;}Status QueueLength(LinkQueue Q){int i=0;p=Q.front;while(Q.rear!=p){i++;p=p->next;}return i;}Status QueueEmpty(LinkQueue Q){if(Q.front==Q.rear)return TRUE;elsereturn FALSE;}void main(){int i,m;char n,a[20];InitStack(&S);InitQueue(&Q);gets(a);for(i=0; a[i]!='&'; i++) /////////// &前的数据进栈{e.a=a[i];Push(&S,e);}for(i=i+1; a[i]!='\0'; i++) ////////// ‘ &'后的数据进⼊队列EnQueue(&Q,a[i]);if( StackLength(S)!=QueueLength(Q)) /////栈和队列的数据个数不⼀样 printf("NO");elsewhile(!Stackempty(S)&&!QueueEmpty(Q))///////栈和队列⾥还有数据 {Pop(&S,&e);m=e.a;DeQueue(&Q,&f);n=f;if(m!=n){printf("NO");break;}}if(m==n&&Stackempty(S)&&QueueEmpty(Q))printf("YES");}运⾏结果:希望本⽂所述对⼤家C语⾔程序设计有所帮助。
用栈和队列判断回文

⽤栈和队列判断回⽂⽤栈和队列判断回⽂栈和队列实验⼆⼀、实验⽬的1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应⽤。
2、掌握栈和队列的特点,即后进先出和先进先出的原则。
3、掌握栈和队列的基本运算,如:⼊栈与出栈,⼊队与出队等运算在顺序存储结构和链式存储结构上的实现。
⼆、实验内容1(请简述栈的基本特性和栈的⼏种基本操作的机制栈是限定仅在表位进⾏插⼊或删除操作的线性表,栈的修改是按照后进先出的原则进⾏的,根据这种特性进⾏回⽂判断。
[问题描述]对于⼀个从键盘输⼊的字符串,判断其是否为回⽂。
回⽂即正反序相同。
如“abba”是回⽂,⽽“abab”不是回⽂。
[基本要求](1)数据从键盘读⼊;(2)输出要判断的字符串;(3)利⽤栈的基本操作对给定的字符串判断其是否是回⽂,若是则输出“该字符串是回⽂”,否则输出“该字符串不是回⽂”。
[测试数据]由学⽣任意指定。
2(设计简单的程序实现⽤栈判断回⽂#include#include#include#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef struct{char *base;char *top;int stacksize;1}SqStack;void InitStack(SqStack &S){S.base =(char *)malloc(STACK_INIT_SIZE * sizeof(char));if(!S.base)exit(0);S.top = S.base;S.stacksize = STACK_INIT_SIZE;}void Push(SqStack &S,char e){if(S.top - S.base >= S.stacksize){S.base = (char *) realloc (S.base,(S.stacksize + STACKINCREMENT) * sizeof(char)); if(!S.base) printf("存储分配失败~");S.top = S.base + S.stacksize;S.stacksize += STACKINCREMENT;}*S.top++ = e;}char Pop(SqStack &S,char &e){if(S.top == S.base) {printf("该栈为空!");printf("\n");e = 'E';}else{e = *--S.top;}return e;}void main(){2SqStack S;InitStack(S);char a[30];char c;char e;int k = 0;printf("请输⼊要转换的字符串,以#号结束:\n"); for(int i = 0;i < 30;i++){scanf("%c",&c);if(c != '#'){a[i] = c;}else{k = i;break;}}for(int h = 0 ; h < k;h++){Push(S,a[h]);}int g = 0; //定义⼀个计数器for(int w = 0;w < k;w++){char x = Pop(S,e);printf("%c",x);if(a[w] == x){//⽐较数组中的第w个值与栈中返回的第w个值是否相等g++; //若相等的话,计数器加⼀}}printf("\n");if(g == k){ //判断计数器的值与输⼊的数值是否相等printf("YES");//相等的话打印出YES}else{printf("NO");//否则打印出NO}printf("\n");}运⾏结果如下图:31(输⼊字符为回⽂字符2(输⼊字符不是回⽂字符三、编程并上机调试运⾏四、时间、地点五、指导教师,在书写过程中若出现错误~望⽼师指出~谢谢, 4。
利用链式堆栈和队列实现回文判断范文

}
*d=p->data;
}
7:撤销堆栈:
void Destory(StNode *head)
{
StNode *p,*p1;
p=head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
}
2、把对队列的创建及操作函数放到LQueue头文件下:
1:头结点结构体的创建:
二、实验目的
熟练运用堆栈和队列的各种操作,并会运用他们的特点做一些实际的应用。
二、实验步骤:
1、把堆栈的创建及操作函数放到LStack头文件下:
1:定义节点结构体:
typedef struct snode
{
DataType data;
struct snode *next;
}StNode;
2:初始化堆栈:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<windows.h>
typedef int DataType;
#include"SLNode.h"
#include"Stack.h"
void main()
{
SLNode *la,*lb;
return 0;
}
p->data=x;
p->next=head->next;
head->next=p;
return 1;
}
5:出栈函数:
int StackPop(StNode *head,DataType *d)
栈和队列实现回文

栈和队列的应用1.题目要求:输入一段字符串,判断字符串是否是回文。
回文字符串的定义:字符串的顺序输入和逆序输出内容完全一样。
要求出入一段字符串,以‘#’为结束符;判断是否是回文,并输出;2.实验目的:通过做回文的题,来熟悉栈和队列的结构以及内容,最终达到掌握3.实验步骤1.定义栈的结构体:typedef struct { //栈结构的定义int *top;int *base;int stacksize;}sqstack;2.定义队列的结构体:typedef struct queue{ //队列的结构体定义int data;struct queue *next;}queue,*que;typedef struct {que front; //队列的头指针que rear; //队列的尾指针}Q;4.相关函数:void inistack(sqstack &s)//初始化一个空栈bool just(sqstack &s)//判断栈是否为空,返回bool类型void push(sqstack &s,int e)//入栈char pop(sqstack &s,int &e)//出栈,返回char类型void initQ(Q &q)//初始化一个空队列pushq(Q &q,int e)//入队char popq(Q &q,int &e)//出队,返回char类型5.总体结构图6.运行结果分析完成对字符串的判断以及输出;7.程序清单#include<iostream.h>#include<malloc.h>typedef struct { //栈结构的定义int *top;int *base;int stacksize;}sqstack;void inistack(sqstack &s)//初始化一个空栈{s.base=(int *)malloc(100*sizeof(sqstack));if(!s.base){cout<<"分配失败"<<endl;}s.top=s.base;s.stacksize=100;}bool just(sqstack &s){bool ss=true;if(s.top==s.base)ss=false;elsess=true;return ss;}void push(sqstack &s,int e)//入栈{if(s.top-s.base>=s.stacksize){s.base=(int *)realloc(s.base,(s.stacksize+10)*sizeof(sqstack));s.top=s.base+s.stacksize;s.stacksize+=10;}*s.top++=e;}char pop(sqstack &s,int &e)//出栈{if(s.base==s.top){cout<<"栈已经为空"<<endl;}e=*--s.top;}typedef struct queue{ //队列的结构体定义int data;struct queue *next;}queue,*que;typedef struct {que front;que rear;}Q;void initQ(Q &q)//初始化一个空队列{q.front=q.rear=(queue *)malloc(sizeof(queue));if(!q.front){cout<<"分配失败"<<endl;}q.front->next=NULL;}void pushq(Q &q,int e)//入队{queue *p;p=(queue *)malloc(sizeof(queue));p->data=e;p->next=NULL;q.rear->next=p;q.rear=p;}char popq(Q &q,int &e)//出队{queue *t;if(q.front==q.rear){cout<<"队列已经为空"<<endl;}t=(queue *)malloc(sizeof(queue));t=q.front->next;e=t->data;q.front->next=t->next;if(q.rear==t)q.rear=q.front;delete t;return e;}{char ch;int e,k;bool ss=true;sqstack s;Q q;inistack(s);initQ(q);cout<<"输入字符以‘#’为结束符:"<<endl;cin>>ch;while(ch!='#'){push(s,ch);pushq(q,ch);cin>>ch;}while(just(s)){pop(s,e);popq(q,k);if(e!=k){cout<<"该字符串不是回文"<<endl;ss=false;break;}}if(ss){cout<<"该字符串是回文"<<endl;}cout<<endl;}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
栈和队列的应用
1.题目要求:
输入一段字符串,判断字符串是否是回文。
回文字符串的定义:字符串的顺序输入和逆序输出内容完全一样。
要求出入一段字符串,以‘#’为结束符;判断是否是回文,并输出;
2.实验目的:
通过做回文的题,来熟悉栈和队列的结构以及内容,最终达到掌握
3.实验步骤
1.定义栈的结构体:
typedef struct { //栈结构的定义
int *top;
int *base;
int stacksize;
}sqstack;
2.定义队列的结构体:
typedef struct queue{ //队列的结构体定义
int data;
struct queue *next;
}queue,*que;
typedef struct {
que front; //队列的头指针
que rear; //队列的尾指针
}Q;
4.相关函数:
void inistack(sqstack &s)//初始化一个空栈
bool just(sqstack &s)//判断栈是否为空,返回bool类型
void push(sqstack &s,int e)//入栈
char pop(sqstack &s,int &e)//出栈,返回char类型
void initQ(Q &q)//初始化一个空队列
pushq(Q &q,int e)//入队
char popq(Q &q,int &e)//出队,返回char类型
5.总体结构图
6.运行结果分析
完成对字符串的判断以及输出;
7.程序清单
#include<iostream.h>
#include<malloc.h>
typedef struct { //栈结构的定义
int *top;
int *base;
int stacksize;
}sqstack;
void inistack(sqstack &s)//初始化一个空栈
{
s.base=(int *)malloc(100*sizeof(sqstack));
if(!s.base)
{
cout<<"分配失败"<<endl;
}
s.top=s.base;
s.stacksize=100;
}
bool just(sqstack &s)
{
bool ss=true;
if(s.top==s.base)
ss=false;
else
ss=true;
return ss;
}
void push(sqstack &s,int e)//入栈
{
if(s.top-s.base>=s.stacksize)
{
s.base=(int *)realloc(s.base,(s.stacksize+10)*sizeof(sqstack));
s.top=s.base+s.stacksize;
s.stacksize+=10;
}
*s.top++=e;
}
char pop(sqstack &s,int &e)//出栈
{
if(s.base==s.top)
{
cout<<"栈已经为空"<<endl;
}
e=*--s.top;
}
typedef struct queue{ //队列的结构体定义int data;
struct queue *next;
}queue,*que;
typedef struct {
que front;
que rear;
}Q;
void initQ(Q &q)//初始化一个空队列
{
q.front=q.rear=(queue *)malloc(sizeof(queue));
if(!q.front)
{
cout<<"分配失败"<<endl;
}
q.front->next=NULL;
}
void pushq(Q &q,int e)//入队
{
queue *p;
p=(queue *)malloc(sizeof(queue));
p->data=e;
p->next=NULL;
q.rear->next=p;
q.rear=p;
}
char popq(Q &q,int &e)//出队
{
queue *t;
if(q.front==q.rear)
{
cout<<"队列已经为空"<<endl;
}
t=(queue *)malloc(sizeof(queue));
t=q.front->next;
e=t->data;
q.front->next=t->next;
if(q.rear==t)
q.rear=q.front;
delete t;
return e;
}
{
char ch;int e,k;
bool ss=true;
sqstack s;
Q q;
inistack(s);
initQ(q);
cout<<"输入字符以‘#’为结束符:"<<endl;
cin>>ch;
while(ch!='#')
{
push(s,ch);
pushq(q,ch);
cin>>ch;
}
while(just(s))
{
pop(s,e);
popq(q,k);
if(e!=k)
{
cout<<"该字符串不是回文"<<endl;ss=false;break;
}
}
if(ss)
{
cout<<"该字符串是回文"<<endl;
}
cout<<endl;
}。