首页 > 其他 > 详细

栈的应用

时间:2015-04-24 15:59:02      阅读:245      评论:0      收藏:0      [点我收藏+]
 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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!