栈的基本操作C语言实现

上传人:鲁** 文档编号:490441309 上传时间:2023-08-17 格式:DOC 页数:5 大小:106.51KB
返回 下载 相关 举报
栈的基本操作C语言实现_第1页
第1页 / 共5页
栈的基本操作C语言实现_第2页
第2页 / 共5页
栈的基本操作C语言实现_第3页
第3页 / 共5页
栈的基本操作C语言实现_第4页
第4页 / 共5页
栈的基本操作C语言实现_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《栈的基本操作C语言实现》由会员分享,可在线阅读,更多相关《栈的基本操作C语言实现(5页珍藏版)》请在金锄头文库上搜索。

1、#include#include#define length sizeof(struct node)struct nodeint data;struct node *next;/定义结点 void Crestack(struct node *p)/创建栈 struct node *q; q=(struct node*)malloc(length); q=NULL; *p=q; void push(struct node *p,int x) /压栈 struct node *q; q=(struct node *)malloc(sizeof(struct node); q-data=x; q-n

2、ext=*p; *p=q; int pop(struct node *p) /出栈 struct node *q; q=*p; if(*p=NULL) printf(提示:栈空 !n); else *p=(*p)-next; return(q-data); void display(struct node *p)/顺序遍历 struct node *q; q=*p; do printf(%d ,q-data); q=q-next; while(q!=NULL); void main() printf(*n);printf(* 欢迎进入栈的管理程序 *n);printf(*n);printf(*

3、 操作代码 *n);printf(*n);printf(* 1.显示栈中元素 2.弹出栈顶元素 *n);printf(* *n);printf(* 3.插入N 个元素 4.退出应用程序 *n);printf(*n);int i, m, n,t=0, temp=1;struct node *head; Crestack(&head);printf(请输入六个栈元素n) ;for(i=1;i=6;i+)scanf(%d,&m);push(&head,m);while(temp)printf(请输入操作代码n) ;scanf(%d,&t);while(!(t=1|t=2|t=3|t=4) print

4、f(请重新输入操作代码n) ;scanf(%d,&t);switch(t)case 1: printf(栈中元素是 :n);display(&head); printf(nn) ; break;case 2: pop(&head); printf(剩余元素是 :n);display(&head); printf(nn) ; break;case 3: printf(请输入要插入元素个数 :n);printf(N=);scanf(%d,&n);printf(请输入要插入的元素:n) ;for(i=1;i=n;i+)scanf(%d,&m);push(&head,m);printf(栈中元素是 :n);display(&head); printf(nn) ; break;case 4: temp=0; break;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 机械/制造/汽车 > 汽车技术

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号