1、武汉理工数据结构实验2 栈和队列基本操作和应用实验2 栈和队列的基本操作和应用1实验目的(1) 熟练掌握顺序栈的基本操作。(2) 掌握顺序栈的应用。(3) 掌握顺序循环队列的基本操作。(4) 掌握链式队列的基本操作。2实验内容(1) 设计一个顺序栈的基本操作的演示程序;(2) 利用顺序栈,进行整数的不同进制之间的转换;(3) 设计一个顺序循环队列的基本操作演示程序;(4) 设计一个链式队列的基本操作演示程序。【基本要求】I. 实验内容(1)的基本要求:编写一个程序,将一个顺序栈的元素依次取出,并打印其元素值。II. 实验内容(2)的基本要求:编写一个程序,将一个非负的十进制整数转换成二进制。I
2、II. 实验内容(3)的基本要求:编写一个程序,将一个顺序队列的元素依次取出,并打印其元素值。IV. 实验内容(4)的基本要求:编写一个程序,将一个链式队列的元素依次取出,并打印其元素值。 【测试数据】自定3实验结果 按照学校实验格式要求撰写实验报告,内容主要包括1)实验目的;2)实验内容;3)实验环境和方法;4)实验过程描述;5)实验心得体会参考程序如下:实验内容(1)参考程序/*sqStack.h文件 */#define INIT_SIZE 100#define INCREMENT 10typedef int ElemType;/typedef char ElemType;typedef
3、struct SqStack ElemType *base; ElemType *top; int stacksize;SqStack;enum Status OK, ERROR, OVERFLOW;/*sqStackOp.h文件 */#include sqStack.hStatus InitStack(SqStack &S) ;Status GetTop(SqStack S,ElemType &e);Status Push(SqStack &S,ElemType e);Status Pop(SqStack &S,ElemType &e);bool StackEmpty(SqStack &S)
4、; /*sqStackOp.cpp文件 */#include #include #include sqStackOp.hStatus InitStack(SqStack &S) /构造一个空的栈 S.base=(ElemType*)malloc(INIT_SIZE*sizeof(ElemType); if(! S.base) exit(OVERFLOW); /存储分配失败 S.top=S.base; S.stacksize=INIT_SIZE; return OK; /InitStackStatus GetTop(SqStack S,ElemType &e)/若栈不空,则用e返回S的栈顶元素,
5、并返回OK;否则返回ERROR if(S.top=S.base) return ERROR; e=*(S.top-1); return OK; /GetTopStatus Push(SqStack &S,ElemType e)/插入元素e为新的栈顶元素 if(S.top-S.base=S.stacksize) /栈满,追加存储空间 S.base=(ElemType *)realloc(S.base,(S.stacksize+INCREMENT)*sizeof(ElemType); if(!S.base)exit(OVERFLOW); /存储分配失败 S.top=S.base+S.stacksi
6、ze; S.stacksize+=INCREMENT; *S.top+=e; return OK; /PushStatus Pop(SqStack &S,ElemType &e)/若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR if(S.top=S.base) return ERROR; e=*(-S.top); return OK; /Push/判断栈是否为空bool StackEmpty(SqStack &S) if(S.top = S.base) return true; else return false;/*main.cpp文件 */#include #i
7、nclude #include sqStackOp.hvoid main() printf(Hellow stack n); SqStack S; /定义顺序栈S if(OK != InitStack(S) printf(顺序栈初始化出错,退出.n); exit(-1); Push(S, 1); Push(S,2); Push(S,3); int e; Pop(S, e); printf(出栈元素 = %d n,e); Push(S,4); Push(S,5); while(!StackEmpty(S) Pop(S, e); printf(出栈元素 = %d n,e); /* SqStack
8、S; char x,y; InitStack(S); x=c;y=k; Push(S,x); Push(S,a); Push(S,y); Pop(S,x); Push(S,t); Push(S,x); Pop(S,x); Push(S,s); while(!StackEmpty(S) Pop(S,y);printf(%c ,y); ; printf(%c ,x);*/ getchar();实验内容(2)参考程序/*sqStack.h文件 */#define INIT_SIZE 100#define INCREMENT 10typedef int ElemType;typedef struct
9、SqStack ElemType *base; ElemType *top; int stacksize;SqStack;enum Status OK, ERROR, OVERFLOW;/*sqStackOp.h文件 */#include sqStack.hStatus InitStack(SqStack &S) ;Status GetTop(SqStack S,ElemType &e);Status Push(SqStack &S,ElemType e);Status Pop(SqStack &S,ElemType &e);bool StackEmpty(SqStack &S);/*sqSt
10、ackOp.cpp文件 */#include #include #include sqStackOp.hStatus InitStack(SqStack &S) /构造一个空的栈 S.base=(ElemType*)malloc(INIT_SIZE*sizeof(ElemType); if(! S.base) exit(OVERFLOW); /存储分配失败 S.top=S.base; S.stacksize=INIT_SIZE; return OK; /InitStackStatus GetTop(SqStack S,ElemType &e)/若栈不空,则用e返回S的栈顶元素,并返回OK;否则
11、返回ERROR if(S.top=S.base) return ERROR; e=*(S.top-1); return OK; /GetTopStatus Push(SqStack &S,ElemType e)/插入元素e为新的栈顶元素 if(S.top-S.base=S.stacksize) /栈满,追加存储空间 S.base=(ElemType *)realloc(S.base,(S.stacksize+INCREMENT)*sizeof(ElemType); if(!S.base)exit(OVERFLOW); /存储分配失败 S.top=S.base+S.stacksize; S.st
12、acksize+=INCREMENT; *S.top+=e; return OK; /PushStatus Pop(SqStack &S,ElemType &e)/若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR if(S.top=S.base) return ERROR; e=*(-S.top); return OK; /Push/判断栈是否为空bool StackEmpty(SqStack &S) if(S.top = S.base) return true; else return false;/*main.cpp文件 */#include #include #
13、include sqStackOp.hvoid main() SqStack s; int x; InitStack(s); scanf(%d,&x); /%d-十进制输入;%O-八进制输入; %x-十六进制输入 /修改这里输入进制和下面整除和余数计算,就可以获得其他进制的转换 while(x!=0) Push(s,x%8); x=x/8; while(!StackEmpty(s) Pop(s,x); printf(%d ,x); printf(n); getchar();实验内容(3)参考程序/*sqQueue.h 文件*/#define MAXQSIZE 100typedef int QE
14、lemType;typedef struct SqQueue QElemType *base; int front; int rear;SqQueue;enum Status OK, ERROR, OVERFLOW;/*sqQueueOp.h 文件*/#include sqQueue.hStatus InitQueue (SqQueue &Q) ;Status EnQueue (SqQueue &Q, QElemType e);Status DeQueue (SqQueue &Q, QElemType &e) ;bool QueueEmpty(SqQueue &Q);int QueueLeng
15、th(SqQueue Q);/*sqQueueOp.cpp 文件*/#include #include #include sqQueueOp.h Status InitQueue (SqQueue &Q) / 构造一个空队列Q Q.base = (QElemType *) malloc (MAXQSIZE *sizeof (QElemType); if (!Q.base) exit (OVERFLOW); / 存储分配失败 Q.front = Q.rear = 0; return OK; Status EnQueue (SqQueue &Q, QElemType e) / 插入元素e为Q的新的
16、队尾元素 if (Q.rear+1) % MAXQSIZE = Q.front) return ERROR; /队列满 Q.baseQ.rear = e; Q.rear = (Q.rear+1) % MAXQSIZE; return OK; Status DeQueue (SqQueue &Q, QElemType &e) / 若队列不空,则删除Q的队头元素, / 用e返回其值,并返回OK; 否则返回ERROR if (Q.front = Q.rear) return ERROR; e = Q.baseQ.front; Q.front = (Q.front+1) % MAXQSIZE; ret
17、urn OK;/判断队列是否为空bool QueueEmpty(SqQueue &Q) if(Q.front= Q.rear) return true; else return false;/计算循环队列长度int QueueLength(SqQueue Q) return (Q.rear - Q.front + MAXQSIZE) % MAXQSIZE;/*main.cpp 文件*/#include #include #include sqQueueOp.hvoid main() printf(Hello Queue n); SqQueue Q; /定义顺序队列Q QElemType e;
18、if(OK != InitQueue(Q) printf(顺序队列初始化出错,退出.n); exit(-1); EnQueue(Q,1); EnQueue(Q,3); EnQueue(Q,5); EnQueue(Q,7); printf(当前队列长度 = %d n,QueueLength(Q); DeQueue(Q,e); printf(队首元素%d出队,当前队列长度=%dn,e,QueueLength(Q); EnQueue(Q,9); EnQueue(Q,11); while(!QueueEmpty(Q) DeQueue(Q,e); printf(队首元素%d出队,当前队列长度=%dn,e
19、,QueueLength(Q); getchar();实验内容(4)参考程序/*linkQueue.h 文件*/typedef int QElemType; typedef struct QNode / 结点类型 QElemType data; struct QNode *next; QNode, *QueuePtr; typedef struct / 链队列类型 QueuePtr front; / 队头指针 QueuePtr rear; / 队尾指针 LinkQueue;enum Status OK, ERROR, OVERFLOW;/*linkQueueOp.h 文件*/#include
20、linkQueue.hStatus InitQueue (LinkQueue &Q) ;Status EnQueue (LinkQueue &Q, QElemType e);Status DeQueue (LinkQueue &Q, QElemType &e) ;bool QueueEmpty(LinkQueue &Q);/*linkQueueOp.cpp 文件*/#include #include #include linkQueueOp.hStatus InitQueue (LinkQueue &Q) / 构造一个空队列Q Q.front = Q.rear = (QueuePtr)mall
21、oc(sizeof(QNode); if (!Q.front) exit (OVERFLOW); /存储分配失败 Q.front-next = NULL; return OK; Status EnQueue (LinkQueue &Q, QElemType e) / 插入元素e为Q的新的队尾元素 QueuePtr p = (QueuePtr) malloc (sizeof (QNode); if (!p) exit (OVERFLOW); /存储分配失败 p-data = e; p-next = NULL; Q.rear-next = p; Q.rear = p; return OK; Sta
22、tus DeQueue (LinkQueue &Q, QElemType &e) / 若队列不空,则删除Q的队头元素, /用 e 返回其值,并返回OK;否则返回ERROR if (Q.front = Q.rear) return ERROR; QueuePtr p = Q.front-next; e = p-data; Q.front-next = p-next; if (Q.rear = p) Q.rear = Q.front; free (p); return OK;/判断队列是否为空bool QueueEmpty(LinkQueue &Q) if(Q.front = Q.rear) re
23、turn true; else return false;/*main.cpp 文件*/#include #include #include linkQueueOp.hvoid main() printf(Hello LinkQueue n); LinkQueue Q; /定义顺序队列Q QElemType e; if(OK != InitQueue(Q) printf(顺序队列初始化出错,退出.n); exit(-1); EnQueue(Q,1); EnQueue(Q,3); EnQueue(Q,5); EnQueue(Q,7); DeQueue(Q,e); printf(队首元素%d出队,n,e); EnQueue(Q,9); EnQueue(Q,11); while(!QueueEmpty(Q) DeQueue(Q,e); printf(队首元素%d出队,n,e); getchar();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1