栈和队列的基本操作实现及其应用资料Word格式文档下载.docx

上传人:b****1 文档编号:13395481 上传时间:2022-10-10 格式:DOCX 页数:19 大小:18.72KB
下载 相关 举报
栈和队列的基本操作实现及其应用资料Word格式文档下载.docx_第1页
第1页 / 共19页
栈和队列的基本操作实现及其应用资料Word格式文档下载.docx_第2页
第2页 / 共19页
栈和队列的基本操作实现及其应用资料Word格式文档下载.docx_第3页
第3页 / 共19页
栈和队列的基本操作实现及其应用资料Word格式文档下载.docx_第4页
第4页 / 共19页
栈和队列的基本操作实现及其应用资料Word格式文档下载.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

栈和队列的基本操作实现及其应用资料Word格式文档下载.docx

《栈和队列的基本操作实现及其应用资料Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《栈和队列的基本操作实现及其应用资料Word格式文档下载.docx(19页珍藏版)》请在冰豆网上搜索。

栈和队列的基本操作实现及其应用资料Word格式文档下载.docx

栈:

intInitStack(SqStack&

S)

intPush(SqStack&

S,SElemTypee)

intPop(SqStack&

S,SElemType&

e)

intStackEmpty(s)

一_三、数据结构与核心算法的设计描述

1、初始化栈

/*函数功能:

对栈进行初始化。

参数:

栈(SqStackS)。

成功初始化返回0,否则返回-1*/

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*/

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;

//插入操作

4、出栈

在栈中删除元素。

栈(SqStackS),元素(SElemtypee)。

成功删除返回0,否则返回-1*/

intPop(SqStack&

if(S.top==S.base)return-1;

e=*--S.top;

//删除操作

5、判断是否为回文

判断栈中的字符串是否为回文。

是回文时返回1,否则返回0*/

intIsReverse(SqStack&

inti;

chara;

for(i=0;

i<

j;

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<

此为空栈"

endl;

return0;

lpp=IsReverse(p);

if(lpp==0)cout<

此字符串不是回文。

elsecout<

此字符串是回文。

一_五、实验总结

通过这次试验我熟悉了对栈的基本操作,对基本的栈操作有了很好的掌握,知道自己容易在什么地方出错,。

一_六、程序清单

#include<

iostream>

usingnamespacestd;

charb[STACK_INIT_SIZE+STACKINCREMENT];

intj=0;

typedefcharSElemType;

S.base)return-1;

intmain()

intlpp;

二_一、实验目的

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&

QueueLength(LinkQueueQ)

QueueTraverse(LinkQueueQ)

QueueFind(LinkQueueQ,QElemTypee)

二_三、数据结构与核心算法的设计描述

1、初始化队列

intInitQueue(LinkQueue&

Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));

Q.front)return-1;

Q.front->

next=NULL;

2、入队列

intEnQueue(LinkQueue&

QueuePtrlpp;

lpp=(QueuePtr)malloc(sizeof(QNode));

lpp)return-1;

lpp->

data=e;

if(Q.front==NULL)

Q.front->

next=lpp;

Q.rear=lpp;

else

Q.rear->

3、出队列

intDeQueue(LinkQueue&

if(Q.front==Q.rear)return-1;

lpp=Q.front->

next;

e=lpp->

data;

next=lpp->

if(Q.rear==lpp)Q.rear=Q.front;

deletelpp;

4、统计队列的长度

intQueueLength(LinkQueueQ)

QueuePtrlpp=Q.front;

inti=0;

while(lpp!

=Q.rear)

i++;

lpp=lpp->

returni;

5、查找队列的某个元素

intQueueFind(LinkQueueQ,QElemTypee)

QueuePtrp;

p=Q.front->

while(p)

if(p->

data==e)

return1;

p=p->

6、遍历队列

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1