栈的应用

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct stack
 4 {
 5     char date;
 6     struct stack *next;
 7 }stacknode;
 8 
 9 typedef struct
10 {
11     stacknode *top;
12 }linkstack;/*栈顶指针*/
13 
14 void init_stack(linkstack *s)/*初始化栈操作*/
15 {
16     s->top=NULL;
17 }
18 
19 int empty_stack(linkstack *s)
20 {
21     return(s->top==NULL);
22 }
23 
24 void push_stack(linkstack *s,char ch)
25 {
26     stacknode *p;/*新建结点*/
27     p=(stacknode *)malloc(sizeof(stacknode));
28     p->next=s->top;/*如果是第一个结点,那么p->next为NULL*/
29     p->date=ch;
30     s->top=p;
31 }
32 char pop_stack(linkstack *s)
33 {
34     stacknode *p;
35     p=s->top;
36     s->top=p->next;
37     return p->date;
38     
39 }
40 
41 int main()
42 {
43     linkstack s;
44     char ch;
45     init_stack(&s);
46     while((ch=getchar())!='\n')
47     {
48         push_stack(&s,ch);
49     }
50     while(!empty_stack(&s))
51     {
52         printf("%c",pop_stack(&s));
53     
54     }
55 }

 

posted @ 2015-04-24 15:23  御心飞行  阅读(189)  评论(0编辑  收藏  举报