厦门理工学院数据结构实验4_第1页
厦门理工学院数据结构实验4_第2页
厦门理工学院数据结构实验4_第3页
厦门理工学院数据结构实验4_第4页
厦门理工学院数据结构实验4_第5页
已阅读5页,还剩3页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《数据结构》实验报告

实验序号:4实验项目名称:栈的操作

学号姓名专业、班

实验地点实1#514指导教师林仙丽实验时间2022-12-06

一、实验目的及要求

1.熟悉栈的基本概念;

2.掌握栈的顺序存储结构;

3.掌握栈的应用。

二、实验设备(环境)及要求

微型计算机;

windows操作系统;

MicrosoftVisualStudio6.0集成开辟环境。

三、实验内容与步骤

1.栈的顺序表表示和实现的如下:

#include<iostream>

ttdefineMaxSize100

usingnamespacestd;

typedefintElemType;

typedefstruct

(

ElemTypedata[MaxSize];

inttop;

}SqStack;

voidInitStack(SqStack*st)〃初始化栈

(

st->top=-l;

}

intStackEmpty(SqStack*st)〃判断栈为空

return(st->top==-l);

}

voidPush(SqStack*st,ElemTypex)〃元素进栈

(

if(st->top==MaxSize~l)

(

printf("栈上溢出!\n〃);

)

else

(

st->top++;〃挪移栈顶位置

st->data[st->top]=x;//元素进栈

)

}

voidPop(SqStack*st,ElemType&e)〃出栈

(

if(st->top==-l)

(

printf(〃栈下溢出\n〃);

}

else

(

e=st->data[st->top];//元素出栈

st->top-;〃挪移栈顶位置

)

)

intmain()

(

SqStackL;

SqStack*st=&L;

ElemTypee;

inti;

InitStack(st);

for(i=l;i<10;++i)

(

Push(st,i);

printf(〃入栈元素是:%d\nz,,i);

)

for(i=l;i<10;++i)

(

Pop(st,e);

printf(“出栈元素是:%d\n〃,e);

)

return0;

)

改写以上程序,实现功能如下:

改写Push和Pop函数,使得以上两个函数可以一次性将一个数组入栈和出栈。

2.C/C++的库函数中已经实现了栈,实例如下:

#include<stack>〃引入栈

usingnamespacestd;

intmain()

(

inta;

stack<int>s;

scanf(%d”,&a);

s.push(a);〃入栈

printf("d\n”,s.top());〃取得栈顶元素输出

s.pop();〃出栈

return0;

)

请根据以上程序,设计算法如下(任选一题):

1.判别一个算术表达式中的圆括号配对是否正确。

运行结果截图:

2.判别一个算术表达式中的圆括号和方括号配对是否正确。

运行结果截图:

四、分析与讨论

因为有了上一章的练习,使得栈做起来相对容易。

对上机实践结果进行分析,上机的心得体味。

五、教师评语成绩

签名:

日期:

附源程序清单:

1.

#include<stdio.h>

include<eh.h>

#include<iostream>

#defineMaxSize100

usingnamespacestd;

typedefintElemType;

typedefstruct

(

ElemTypedata[MaxSize];

inttop;

}SqStack;

voidlnitStack(SqStack*st)〃初始化栈

{

st->top=-1;

)

intStackEmpty(SqStack*st)//判断栈为空

(

return(st->top==-1);

)

voidPush(SqStack*st,ElemTypex)//元素进栈

(

if(st->top==MaxSize-1)

(

printf("栈上溢出柱n");

)

else

st->top++;//挪移栈顶位置

st->data[st->top]=x;〃元素进栈

voidPop(SqStack*st,ElemType&e)//出栈

(

if(st->top==-1)

(

printf("栈下溢出\n下

)

else

(

e=st->data[st->top];〃元素出栈

st->top-;//挪移栈顶位置

voidPush1(SqStack*st)//元素进栈

(

intx[5];

inti;

if(st->top==MaxSize-1)

(

printf("栈上溢出!\n");

)

else

(

printf("请输入5个元素:)

for(i=0;i<5;i++)

scanf("%d",&x[i]);

for(i=4;i>=0;i—)

(

st->top++;〃挪移栈顶位置

st->data[st->top]=x[i];〃元素进栈

)

}

)

voidPop1(SqStack*st)〃出栈

(

inti,x[5];

if(st->top==-1)

printf("栈下溢出\n");

}

else

for(i=0;i<5;i++)

(

x[i]=st・>data[st->top];〃元素出栈

st->top-;〃挪移栈顶位置

)

printff出栈数组为:\nH);

for(i=0;i<5;i++)

printf("%dH,x[i]);

printf(M\n");

)

)

intmain()

(

SqStackL;

SqStack*st=&L;

ElemTypee;

inti,x[5],a;

InitStack(st);

Push1(st);

Push1(st);

Push1(st);

Pop1(st);

/*for(i=1;i<10;++i)

(

Push(st,i);

printf("入栈元素是:%d\n',i);

)

for(i=1;i<10;++i)

(

Pop(st,e);

printf("出栈元素是:%d\n",e);

}*/

return0;

)

2.

#include<stack>〃引入栈

usingnamespacestd;

voidmain()

intsign=1;

chara,b='(*;

stack<char>s;

printf(”请输入算术表达式,以#为结束标志:\nn);

a=getchar();

while(a!='#>)

(

switch(a)

(

case

s.push(a);

break;

case

if(s.top()=='(')

s.pop();

else

sign=0;

br

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论