ImageVerifierCode 换一换
格式:DOCX , 页数:21 ,大小:19.87KB ,
资源ID:5094980      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5094980.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(用顺序结构表示栈并实现栈的各种基本操作.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

用顺序结构表示栈并实现栈的各种基本操作.docx

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(sizeof(SqStac

3、k) /*申请空间*/)/*入栈函数*/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 *p) for(i=p-top;i=0;i-)pr

4、intf(第%d个数据元素是:%6dn,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-top=-1;/*入栈*/void Push(SqStack *p

5、,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; printf(以前的栈顶数据元素%d已经被删除!n,p-stackp-top); p-top=p-top-1; return(x); else printf(Underflow!n); return(0); /*获取栈顶元素*/ElemType GetTop(SqStack *p) ElemType x;

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

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

8、); InitStack(q); OutStack(q); break; case 2: printf(请输入要插入的数据元素:a=); scanf(%d,&a); Push(q,a); OutStack(q); break; case 3: Pop(q); OutStack(q); break; case 4: y=GetTop(q); printf(n栈顶元素为:%dn,y); OutStack(q); break; case 5: setEmpty(q); printf(n顺序栈被置空!n); OutStack(q); break; case 6: exit(0); while (cor

9、dtop=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; /当前的栈顶指向原栈的next free(p); /释放/*取

10、栈顶元素函数*/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 struct stacknode * top; /栈顶指针Link

11、Stack;/*初始化链栈*/void InitStack(LinkStack * s) s-top=NULL; printf(n已经初始化链栈!n);/*链栈置空*/void setEmpty(LinkStack * s) s-top=NULL; printf(n链栈被置空!n);/*入栈*/void pushLstack(LinkStack * s, Elemtype x) StackNode * p; p=(StackNode *)malloc(sizeof(StackNode); /建立一个节点。 p-data=x; p-next=s-top; /由于是在栈顶pushLstack,所以

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

13、data;/*遍历链栈*/void Disp(LinkStack * s) printf(n链栈中的数据为: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 cord; do printf(n); printf(第一次使用必须初始化!n); prin

14、tf(n); printf(n 主菜单 n); printf(n 1 初始化链栈 n); printf(n 2 入栈 n); printf(n 3 出栈 n); printf(n 4 取栈顶元素 n); printf(n 5 置空链栈 n); printf(n 6 结束程序运行 n); printf(n-n); printf(请输入您的选择( 1, 2, 3, 4, 5,6); scanf(%d,&cord); printf(n); switch(cord) case 1: InitStack(s); Disp(s); break; case 2: printf(输入将要压入链栈的数据的个数:

15、n=); scanf(%d,&n); printf(依次将%d个数据压入链栈:n,n); for(i=1;i=n;i+) scanf(%d,&a); pushLstack(s,a); Disp(s); break; case 3: printf(n出栈操作开始!n); printf(输入将要出栈的数据个数:m=); scanf(%d,&m); for(i=1;i=m;i+) printf(n第%d次出栈的数据是:%d,i,popLstack(s); Disp(s); break; case 4: printf(nn链栈的栈顶元素为:%dn,StackTop(s); printf(n); bre

16、ak; 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 TRUE;/*取队头元素函数*/int gethead

17、(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#define Elemtype int#define TRUE 1

18、#define FALSE 0typedef 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-queueq-rear=x; return TRUE;/*出队*/Elemtype

19、 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+1);/*遍历队列*/void display(sqqueue *q) int s; s=

20、q-front; if (q-front=q-rear) printf(队列空!n); else printf(n顺序队列依次为:); while(srear) s=s+1; printf(%dqueues); printf(n); printf(顺序队列的队尾元素所在位置:rear=%dn,q-rear); printf(顺序队列的队头元素所在位置:front=%dn,q-front); /*建立顺序队列*/void Setsqqueue(sqqueue *q) int n,i,m; printf(n请输入将要入顺序队列的长度:); scanf(%d,&n); printf(n请依次输入入顺

21、序队列的元素值:n); for (i=0;in;i+) scanf(%d,&m); append(q,m); main() sqqueue *head; int x,y,z,select; head=(sqqueue*)malloc(sizeof(sqqueue); doprintf(n第一次使用请初始化!n); printf(n请选择操作(1-7):n); printf(=n); printf(1初始化n); printf(2建立顺序队列n); printf(3入队n); printf(4出队 n); printf(5判断队列是否为空n); printf(6取队头元素 n); printf(7遍历队列n); printf(=n); scanf(%d,&select); switch(select) case 1: initQueue(head); printf(已经初始化顺序队列!n); break; case 2: Setsqqueue(head); printf(n已经建立队列!n); display(head); break; case 3: printf(请输入队的值:n ); scanf(%d,&x); append(head,x); display(head); break;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1