标签:div while splay style color 初始化 操作 print items
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <ctype.h> 4 #include <string.h> 5 typedef int ElemType; 6 typedef struct item{ 7 ElemType items;//数据项 8 struct item *next;//指向下一个数据项的指针 9 }item; 10 typedef struct stack{ 11 item *top;//总是指向栈顶(即最后一个数据项的指针) 12 int length;//当前栈的长度(存储数据项的个数) 13 }stack; 14 stack * initstack(){ 15 stack * stacks=(stack *)malloc(sizeof(stack)); 16 stacks->top=NULL; 17 stacks->length=0;//长度初始化 18 return stacks; 19 } 20 void pop(stack * stacks){ 21 item *p = stacks->top; 22 printf("%d已弹出!\n",stacks->top->items); 23 stacks->top=stacks->top->next; 24 free(p); 25 } 26 void push(stack * stacks){ 27 printf("输入你想添加的整数:\n"); 28 ElemType a; 29 scanf("%d",&a); 30 item *p=(item *)malloc(sizeof(item)); 31 p->items=a; 32 p->next=stacks->top; 33 stacks->top=p; 34 stacks->length++; 35 } 36 void display(stack * stacks){ 37 if(stacks->top){ 38 item *p=stacks->top;//获取栈顶元素 39 printf("以下为数据,由新到旧:\n"); 40 while(p){ 41 printf("%d ",p->items); 42 p=p->next; 43 } 44 printf("\n"); 45 } 46 else 47 printf("栈为空!\n"); 48 } 49 void menu(){ 50 printf("键入以下选项以操作,ctrl+z退出\nA:添加数据\nB:弹出数据\nC:打印数据\n"); 51 } 52 void main(){ 53 stack *stacks=initstack(); 54 menu(); 55 char ch; 56 while((ch=getchar())!=EOF){ 57 setbuf(stdin,NULL); 58 switch (toupper(ch)){ 59 case ‘A‘: push(stacks);break; 60 case ‘B‘: pop(stacks);break; 61 case ‘C‘: display(stacks);break; 62 } 63 menu(); 64 setbuf(stdin,NULL); 65 } 66 }
标签:div while splay style color 初始化 操作 print items
原文地址:https://www.cnblogs.com/Let-us-Coding/p/12936190.html