数据结构(栈子系统:c实现)
来源:互联网 发布:电影院简单数据库设计 编辑:程序博客网 时间:2024/05/08 02:22
#include<stdio.h>#include<stdlib.h>#define N sizeof(stacknode)//结点所占字节数 N//定义结构体typedef int datatype;typedef struct stacknode{datatype data;struct stacknode *next;}stacknode;//定义栈顶typedef struct {stacknode *top;int count;//计数用}linkstack;//进栈,元素一一进栈void InsertStack(linkstack *s){int x=0;stacknode *p;s->top=NULL;s->count=0;//printf("\n\t建立一个栈子系统");p=(stacknode*)malloc(N);printf("\n\t\t请逐个输入数字,结束标记位,做结束符的数字: 0 \n");while(1){/*printf("\t\t请输入:");fflush(stdin);if(!scanf("%d",&x)){printf("输入的元素种类错误!!!\n");continue;}else if(x=='#') break;else{p->data=x;p->next=s->top;s->top=p;s->count++;}*/printf("\t\t请输入:");p=(stacknode*)malloc(N);fflush(stdin);if(!scanf("%d",&x)){printf("\t\t\t输入的“元素“种类错误!!!\n");continue;}else if(x==0)break;else{p->data=x;p->next=s->top;s->top=p;s->count++;}}printf("\n");}//显示栈中元素void ShowStack(linkstack *s){stacknode *p;int i=0;p=s->top;i=s->count;//p->data=s->top->data;//if(p->next==NULL)if(i==0)printf("\t\t栈是一个空栈!!!!\n");else{printf("\t栈中各个元素为:\t");while(i!=0){printf("%8d",p->data);p=p->next;//s->top=s->top->next;i--;}}printf("\n");}//求栈中元素的个数void LengthStack(linkstack *s){printf("\t栈中元素的个数为:\t");printf("%d",s->count);}//出栈,栈中各个元素的出栈void PutStack(linkstack *s){//int x;stacknode *p;//linkstack *i;if(s->count==0){printf("\t\t栈是一个空栈!!!!");//return 0;}else{/*p=s->top;x=p->data;s->top=p->next;free(p);//s->count-=1;*/p=s->top;s->top=s->top->next;printf("\n\t\t\t\t出栈元素为:%d\n",p->data);free(p);s->count--;}}//数制转换,十进制转换为二进制void ShiftStack(linkstack *s){int z=0;int m=0;stacknode *p;printf("请输入所要转换的 ”数字“ Z:\t");scanf("%d",&z);s->top=NULL;while(z){m=z%2;z=z/2;p=(stacknode*)malloc(N);p->next=s->top;s->top=p;s->top->data=m;}printf("\n\t转化后的二进制为\t");while(s->top){p=s->top;printf("%d",p->data);s->top=s->top->next;free(p);}printf("\n");}int main(){int a;linkstack s;s.count=0;//linkstack *s;while(1) {printf(" \n\t\t\t\t\t\t栈子系统\n");printf(" \t\t***************************************************\n");printf(" \t\t* 1------进 栈 *\n");printf(" \t\t* 2------出 栈 *\n");printf(" \t\t* 3------显示栈中元素 *\n");printf(" \t\t* 4------求栈中元素个数 *\n");printf(" \t\t* 5------数制转换 *\n");printf(" \t\t* 0------返 回 *\n");printf(" \t\t***************************************************\n");printf(" 请输入(0-5)选项:\n"); printf("\n请输入所要达到第几号功能:\t");fflush(stdin);scanf("%d",&a); if(a == 1) InsertStack(&s); else if(a == 2) PutStack(&s); else if(a == 3) ShowStack(&s); else if(a == 4) LengthStack(&s); else if(a == 5) ShiftStack(&s); else if(a == 0) return 0; else{ printf("!!!!!输入有误,请重新输入!!!!!\n"); } } }
1 0
- 数据结构(栈子系统:c实现)
- 数据结构(线性表子系统:c实现)
- 数据结构(循环队列子系统:c实现)
- 数据结构(二叉树子系统:c语言实现)
- 数据结构用c实现线性表子系统
- 数据结构(栈)c/c++/java实现
- 【数据结构】数据结构C语言的实现(栈)
- 数据结构之单链表实现栈(C++)
- 数据结构--顺序栈实现(c语言)
- 数据结构(C实现)------- 顺序栈
- 【数据结构】顺序栈的实现(c++)
- 数据结构之栈(C语言实现)
- 数据结构之 栈(C语言实现)
- 数据结构C语言实现栈
- 数据结构 链式 栈 c实现
- 数据结构.栈(C语言实现)
- 数据结构---栈C语言实现
- 数据结构之栈(C实现)
- [CodeForces 566F] Clique in the Divisibility Graph (记忆化搜索)
- js 判断undefined类型
- 又论Node.js的HTTP模块之深入理解
- IntelliJ IDEA 14 注册码
- OpenJudge-计算机概论(A)-1-生日相同 2.0
- 数据结构(栈子系统:c实现)
- android adb常用指令
- C#——方法之输出参数
- 欢迎使用CSDN-markdown编辑器
- Block很简单,就像delegate的简化版
- Android6.0系统增加那些新特性
- 结合Wireshark捕获分组深入理解TCP/IP协议栈之HTTP协议
- iOS开发------简单实现图片多选功能(AssetsLibrary.framework篇)
- 治疗咽炎最有效、最省钱、最方便的方法(我认为)