《数据结构》实验报告

  • 格式:doc
  • 大小:1.48 MB
  • 文档页数:7

下载文档原格式

  / 7
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《数据结构》实验报告

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

附源程序清单:

1.

#include

#define MaxSize 100

using namespace std;

typedef int ElemType;

typedef struct

{

ElemType data[MaxSize];

int top;

}SqStack;

void InitStack(SqStack *st) //初始化栈

{

st->top=-1;

}

int StackEmpty(SqStack *st) //判断栈为空{

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

}

bool Push(SqStack *st,ElemType x) //元素进栈{

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

{

return false;

}

else

{

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

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

}

return true;

}

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

{

if(st->top==-1)

{

return false;

}

else

{

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

st->top--; //移动栈顶位置}

return true;

}

//函数名:Pushs

//功能:数组入栈

//参数:st栈名,a->数组名,i->数组个数

bool Pushs(SqStack *st,ElemType *a,int i)

{

int n=0;

for(;n

if(!Push(st,a[n]))

return false;

return true;

}

//函数名:Pops

//功能:数组出栈

//参数:a->数组名,i->数组个数

bool Pops(SqStack *st,ElemType *a,int i)

{

int n=0;

for(;n

if(!Pop(st,a[n]))

return false;

return true;

}

int main()

{

SqStack L;

SqStack *st=&L;

int i;

ElemType a[10];

int b=10;

InitStack(st);

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

a[i]=i;

if(!Pushs(st,a,b))

printf("入栈失败!!!\n");

b=8;

if(!Pops(st,a,b))

printf("出栈失败!!!\n");

for(i=0;i

{

printf("出栈元素是:%d\n",a[i]);

}

return 0;

}

2.

#include //引入栈

#include

using namespace std;

int main()

{

char a;

stacks;

a=getchar();

while(a!='$')

{

if(a=='('||a=='[')

s.push(a);

else if(a==']'||a==')')

{

if(a==')')

if(s.size()!=0&&s.top()=='(')

s.pop();

else break;

else

if(s.size()!=0&&s.top()=='[')

s.pop();

else break;

}

a=getchar();

}

if(a=='$'&&s.size()==0)

printf("括号配对成功!!");

else if(a!='$')

printf("括号配对不成功12!!");

else printf("括号配对不成功21!!%d/n",s.size());

return 0;

}