1、数据结构实验三栈和队列及其应用数据结构_实验三_栈和队列及其应用实验编号:3 四川师大数据结构实验报告 2016年10月29日 实验三 栈和队列及其应用_ 一(实验目的及要求 (1) 掌握栈和队列这两种特殊的线性表,熟悉它们的特性,在实际问题背景下灵活运用它们; (2) 本实验训练的要点是“栈”的观点及其典型用法; (3) 掌握问题求解的状态表示及其递归算法,以及由递归程序到非递归程序的转化方法。 二(实验内容 (1) 编程实现栈在两种存储结构中的基本操作(栈的初始化、判栈空、入栈、出栈等); (2) 应用栈的基本操作,实现数制转换(任意进制); (3) 编程实现队列在两种存储结构中的基本操作
2、(队列的初始化、判队列空、入队列、出队列); (4) 利用栈实现任一个表达式中的语法检查(括号的匹配)。 (5) 利用栈实现表达式的求值。 注:(1),(3)必做,(4),(5)选做。 三(主要仪器设备及软件 (1) PC机 (2) Dev C+ ,Visual C+, VS2010等 四(实验主要流程、基本操作或核心代码、算法片段(该部分如不够填写,请另加附页) (1) 编程实现栈在两种存储结构中的基本操作(栈的初始化、判栈空、入栈、出栈等); A.顺序储存: , 代码部分: /Main.cpp: #includeSStack.h int main() SqStack S; SElemTyp
3、e e; int elect=1; InitStack(S); cout 已经创建一个存放字符型的栈 elect; cout endl; switch (elect) case 1: cout e; Push(S, e); break; case 2: if(Pop(S, e) cout e is pop endl; elsecoutblankendl; break; case 3: if (StackEmpty(S) cout 栈空 endl; else cout 栈未空 endl; break; case 4: GetTop(S, e); cout e is e endl; break;
4、case 5: StackLength(S); break; case 0:break; DestroyStack(S); return OK; /SStack.cpp: #includeSStack.h /输出菜单 void Muse() cout 请选择功能: endl; cout 1.入栈 endl; cout 2.出栈 endl; cout 3.判栈空 endl; cout 4.返回栈顶部数据 endl; cout 5.栈长 endl; cout 0.退出系统 endl; cout = STACK_INIT_SIZE) S.base = (SElemType *)realloc(S.b
5、ase, (STACK_INIT_SIZE + STACKINCREMENT) * sizeof(SElemType); if (!S.base) exit(ERROR); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; *S.top+ = e; return OK; /出栈 Status Pop(SqStack &S, SElemType &e) if (S.base = S.top) return ERROR; e = *-S.top; coutpop succeedendl; return OK; /判栈空 Sta
6、tus StackEmpty(SqStack S) if (S.top = S.base) return ERROR; return OK; /销毁栈 Status DestroyStack(SqStack &S) free(S.base); S.top=NULL; S.stacksize = 0; cout 栈已销毁 endl; return OK; int StackLength(SqStack S) cout StackLength is S.top-S.base endl; return OK; /SStack.h: #include #include using namespace
7、std; const int STACK_INIT_SIZE = 100; const int STACKINCREMENT = 10; const int ERROR = 0; const int OK = 1; typedef char SElemType; typedef int Status; typedef struct SElemType *base; SElemType *top; int stacksize; SqStack; Status InitStack(SqStack &S);/创建顺序存储的栈 Status GetTop(SqStack S, SElemType &e
8、);/得到栈顶数据 Status Push(SqStack &S, SElemType &e);/入栈 Status Pop(SqStack &S, SElemType &e);/出栈 void Muse();/输出菜单界面 Status StackEmpty(SqStack S);/判断栈是否为空 Status DestroyStack(SqStack &S);/销毁栈 int StackLength(SqStack S);/计算栈的长度 , 运行结果: B. 链式储存: , 代码部分: /Main.cpp #includeLstack.h int main() Lq_Stack L; if
9、(InintStack (L) coutbuild stack succeeddata=0; L-next=NULL; return OK; Status push (Lq_Stack &L,SElemType e) /入栈 LqStack *p; p=(LqStack *)malloc(sizeof(LqStack); if(!p) exit(ERROR); p-data=e; L-data+; p-next=L-next; L-next=p; return OK; Status pop (Lq_Stack &L,SElemType &e) /出栈 LqStack *p; if(L-next
10、=NULL) return ERROR; p=L-next; e=p-data; L-next=p-next; L-data-; free(p); return OK; Status GetTop(Lq_Stack L, SElemType &e) /得到栈顶数据 if(L-next=NULL) return ERROR; e=L-next-data; return OK; Status StackEmpty(Lq_Stack L) /判断栈是否为空 if(L-next=NULL)return ERROR; else return OK; int StackLength(Lq_Stack L)
11、 /计算栈的长度 return L-data; Status DestroyStack(Lq_Stack &L) /销毁栈 LqStack *p; while(!L) L=p; L=L-next; free(p); return OK; void Menu(Lq_Stack &L,SElemType e) /输出菜单选择执行的功能 int select=1; while(select) coutendl; cout请选择功能endl; cout1.入栈endl; cout2.出栈endl; cout3.得到顶部数据endl; cout4.判断栈是否为空endl; cout5.输出栈的长度end
12、l; cout0.退出程序endl; coutselect; switch (select) case 0:break; case 1: coute; if(push(L,e) coutpush succeedendl; else coutpush failedendl; break; case 2: if(pop(L,e) coutdata e is popendl; else coutpop failedendl; break; case 3: if(GetTop(L,e) couthead data e is popendl; else coutGet failedendl; break
13、; case 4: if(StackEmpty(L) coutstack is not NULLendl; else coutstack is NULLendl; break; case 5: coutthis stack length is StackLength(L)endl; break; /Lstack.h #include #include using namespace std; const int OK=1; const int ERROR=0; typedef int SElemType; typedef int Status; typedef struct LqStack S
14、ElemType data; struct LqStack *next; LqStack,*Lq_Stack; Status InintStack (Lq_Stack &L);/创建栈 Status push (Lq_Stack &L,SElemType e);/入栈 Status pop (Lq_Stack &L,SElemType &e);/出栈 Status GetTop(Lq_Stack L, SElemType &e);/得到栈顶数据 Status StackEmpty(Lq_Stack L);/判断栈是否为空 int StackLength(Lq_Stack L);/计算栈的长度
15、Status DestroyStack(Lq_Stack &L);/销毁栈 void Menu(Lq_Stack &L,SElemType e);/输出菜单选择执行的功能 , 运行结果: (2) 应用栈的基本操作,实现数制转换(任意进制); , 代码部分: /Main.cpp #includeSStack.h int main() int number; coutnumber; conversion(number); return 0; SStack.cpp #includeSStack.h Status InitStack(SStack &S) /创建栈 S.dase=(ElemType *
16、)malloc(STACK_INIT_SIZE * sizeof(ElemType); if (!S.dase) exit(ERROR); S.top=S.dase; S.stacksize=STACK_INIT_SIZE; return OK; Status push(SStack &S,ElemType e) /入栈 if(S.top-S.dase = S.stacksize)/栈满追加空间 S.dase=(ElemType *)realloc(S.dase, (STACK_INIT_SIZE+STACKINCREMENT) * sizeof(ElemType); if(!S.dase)
17、exit(ERROR); S.top=S.dase+STACK_INIT_SIZE; S.stacksize+=STACKINCREMENT; *S.top+=e; return OK; Status pop(SStack &S,ElemType &e) /出栈 if(S.top= S.dase) return ERROR; e=*-S.top; return OK; Status StackEmpty(SStack &S) /判断栈是否为空 if(S.dase=S.top) return ERROR; return OK; void conversion(int number) /转换为e进
18、制并输出 SStack S; int N,e; if(InitStack(S) cout栈创建成功endl; coutN; while(N) push(S,N%number); N=N/number; while(StackEmpty(S) pop(S,e); coute; coutendl; /SStack.h #ifndef SSTACK_H #define SSTACK_H #include #include using namespace std; const int STACK_INIT_SIZE=100; const int STACKINCREMENT=10; const int
19、 OK=1; const int ERROR=0; typedef int Status; typedef int ElemType; typedef struct ElemType *dase; ElemType *top; int stacksize; SStack; Status InitStack(SStack &S);/创建栈 Status push(SStack &S,ElemType e);/入栈 Status push(SStack &S,ElemType &e);/出栈 Status StackEmpty(SStack &S);/判断栈是否为空 void conversion
20、(int number);/转换为number进制并输出 #endif , 运行结果: (3) 编程实现队列在两种存储结构中的基本操作(队列的初始化、判队列空、入队列、出队列)。 , 代码部分: A:链式储存: /Main.cpp: #includeQNode.h int main() LinkQueue Q; InitQueue(Q); Menu(Q); DestroyQueue(Q); return 0; /QNode.cpp: #includeQNode.h Status InitQueue(LinkQueue &Q) /构造空队列 Q.front=Q.rear=(QueuePrt)ma
21、lloc(sizeof(QNode); if(!Q.front) exit (ERROR); Q.front-next=NULL; return OK; Status DestroyQueue(LinkQueue &Q) /销毁队列Q while(Q.front) Q.rear=Q.front-next; free(Q.front); Q.front=Q.rear; return OK; Status EnQueue (LinkQueue &Q,QElemType e) /插入元素a为Q的新的队尾元素 QNode *p; p=(QueuePrt)malloc(sizeof(QNode); if
22、(!p) exit(ERROR); p-data=e;p-next=NULL; Q.rear-next=p; Q.rear=p; return OK; Status DeQueue (LinkQueue &Q,QElemType &e) /删除Q的队头元素,用e返回其值 QNode *p; if(Q.front=Q.rear) return ERROR; p = Q.front-next; e=p-data; Q.front-next=p-next; if (Q.rear=p) Q.rear=Q.front; free(p); return OK; Status QueueEmpty(Link
23、Queue Q) /判断对是否为空 if(Q.front=Q.rear)return ERROR; return OK; void Menu(LinkQueue &Q) /输出选择界面 int select=1; QElemType e; while(select) cout-endl; cout请选择以下功能:endl; cout-1,入队endl; cout-2,出队endl; cout-3,判断队空endl; cout-0,退出程序endl; coutselect; switch (select) case 0:break; case 1: cout输入入队数据e; if(EnQueue
24、(Q,e) cout入队成功endl; break; case 2: if(DeQueue(Q,e) coute出队endl; break; case 3: if(QueueEmpty(Q) cout此队不为空endl; else cout此队为空endl; break; /QNode.h #ifndef QNODE_H #define QNODE_H #include #include using namespace std; const int OK=1; const int ERROR=0; typedef int QElemType; typedef int Status; typedef struct QNode QElemType data; struct QNode * next; QNode,*QueuePrt; typedef struct QueuePrt front; QueuePrt rear; LinkQueue; Status InitQueue(LinkQueue &Q);/构造空队列 Status DestroyQueue(LinkQueue &Q);/销毁队列Q Status EnQueu
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1