文档介绍:数据结构与算法实现实验报告
实验一
1.实验题目
顺序栈的定义及其操作算法的实现
2.需求分析
本演示程序用C++编写,编程实现顺序栈表的类型定义及顺序表的初始化操作、入栈操作、出栈操作、取栈顶元素操作、输出操作等,并对其进行验证======"<<endl;
Push(S,7);Push(S,14);Push(S,21);Push(S,28);
cout<<"栈中元素分别为:"<<endl;
StackOutput(S);Push(S,35);
cout<<"元素入栈之后栈中元素为:"<<endl;
StackOutput(S);Push(S,42);
cout<<"元素入栈之后栈中元素为:"<<endl;
StackOutput(S);Pop(S, e);
cout<<"元素出栈之后栈中元素为:"<<endl;
StackOutput(S);GetTop(S,e);
cout<<"取栈顶元素为:"<<endl;
cout<<e<<endl;
cout<<"取栈顶元素之后栈中元素为:"<<endl;
StackOutput(S);Pop(S, e);
cout<<"元素出栈之后栈中元素为:"<<endl;
StackOutput(S);
system("pause");
}
5.调试分析
#include<iostream>
using namespace std;
typedef int Status;
#define Init_Size 100
#define INCR 20
typedef int ElemType;
typedef struct
{ElemType *Elem;
int Top;
int StackSize;
}SqStack;
void InitStack(SqStack &S)
{ =(ElemType *)malloc(Init_Size*sizeof(ElemType));
if(!) cout<<"OVERFLOW";
=0;
}
void GetTop(SqStack S, ElemType &e)
{if (==0) cout<<"ERROR";
e=[-1];
}
void Push(SqStack &S, ElemType e)
{ if(==)
{ =(ElemType*)malloc((+INCR)*sizeof(ElemType));
if(!) cout<<"OVERFLOW";
+=INCR;
}
[++]=e;
}
void Pop(SqStack &S,ElemType &e)
{if (!)
{cout<<"栈为空"<<endl;cout<<"ERROR";}
e=[--];
}
void StackOutput(SqStack S)
{ int i;
for(i=0;i<;i++)
cout<<[i]<<" ";
cout<<endl;
}
int main()
{ SqStack S;
ElemType e;
InitStack(S);
cout<<"===================================="<<endl;
cout<<"顺序栈实验"<<endl;
cout<<"===================================="<<endl;
Push(S,7);Push(S,14);Push(S,21);Push(S,28);
cout<<"栈中元素分别为:"<<endl;
StackOutput(S);Push(S,35);
cout<<"元素入栈之后栈中元素为:"<<endl;
StackOutput(S);Push(S,42);
cout<<"元素入栈之后栈中元素为:"<<endl;
StackOutput(S);Pop(S, e);
cout<<"元素出栈之后栈中元素为:"<<endl