用顺序结构表示栈并实现栈的各种基本操作_第1页
用顺序结构表示栈并实现栈的各种基本操作_第2页
用顺序结构表示栈并实现栈的各种基本操作_第3页
用顺序结构表示栈并实现栈的各种基本操作_第4页
用顺序结构表示栈并实现栈的各种基本操作_第5页
已阅读5页,还剩19页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 基础实验 实验目的 (1)掌握栈的顺序表示和实现 (2)掌握栈的链式表示和实现 (3)掌握队列的顺序表示和实现 (4)掌握队列的链式表示和实现 实验内容 实验一:栈的顺序表示和实现 【实验内容与要求】 编写一个程序实现顺序栈的各种基本运算,并在此基础上设计一个主程序,完成如下功能: (1)初始化顺序栈 (2)插入元素 (3)删除栈顶元素 (4)取栈顶元素 (5)遍历顺序栈 (6)置空顺序栈 【知识要点】 栈的顺序存储结构简称为顺序栈,它是运算受限的顺序表。 对于顺序栈,入栈时,首先判断栈是否为满,栈满的条件为:p-top= =MAXNUM-1,栈满时,不能入栈;否则出现空间溢出,引起错误,这

2、种现象称为上溢。 出栈和读栈顶元素操作,先判栈是否为空,为空时不能操作,否则产生错误。通常栈空作为一种控制转移的条件。 注意: (1)顺序栈中元素用向量存放 (2)栈底位置是固定不变的,可设置在向量两端的任意一个端点 (3)栈顶位置是随着进栈和退栈操作而变化的,用一个整型量top(通常称top为栈顶指针)来指示当前栈顶位置 【实现提示】 /*定义顺序栈的存储结构*/ typedef struct ElemType stackMAXNUM; int top;SqStack; /*初始化顺序栈函数*/ void InitStack(SqStack *p) q=(SqStack*)malloc(si

3、zeof(SqStack) /*申请空间*/) /*入栈函数*/ void Push(SqStack *p,ElemType x) if(p-toptop=p-top+1; /*栈顶+1*/ p-stackp-top=x; /*数据入栈*/ /*出栈函数*/ ElemType Pop(SqStack *p) x=p-stackp-top; /*将栈顶元素赋给x*/ p-top=p-top-1; /*栈顶-1*/ /*获取栈顶元素函数*/ ElemType GetTop(SqStack *p) x=p-stackp-top; /*遍历顺序栈函数*/ void OutStack(SqStack *

4、p) for(i=p-top;i=0;i-) 牰湩晴尨第%d个数据元素是:mn,i,p-stacki); /*置空顺序栈函数*/ void setEmpty(SqStack *p) p-top= -1; 【参考程序】 #include #include #define MAXNUM 20 #define ElemType int /*定义顺序栈的存储结构*/ typedef struct ElemType stackMAXNUM; int top; SqStack; /*初始化顺序栈*/ void InitStack(SqStack *p) if(!p) printf(Eorror); p-t

5、op=-1; /*入栈*/ void Push(SqStack *p,ElemType x) if(p-toptop=p-top+1; p-stackp-top=x; else printf(Overflow!n); /*出栈*/ ElemType Pop(SqStack *p) ElemType x; if(p-top!=0) x=p-stackp-top; ?瀠楲瑮?以前的栈顶数据元素%d已经被删除!n,p-stackp-top); p-top=p-top-1; return(x); else printf(Underflow!n); return(0); /*获取栈顶元素*/ ElemT

6、ype GetTop(SqStack *p) ElemType x; if(p-top!=0) x=p-stackp-top; return(x); else printf(Underflow!n); return(0); /*遍历顺序栈*/ void OutStack(SqStack *p) int i; printf(); if(p-toptop;i=0;i-) ?牰湩晴尨第%d个数据元素是:mn,i,p-stacki); */置空顺序栈/*void setEmpty(SqStack *p) p-top= -1; /*主函数*/ main() SqStack *q; int y,cord;

7、ElemType a; do printf(); ?瀠楲瑮?第一次使用必须初始化!n); printf(); printf( 主菜单 n); printf( 1 初始化顺序栈 n); printf( 2 插入一个元素 n); printf( 3 删除栈顶元素 n); printf( 4 取栈顶元素 n); printf( 5 置空顺序栈 n); printf( 6 结束程序运行 n); printf(-n); ?瀠楲瑮?请输入您的选择( 1, 2, 3, 4, 5,6); scanf(%d,&cord); printf(); switch(cord) case 1: q=(SqStack*)m

8、alloc(sizeof(SqStack); InitStack(q); OutStack(q); break; case 2: ?笠?牰湩晴尨请输入要插入的数据元素:a=); scanf(%d,&a); Push(q,a); OutStack(q); break; case 3: Pop(q); OutStack(q); break; case 4: y=GetTop(q); printf(栈顶元素为:%dn,y); OutStack(q); break; case 5: setEmpty(q); printf(顺序栈被置空!n); OutStack(q); break; case 6: e

9、xit(0); while (cordtop=NULL; /*链栈置空函数*/ void setEmpty(LinkStack * s) s-top=NULL; /*入栈函数*/ void pushLstack(LinkStack * s, Elemtype x) p=(StackNode *)malloc(sizeof(StackNode); /建立一个节点。 p-data=x; p-next=s-top; /指向栈顶。 s-top=p; /插入 /*出栈函数*/ Elemtype popLstack(LinkStack * s) x=p-data; s-top=p-next; /当前的栈顶

10、指向原栈的next free(p); /释放 /*取栈顶元素函数*/ Elemtype StackTop(LinkStack *s) return s-top-data; /*遍历链栈函数*/ void Disp(LinkStack * s) while (p!=NULL) printf(%dn,p-data); p=p-next; 【参考程序】 #include #include #include typedef int Elemtype; typedef struct stacknode Elemtype data; stacknode * next; StackNode; typedef

11、 struct stacknode * top; /栈顶指针 LinkStack; /*初始化链栈*/ void InitStack(LinkStack * s) s-top=NULL; printf(已经初始化链栈!n); /*链栈置空*/ void setEmpty(LinkStack * s) s-top=NULL; printf(链栈被置空!n); /*入栈*/ void pushLstack(LinkStack * s, Elemtype x) StackNode * p; p=(StackNode *)malloc(sizeof(StackNode); /建立一个节点。 p-dat

12、a=x; p-next=s-top; /由于是在栈顶pushLstack,所以要指向栈顶。 s-top=p; /插入 /*出栈*/ Elemtype popLstack(LinkStack * s) Elemtype x; StackNode * p; p=s-top; /指向栈顶 if (s-top =0) printf(栈空,不能出栈!n); exit(-1); x=p-data; s-top=p-next; /当前的栈顶指向原栈的next free(p); /释放 return x; /*取栈顶元素*/ Elemtype StackTop(LinkStack *s) if (s-top

13、=0) printf(链栈空n); exit(-1); return s-top-data; /*遍历链栈*/ void Disp(LinkStack * s) printf(链栈中的数据为:n); printf(=n); StackNode * p; p=s-top; while (p!=NULL) printf(%dn,p-data); p=p-next; printf(=n); void main() printf(=链栈操作=nn); int i,m,n,a; LinkStack * s; s=(LinkStack *)malloc(sizeof(LinkStack); int cor

14、d; do printf(); ?瀠楲瑮?第一次使用必须初始化!n); printf(); printf( 主菜单 n); printf( 1 初始化链栈 n); printf( 2 入栈 n); printf( 3 出栈 n); printf( 4 取栈顶元素 n); printf( 5 置空链栈 n); printf( 6 结束程序运行 n); printf(-n); ?瀠楲瑮?请输入您的选择( 1, 2, 3, 4, 5,6); scanf(%d,&cord); printf(); switch(cord) case 1: InitStack(s); Disp(s); break; ca

15、se 2: ?笠牰湩晴尨输入将要压入链栈的数据的个数:n=); scanf(%d,&n); ?瀠楲瑮?依次将%d个数据压入链栈:n,n); for(i=1;i=n;i+) scanf(%d,&a); pushLstack(s,a); Disp(s); break; case 3: printf(出栈操作开始!n); ?牰湩晴尨输入将要出栈的数据个数:m=); scanf(%d,&m); for(i=1;i=m;i+) printf(第%d次出栈的数据是:%d,i,popLstack(s); Disp(s); break; case 4: printf(n链栈的栈顶元素为:%dn,StackTo

16、p(s); printf(); break; case 5: setEmpty(s); Disp(s); break; case 6: exit(0); while (cordfront=-1; q-rear=-1; /*入队函数*/ int append(sqqueue *q, Elemtype x) q-rear+; q-queueq-rear=x; /*出队函数*/ Elemtype Delete(sqqueue *q) x=q-queue+q-front; /*判断队列是否为空函数*/ int Empty(sqqueue *q) if (q-front=q-rear) return T

17、RUE;/*取队头元素函数*/ int gethead(sqqueue *q) return(q-queueq-front+1); /*遍历队列函数*/ void display(sqqueue *q) while(srear) s=s+1; printf(%dqueues); /*建立顺序队列函数*/ void Setsqqueue(sqqueue *q) for (i=0;in;i+) /*利用循环快速输入数据*/ scanf(%d,&m); append(q,m); /*利用入队函数快速输入数据*/ 【参考程序】 #include #include #define MAXNUM 100

18、#define Elemtype int #define TRUE 1 #define FALSE 0 typedef struct Elemtype queueMAXNUM; int front; int rear; sqqueue; /*队列初始化*/ int initQueue(sqqueue *q) if(!q) return FALSE; q-front=-1; q-rear=-1; return TRUE; /*入队*/ int append(sqqueue *q, Elemtype x) if(q-rear=MAXNUM-1) return FALSE; q-rear+; q-q

19、ueueq-rear=x; return TRUE; /*出队*/ Elemtype Delete(sqqueue *q) Elemtype x; if (q-front=q-rear) return 0; x=q-queue+q-front; return x; /*判断队列是否为空*/ int Empty(sqqueue *q) if (q-front=q-rear) return TRUE; return FALSE; /*取队头元素*/ int gethead(sqqueue *q) if (q-front=q-rear) return 0; return(q-queueq-front

20、+1); /*遍历队列*/ void display(sqqueue *q) int s; s=q-front; if (q-front=q-rear) ?瀠楲瑮?队列空!n); else printf(顺序队列依次为:); while(srear) s=s+1; printf(%dqueues); printf(); ?瀠楲瑮?顺序队列的队尾元素所在位置:rear=%dn,q-rear); ?瀠楲瑮?顺序队列的队头元素所在位置:front=%dn,q-front); /*建立顺序队列*/ void Setsqqueue(sqqueue *q) int n,i,m; printf(请输入将要入顺序队列的长度:); scanf(%d,&n); printf(请依次输入入顺序队列的元素值:n); for (i=0;in;i+) scanf(%d,&m); append(q,m); main() sqqueue *head; int x,y,z,select; head=(s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论