鏁版嵁缁撴瀯-C璇█瀹炵幇-閾炬爤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏁版嵁缁撴瀯-C璇█瀹炵幇-閾炬爤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/div' title='div'>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;//鏁版嵁椤?/span> 8 struct item *next;//鎸囧悜涓嬩竴涓暟鎹」鐨勬寚閽?/span> 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;//闀垮害鍒濆鍖?/span> 18 return stacks; 19 } 20 void pop(stack * stacks){ 21 item *p = stacks->top; 22 printf("%d宸插脊鍑? ",stacks->top->items); 23 stacks->top=stacks->top->next; 24 free(p); 25 } 26 void push(stack * stacks){ 27 printf("杈撳叆浣犳兂娣诲姞鐨勬暣鏁? "); 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("浠ヤ笅涓烘暟鎹?鐢辨柊鍒版棫: "); 40 while(p){ 41 printf("%d ",p->items); 42 p=p->next; 43 } 44 printf(" "); 45 } 46 else 47 printf("鏍堜负绌? "); 48 } 49 void menu(){ 50 printf("閿叆浠ヤ笅閫夐」浠ユ搷浣?ctrl+z閫€鍑篭nA:娣诲姞鏁版嵁 B:寮瑰嚭鏁版嵁 C:鎵撳嵃鏁版嵁 "); 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 鈥?/span>A鈥?/span>: push(stacks);break; 60 case 鈥?/span>B鈥?/span>: pop(stacks);break; 61 case 鈥?/span>C鈥?/span>: display(stacks);break; 62 } 63 menu(); 64 setbuf(stdin,NULL); 65 } 66 }
以上是关于鏁版嵁缁撴瀯-C璇█瀹炵幇-閾炬爤的主要内容,如果未能解决你的问题,请参考以下文章