《数据结构》实验报告 实验序号:4 实验项目名称:栈的操作 学 号 姓 名 pmj 专业、班 实验地点 指导教师 实验时间 一、实验目的及要求 1
熟悉栈的基本概念; 2
掌握栈的顺序存储结构; 3.掌握栈的应用
二、实验设备(环境)及要求 微型计算机; w indow s 操作系统; Microsoft Visual Studio 6
0 集成开发环境
三、实验内容与步骤 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); } void Push(SqStack *st,ElemType x) //元素进栈 { if(st->top==MaxSize-1) { printf("栈上溢出
\n"); } else { st->top++; //移动栈顶位置 st->data[st->top]=x; //元素进栈 } } void Pop(SqStack *st,ElemType &e) //出栈 { if(st->top==-1) { printf("栈下溢出\n"); } else { e=st->data[st->top]; //元素出栈 st->top--; //移动栈顶位置 } } int main() { SqStack L; SqStack *st=&L; ElemType e; int i;