数据结构实验4.docxVIP

  • 0
  • 0
  • 约5.95千字
  • 约 13页
  • 2021-03-25 发布于山东
  • 举报
精品文档 《数据结构》实验报告 实验序号: 4 实验项目名称:栈的操作 学 号 姓 名 专业、班 实验地点 指导教师 实验时间 一、实验目的及要求 熟悉栈的基本概念; 掌握栈的顺序存储结构; 3.掌握栈的应用。 二、实验设备(环境)及要求 微型计算机; windows 操作系统; Microsoft Visual Studio 6.0 集成开发环境。 三、实验内容与步骤 栈的顺序表表示和实现的如下: #include iostream #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); } 。 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; InitStack(st); for(i=1;i10;++i) { Push(st,i); printf( 入栈元素是: %d\n,i); } for(i=1;i10;++i) { Pop(st,e); printf( 出栈元素是: %d\n,e); } return 0; 。 2欢迎下载 精品文档 } 改写以上程序,实现功能如下: 1)调用栈操作函数实现判别一个算术表达式中的圆括号配对是否正确。 2)改写 Push 和 Pop函数,使得以上两个函数可以一次性将一个数组入栈和出栈。 1) 2) 。 3欢迎下载 精品文档 2.C/C++的库函数中已经实现了栈,实例如下: #includestack // 引入栈 using namespace std; int main() { int a; stackints; scanf(%d,a); s.push(a); // 入栈 printf(%d\n,s.top()); // 取得栈顶元素输出 s.pop(); // 出栈 return 0; } 请根据以上程序,设计算法如下: 判别一个算术表达式中的圆括号和方括号配对是否正确。 。 4欢迎下载 精品文档 四、分析与讨论 五、教师评语 成绩 签名: 日期: 附源程序清单: 1. 1) #include stdio.h #define MaxSize 100 。 5欢迎下载 精品文档 #define SUCCESS 0 #define ERROR -1 typedef struct { int nData[MaxSize]; int nTop; }SqStack; void InitStack(SqStack *PST_List) // 初始化栈 { PST_List -nTop = -1; } int StackEmpty(SqStack *PST_List) // 判断栈为空 { if (PST_List -nTop == -1) return ERROR; else return SUCCESS; } void Push(SqStack *PST_List, int nData) // 元素进栈 { if (PST_List -nTop == MaxSize - 1) { printf( 栈上溢出 !\n); } else { PST_List -nTop++; // 移动栈顶位置 PST_List -nData[PST_List-nTop] = nData; // 元素进栈 } } void Pop(SqStack *PST_List, int *pnData) // 出栈 { if (PST_List -nTop == -1) { printf( 栈下溢出 \n); } 。 6欢迎下载 精品文档 else { pnData = PST

文档评论(0)

1亿VIP精品文档

相关文档