1、 栈和队列的基本操作实现及其应用 精品资料 实验 二 栈和队列的基本操作实现及其应用 一_一、实验目的 1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。 一_二、实验内容 题目一、试写一个算法,判断依次读入的一个以@为结束符的字符序列,是否为回文。所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。 相关常量及结构定义: #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int SElemType; typedef struct
2、 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、初始化栈 /* 函
3、数功能:对栈进行初始化 。参数:栈(SqStack S)。 成功初始化返回0,否则返回-1 */ int InitStack(SqStack &S) { S.base=(SElemType *)malloc(10*sizeof(SElemType)); if(!S.base) //判断有无申请到空间 return -1; //没有申请到内存,参数失败返回-1 S.top=S.base; S.stacksize=STACK_INIT_SIZE; S.base=new SElemType; return 0; } 2
4、判断栈是否是空 /*函数功能:判断栈是否为空。参数; 栈(SqStack S)。栈为空时返回-1,不为空返回0*/ int StackEmpty(SqStack S) { if(S.top==S.base) return -1; else return 0; } 3、入栈 /*函数功能:向栈中插入元素。参数; 栈(SqStack S),元素(SElemtype e)。成功插入返回0,否则返回-1 */ int Push(SqStack &S,SElemType e) { if(S.top-S.base>=S.stacksize) { S.base=(SE
5、lemType *)realloc(S.base,(S.stacksize+1) * sizeof(SElemType)); //重新分配空间 if(!S.base) return -1; S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; //插入操作 return 0; } 4、出栈 /*函数功能:在栈中删除元素。参数;栈(SqStack S),元素(S
6、Elemtype e)。成功删除返回0,否则返回-1 */
int Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return -1;
e=*--S.top; //删除操作
return 0;
}
5、判断是否为回文
/*函数功能:判断栈中的字符串是否为回文。参数; 栈(SqStack S)。是回文时返回1,否则返回0*/
int IsReverse(SqStack &S)
{
int i;
char a;
for(i=0;i 7、f(a!=b[i]) return 0;
}
return 1;
}
一_四、函数的调用
主函数主要设计:
int lpp;
char ch;
SqStack p;
InitStack(p);
cout<<"请输入字符:";
while((ch=cin.get())&&ch!='@')
{
Push(p,ch);
b[j]=ch;
j++;
}
if (StackEmpty(p)==-1)
{
cout<<"此为空栈"< 8、if(lpp==0) cout<<"此字符串不是回文。"< 9、 SElemType;
typedef struct SqStack
{ SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(10*sizeof(SElemType));
if(!S.base) return -1;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
S.base=new SElemType;
re 10、turn 0;
}
int StackEmpty(SqStack S)
{
if(S.top==S.base) return -1;
else return 0;
}
int Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+1) * sizeof(SElemType));
if(!S.base) return -1;
S.top=S.base+S.stacksize;
11、 S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return 0;
}
int Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return -1;
e=*--S.top;
return 0;
}
int IsReverse(SqStack &S)
{
int i;
char a;
for(i=0;i 12、ain()
{
int lpp;
char ch;
SqStack p;
InitStack(p);
cout<<"请输入字符:";
while((ch=cin.get())&&ch!='@')
{
Push(p,ch);
b[j]=ch;
j++;
}
if (StackEmpty(p)==-1)
{
cout<<"此为空栈"< 13、符串是回文。"< 14、 QueuePtr rear;
int count;
}LinkQueue;
设计相关函数声明:
InitQueue(LinkQueue &Q)
EnQueue(LinkQueue &Q,QElemType e)
DeQueue(LinkQueue &Q,QElemType &e)
QueueLength(LinkQueue Q)
QueueTraverse(LinkQueue Q)
QueueFind(LinkQueue Q,QElemType e)
二_三、数据结构与核心算法的设计描述
1、初始化队列
int InitQueu 15、e(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front) return -1;
Q.front->next=NULL;
return 0;
}
2、入队列
int EnQueue(LinkQueue &Q,QElemType e)
{
QueuePtr lpp;
lpp=(QueuePtr)malloc(sizeof(QNode));
if(!lpp) return -1;
lpp->data=e; lpp->next=NULL;
if( 16、Q.front==NULL)
{
Q.front->next=lpp;
Q.rear=lpp;
}
else
{
Q.rear->next=lpp;
Q.rear=lpp;
}
return 0;
}
3、出队列
int DeQueue(LinkQueue &Q,QElemType &e)
{
QueuePtr lpp;
if(Q.front==Q.rear) return -1;
lpp=Q.front->next;
e=lpp->data;
Q.front-> 17、next=lpp->next;
if(Q.rear==lpp) Q.rear=Q.front;
delete lpp;
return 0;
}
4、统计队列的长度
int QueueLength(LinkQueue Q)
{
QueuePtr lpp=Q.front;
int i=0;
while(lpp!=Q.rear)
{
i++;
lpp=lpp->next;
}
return i;
}
5、查找队列的某个元素
int QueueFind(LinkQueue Q,QElemType e)
{
QueuePtr p;
18、 p=Q.front->next;
while(p)
{
if(p->data==e)
return 1;
p=p->next;
}
return 0;
}
6、遍历队列
int QueueTraverse(LinkQueue Q)
{
QueuePtr p;
p=Q.front->next;
while(p)
{
cout< 19、界面函数
void zhujiemian()
{
cout< 20、 】"< 21、<<"【\t\t 其他键退出 】"< 22、
{
cout<<"输入错误,必须先初始化,请重新输入:";
cin>>a;
}
cout< 23、 break;
}
if(DeQueue(Q,c)==0)
cout<<"删除成功!"< 24、th(Q);
cout<<"队列的长度为:"<>b;
if(QueueFind(Q,b)==1)
cout<<"恭喜您,队列中有您要找的元素"< 25、 }
zhujiemian();
cin>>a;
cout< 26、g。
二_六、程序清单
#include 27、ePtr)malloc(sizeof(QNode));
if(!Q.front) return -1;
Q.front->next=NULL;
return 0;
}
int EnQueue(LinkQueue &Q,QElemType e)
{
QueuePtr lpp;
lpp=(QueuePtr)malloc(sizeof(QNode));
if(!lpp) return -1;
lpp->data=e; lpp->next=NULL;
if(Q.front==NULL)
{
Q.front->next=lpp;
Q.re 28、ar=lpp;
}
else
{
Q.rear->next=lpp;
Q.rear=lpp;
}
return 0;
}
int DeQueue(LinkQueue &Q,QElemType &e)
{
QueuePtr lpp;
if(Q.front==Q.rear) return -1;
lpp=Q.front->next;
e=lpp->data;
Q.front->next=lpp->next;
if(Q.rear==lpp) Q.rear=Q.front;
de 29、lete lpp;
return 0;
}
int QueueLength(LinkQueue Q)
{
QueuePtr lpp=Q.front;
int i=0;
while(lpp!=Q.rear)
{
i++;
lpp=lpp->next;
}
return i;
}
int QueueTraverse(LinkQueue Q)
{
QueuePtr p;
p=Q.front->next;
while(p)
{
cout< 30、
}
cout< 31、"< 32、l;
cout<<"【\t\t 4 队列长度 】"< 33、"< 34、 case 1:
if(InitQueue(Q)==0)
cout<<"初始化成功!"< 35、 break;
case 3:
cout<<"输入你要入队元素"< 36、QueueFind(Q,b)==1)
cout<<"恭喜您,队列中有您要找的元素"<>a;
cout<






