栈和队列的基本操作实现及其应用资料.docx
- 文档编号:467617
- 上传时间:2022-10-10
- 格式:DOCX
- 页数:19
- 大小:18.72KB
栈和队列的基本操作实现及其应用资料.docx
《栈和队列的基本操作实现及其应用资料.docx》由会员分享,可在线阅读,更多相关《栈和队列的基本操作实现及其应用资料.docx(19页珍藏版)》请在冰豆网上搜索。
栈和队列的基本操作实现及其应用资料
栈和队列的基本操作实现及其应用
实验二栈和队列的基本操作实现及其应用
一_一、实验目的
1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。
一_二、实验内容
题目一、试写一个算法,判断依次读入的一个以@为结束符的字符序列,是否为回文。
所谓“回文“是指正向读和反向读都一样的一字符串,如“321123”或“ableelba”。
相关常量及结构定义:
#defineSTACK_INIT_SIZE100
#defineSTACKINCREMENT10
typedefintSElemType;
typedefstructSqStack
{SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
设计相关函数声明:
判断函数:
intIsReverse()
栈:
intInitStack(SqStack&S)
intPush(SqStack&S,SElemTypee)
intPop(SqStack&S,SElemType&e)
intStackEmpty(s)
一_三、数据结构与核心算法的设计描述
1、初始化栈
/*函数功能:
对栈进行初始化。
参数:
栈(SqStackS)。
成功初始化返回0,否则返回-1*/
intInitStack(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=newSElemType;
return0;
}
2、判断栈是否是空
/*函数功能:
判断栈是否为空。
参数;栈(SqStackS)。
栈为空时返回-1,不为空返回0*/
intStackEmpty(SqStackS)
{
if(S.top==S.base)return-1;
elsereturn0;
}
3、入栈
/*函数功能:
向栈中插入元素。
参数;栈(SqStackS),元素(SElemtypee)。
成功插入返回0,否则返回-1*/
intPush(SqStack&S,SElemTypee)
{
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;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;//插入操作
return0;
}
4、出栈
/*函数功能:
在栈中删除元素。
参数;栈(SqStackS),元素(SElemtypee)。
成功删除返回0,否则返回-1*/
intPop(SqStack&S,SElemType&e)
{
if(S.top==S.base)return-1;
e=*--S.top;//删除操作
return0;
}
5、判断是否为回文
/*函数功能:
判断栈中的字符串是否为回文。
参数;栈(SqStackS)。
是回文时返回1,否则返回0*/
intIsReverse(SqStack&S)
{
inti;
chara;
for(i=0;i { Pop(S,a); if(a! =b[i])return0; } return1; } 一_四、函数的调用 主函数主要设计: intlpp; charch; SqStackp; InitStack(p); cout<<"请输入字符: "; while((ch=cin.get())&&ch! ='@') { Push(p,ch); b[j]=ch; j++; } if(StackEmpty(p)==-1) { cout<<"此为空栈"< return0; } lpp=IsReverse(p); if(lpp==0)cout<<"此字符串不是回文。 "< elsecout<<"此字符串是回文。 "< 一_五、实验总结 通过这次试验我熟悉了对栈的基本操作,对基本的栈操作有了很好的掌握,知道自己容易在什么地方出错,。 一_六、程序清单 #include usingnamespacestd; #defineSTACK_INIT_SIZE100 #defineSTACKINCREMENT10 charb[STACK_INIT_SIZE+STACKINCREMENT]; intj=0; typedefcharSElemType; typedefstructSqStack {SElemType*base; SElemType*top; intstacksize; }SqStack; intInitStack(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=newSElemType; return0; } intStackEmpty(SqStackS) { if(S.top==S.base)return-1; elsereturn0; } intPush(SqStack&S,SElemTypee) { 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; S.stacksize+=STACKINCREMENT; } *S.top++=e; return0; } intPop(SqStack&S,SElemType&e) { if(S.top==S.base)return-1; e=*--S.top; return0; } intIsReverse(SqStack&S) { inti; chara; for(i=0;i { Pop(S,a); if(a! =b[i])return0; } return1; } intmain() { intlpp; charch; SqStackp; InitStack(p); cout<<"请输入字符: "; while((ch=cin.get())&&ch! ='@') { Push(p,ch); b[j]=ch; j++; } if(StackEmpty(p)==-1) { cout<<"此为空栈"< return0; } lpp=IsReverse(p); if(lpp==0)cout<<"此字符串不是回文。 "< elsecout<<"此字符串是回文。 "< return0; } 二_一、实验目的 2、会用栈和队列解决简单的实际问题。 二_二、实验内容 题目二、编程模拟队列的管理,主要包括: 出队列、入队、统计队列的长度、查找队列某个元素e、及输出队列中元素。 相关常量及结构定义: typedefintQElemType; typedefstructQNode { QElemTypedata; structQNode*next; }QNode,*QueuePtr; typedefstruct { QueuePtrfront; QueuePtrrear; intcount; }LinkQueue; 设计相关函数声明: InitQueue(LinkQueue&Q) EnQueue(LinkQueue&Q,QElemTypee) DeQueue(LinkQueue&Q,QElemType&e) QueueLength(LinkQueueQ) QueueTraverse(LinkQueueQ) QueueFind(LinkQueueQ,QElemTypee) 二_三、数据结构与核心算法的设计描述 1、初始化队列 intInitQueue(LinkQueue&Q) { Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); if(! Q.front)return-1; Q.front->next=NULL; return0; } 2、入队列 intEnQueue(LinkQueue&Q,QElemTypee) { QueuePtrlpp; lpp=(QueuePtr)malloc(sizeof(QNode)); if(! lpp)return-1; lpp->data=e;lpp->next=NULL; if(Q.front==NULL) { Q.front->next=lpp; Q.rear=lpp; } else { Q.rear->next=lpp; Q.rear=lpp; } return0; } 3、出队列 intDeQueue(LinkQueue&Q,QElemType&e) { QueuePtrlpp; 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; deletelpp; return0; } 4、统计队列的长度 intQueueLength(LinkQueueQ) { QueuePtrlpp=Q.front; inti=0; while(lpp! =Q.rear) { i++; lpp=lpp->next; } returni; } 5、查找队列的某个元素 intQueueFind(LinkQueueQ,QElemTypee) { QueuePtrp; p=Q.front->next; while(p) { if(p->data==e) return1; p=p->next; } return0; } 6、遍历队列
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 队列 基本 操作 实现 及其 应用 资料