标签:
1 #include<stdio.h> 2 #include<stdlib.h> 3 typedef char datetype; 4 typedef struct stacknode 5 { 6 datetype date; 7 struct stacknode *next; 8 }stacknode; 9 typedef struct 10 { 11 stacknode *top;/*栈顶指针*/ 12 }linkstack; 13 14 void initstack(linkstack *s)/*初始化栈操作*/ 15 { 16 s->top==NULL; 17 } 18 19 int stackempty(linkstack *s)/*判断栈空操作*/ 20 { 21 if(s->top==NULL) 22 return 1; 23 else 24 return 0; 25 } 26 27 int push(linkstack *s,datetype *x) 28 { 29 stacknode *p; 30 p=(stacknode *)malloc(sizeof(stacknode));/*生成新结点*/ 31 if(p==NULL) 32 return 0; 33 else 34 { 35 p->date=x;/*将x放入新结点的数据域中*/ 36 p->next=s->top;/*将新结点插入链表表头之前*/ 37 s->top=p;/*新结点作为栈顶*/ 38 return 1; 39 } 40 } 41 42 int pop(linkstack *s) 43 { 44 stacknode *p; 45 datetype x; 46 if(stackempty(s)) 47 { 48 printf("栈空"); 49 return 0; 50 } 51 else 52 { 53 p=s->top; 54 x=p->date; 55 s->top=p->next; 56 free(p); 57 return x; 58 } 59 } 60 61 int main() 62 { 63 linkstack s; 64 datetype x; 65 initstack(&s); 66 printf("stack element:\n"); 67 while((x=getchar())!=‘\n‘) 68 { 69 push(&s,&x); 70 } 71 while(!stackempty(&s)) 72 { 73 printf("%c",pop(&s)); 74 } 75 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/4453605.html