文档介绍:实验二堆栈和队列的基本操作一、上机目的:1、掌握栈的思想及其存储实现,掌握栈,队列的类型定义方法。2、明确栈是特殊的线性表。3、掌握栈的常见算法的程序实现:初始化栈、判栈为空、出栈、入栈,出队入队等运算。4、掌握顺序栈,链栈,队列的简单应用。5、理解栈,队列的算法思想,能够根据实际情况选择合适的存储结构。二、上机要求:1、认真阅读和掌握本实验的算法。2、上机将本算法实现。3、保存和打印出程序的运行结果,并结合程序进行分析。三、上机内容:堆栈(包括顺序结构、链式结构)和队列的基本操作:初始化栈、判断栈空、出栈、入栈等运算。(详细设计):顺序栈的基本操作:#include"String"typedefcharElemType;#defineStackSize100 /*顺序栈的初始分配空间*/typedefstruct{ ElemTypedata[StackSize]; /*保存栈中元素*/ inttop; /*栈顶指针*/}SqStack;voidInitStack(SqStack&st) { =-1;}intPush(SqStack&st,ElemTypex) /*进栈运算*/{ if(==StackSize-1) /*栈满*/ return0; else /*栈不满*/ { ++; []=x; return1; }}intPop(SqStack&st,ElemType&x)/*出栈运算*/{ if(==-1) /*栈空*/ return0; else /*栈不空*/ { x=[]; --; return1; }}intGetTop(SqStackst,ElemType&x) /*取栈顶元素*/{ if(==-1) /*栈空*/ return0; else { x=[]; return1; }}intStackEmpty(SqStackst)/*判断栈空运算*/{ if(==-1) /*栈空*/ return1; else /*栈不空*/ return0;}voidmain(){ SqStackst; ElemTypee; InitStack(st); printf("栈%s\n",(StackEmpty(st)==1?"空":"不空")); printf("a进栈\n");Push(st,'a'); printf("b进栈\n");Push(st,'b'); printf("c进栈\n");Push(st,'c'); printf("d进栈\n");Push(st,'d'); printf("栈%s\n",(StackEmpty(st)==1?"空":"不空")); GetTop(st,e); printf("栈顶元素:%c\n",e); printf("出栈次序:"); while(!StackEmpty(st)) { Pop(st,e); printf("%c",e); } printf("\n");}运行结果链栈的基本操作:#include<>#include<>typedefintElemType;typedefstructlsnode{E