数据结构栈的相关操作c程序实现

上传人:正** 文档编号:41702913 上传时间:2018-05-30 格式:DOC 页数:14 大小:240KB
返回 下载 相关 举报
数据结构栈的相关操作c程序实现_第1页
第1页 / 共14页
数据结构栈的相关操作c程序实现_第2页
第2页 / 共14页
数据结构栈的相关操作c程序实现_第3页
第3页 / 共14页
数据结构栈的相关操作c程序实现_第4页
第4页 / 共14页
数据结构栈的相关操作c程序实现_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《数据结构栈的相关操作c程序实现》由会员分享,可在线阅读,更多相关《数据结构栈的相关操作c程序实现(14页珍藏版)》请在金锄头文库上搜索。

1、顺序栈 #include #include #include # define m 10 typedef struct int am; int top; stack; void main() int isempty(stack *s); int isfull(stack *s); int gettop(stack *s,int x); int get(stack *s,int b); void push(stack *s,int x); void pop(stack*s);int i,x,b,c,j=0; stack *s; s=(stack *)malloc(sizeof(stack);s-

2、top=0; printf(“please enter 10 numbersn“); for(i=0;itop=0) return 1; else return 0; int isfull(stack *s) if(s-top=m) return 1; else return 0; int gettop(stack *s,int x) int i; i=isempty(s); if(i)printf(“empty“); else x=s-as-top-1; return x; void push(stack *s,int x) int i; i=isfull(s); if(i)printf(“

3、overflow“); else s-as-top=x; s-top+; int get(stack *s,int b) int c; c=s-ab-1; return c; void pop(stack*s)int i,j,t; t=s-top; for(i=0;ias-top-1); s-top-; printf(“%dn“,s-as-top-1); s-top-; j=isempty(s); if(j)printf(“出栈成功!n“);链栈约瑟夫环(循环队列,链队) #include #include #include typedef struct int data; int a; st

4、ruct Lnode *next; Lnode; void main() int m,n,i;Lnode *p,*q,*r; Lnode *establish_h(Lnode*p,int n);Lnode *nizhi(Lnode *p); printf(“请输入人数n“); scanf(“%d“, p=establish_h(p,n); p=nizhi(p);printf(“请输入 m 初值n“); scanf(“%d“, printf(“出列序列为n“); while(p-aa0) for(i=1;inext; m=p-data; printf(“%dn“,p-a);q=p-next;r-

5、next=q; free(p); p=q; Lnode *establish_h(Lnode*p,int n) Lnode *q;int i;int t; p=(Lnode*)malloc(sizeof(Lnode);p-data=n;/头节点存储元素个数 p-next=NULL;i=1;/第二个节点开始存储元素 while(idata) q=(Lnode*)malloc(sizeof(Lnode); printf(“请输入密码(用回车隔开)n“);scanf(“%d“,q-data=t; q-a=i; q-next=p-next; p-next=q; i+; return p;Lnode *

6、nizhi(Lnode *p) Lnode *q,*w;int i;int t; Lnode *r;Lnode *l;q=p-next;l=(Lnode*)malloc(sizeof(Lnode);l-data=p-data; l-a=p-a;l-next=NULL;i=1;/第二个节点开始存储元素 while(idata) r=(Lnode*)malloc(sizeof(Lnode);r-data=q-data; r-a=q-a; q=q-next;r-next=l-next; l-next=r; i+; p=l; q=p-next; printf(“起始密码顺序为:n“);for(i=1;

7、q-next;i+) printf(“%dn“,q-data);q=q-next; printf(“%dn“,q-data); q-next=p-next; p=q-next;return p; 串的基本操作 #include #include #include#define m 100 typedef struct char chm; int length; Hstr;void main() Hstr *l,*p,*r; char c,w; int h,j,k; int i=0; l=(Hstr *)malloc(sizeof(Hstr); p=(Hstr *)malloc(sizeof(H

8、str); r=(Hstr *)malloc(sizeof(Hstr); l-length=0;p-length=0; r-length=0; printf(“请选择相关操作(数字 15 控制,输入 0 结束)n“); printf(“-1.建立串-n“); printf(“-2.显示串长度-n“);printf(“-3.生成与原来相同的串-n“); printf(“-4.串比较-n“); printf(“-5.串连接-n“); printf(“-6.返回值-n“); scanf(“%c“, getchar(); while(w) switch(w) case 1:printf(“请输入字符(

9、#结束):n“); scanf(“%c“, while(c!=#) l-length+; l-chi=c; i+;scanf(“%c“, printf(“串中字符为n“); for(i=0;ilength;i+) printf(“%c“,l-chi);printf(“n“); break; case 2: printf(“串长度为%dn“,l-length);break; case 3:for(i=0;ilength;i+)p-chi=l-chi;p-length=l-length;printf(“复制的串中字符为n“); for(i=0;ilength;i+) printf(“%c“,p-c

10、hi);printf(“n“);break; case 4:i=0; printf(“请输入要与原串比较的字符串(#结束):n“); scanf(“%c“, while(c!=#) r-length+;r-chi=c; i+;scanf(“%c“, printf(“第二个串中字符为n“); for(i=0;ilength;i+) printf(“%c“,r-chi);printf(“n“);for(i=0;ilengthi+) if(l-chi!=r-chi) if(l-chi-r-chi)chi-r-chi)0)printf(“第一个串大“); printf(“n“); break; if(

11、i=r-length|i=l-length)printf(“两个串一样大n“);break; case5:i=0; printf(“请输入要与原串连接的串(#结束)n“);scanf(“%c“, while(c!=#) r-length+; r-chi=c; i+;scanf(“%c“, printf(“第二个串中字符为n“); for(i=0;ilength;i+) printf(“%c“,r-chi);printf(“n“); for(i=0,j=0;ilength;i+,j+) l-chl-length+i=r-chj; l-length=l-length+r-length;printf

12、(“连接后第一个串中字符为n“); for(i=0;ilength;i+) printf(“%c“,l-chi);printf(“n“); break;case 6: i=0; printf(“请输入要找串的起始位置(第几个字符?)n“);scanf(“%d“,printf(“请输入要找的字符个数n“);scanf(“%d“,printf(“内容为:n“);for(i=0;ichh-1);h+;printf(“n“);break; getchar(); printf(“请选择相关操作(数字 16 控制,输入 0 结束)n“); printf(“-1.建立串-n“); printf(“-2.显示串长度-n“); printf(“-3.生成与原来相同的串-n“); printf(“-4.串比较-n“); printf(“-5.串连接-n“); printf(“-6.返回值-n“);scanf(“%c“, getchar();

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

当前位置:首页 > 办公文档 > 其它办公文档

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