1 /*顺序栈的基本操作*/
2#include "stdafx.h"
3 #include <stdio.h>
4 #define MaxSize 100
5
6 typedef char ElemType;
7 typedef struct
8 {
9 char stack[MaxSize];
10 int top;
11 }stacktype;
12//****************************************************************initstack()
13 void initstack(stacktype *S)
14 {
15 S->top=-1;
16 }
17//****************************************************************push()
18 void push(stacktype *S,ElemType x)
19 {
20 if (S->top==MaxSize)
21 printf("栈上溢出!\n");
22 else
23 {
24 S->top++;
25 S->stack[S->top]=x;
26 }
27 }
28//****************************************************************pop()
29 void pop(stacktype *S)
30 {
31 if (S->top==-1)
32 printf("栈下溢出!\n");
33 else
34 S->top--;
35 }
36//****************************************************************gettop()
37 ElemType gettop(stacktype *S)
38 {
39 if (S->top==-1)
40 {
41 printf("栈空!\n");
42 return NULL;
43 }
44 else return(S->stack[S->top]);
45 }
46//****************************************************************empty()
47 int empty(stacktype *S)
48 {
49 if (S->top==-1)
50 return(1); //空栈则返回1
51 else
52 return(0);
53 }
54//****************************************************************display()
55 void display(stacktype *S)
56 {
57 int i;
58 printf("栈中元素:");
59 for (i=S->top;i>=0;i--)
60 printf("%c ",S->stack[i]);
61 printf("\n");
62 }
63
64 void main()
65 {
66 stacktype L;
67 stacktype *st=&L;
68 printf("建立一空栈\n");
69 initstack(st);
70 printf("栈空:%d\n",empty(st));
71 printf("依次插入a,b,c,d元素\n");
72 push(st,'a');
73 push(st,'b');
74 push(st,'c');
75 push(st,'d');
76 display(st);
77 printf("退一次栈\n");
78 pop(st);
79 printf("栈顶元素:%c\n",gettop(st));
80 printf("退一次栈\n");
81 pop(st);
82 display(st);
83 }
84
85/*运行结果如下:
86
87 建立一空栈
88 栈空:1
89 依次插入a,b,c,d元素
90 栈中元素:d c b a
91 退一次栈
92 栈顶元素:c
93 退一次栈
94 栈中元素:b a
95
96*/
97
98
99
100
2#include "stdafx.h"
3 #include <stdio.h>
4 #define MaxSize 100
5
6 typedef char ElemType;
7 typedef struct
8 {
9 char stack[MaxSize];
10 int top;
11 }stacktype;
12//****************************************************************initstack()
13 void initstack(stacktype *S)
14 {
15 S->top=-1;
16 }
17//****************************************************************push()
18 void push(stacktype *S,ElemType x)
19 {
20 if (S->top==MaxSize)
21 printf("栈上溢出!\n");
22 else
23 {
24 S->top++;
25 S->stack[S->top]=x;
26 }
27 }
28//****************************************************************pop()
29 void pop(stacktype *S)
30 {
31 if (S->top==-1)
32 printf("栈下溢出!\n");
33 else
34 S->top--;
35 }
36//****************************************************************gettop()
37 ElemType gettop(stacktype *S)
38 {
39 if (S->top==-1)
40 {
41 printf("栈空!\n");
42 return NULL;
43 }
44 else return(S->stack[S->top]);
45 }
46//****************************************************************empty()
47 int empty(stacktype *S)
48 {
49 if (S->top==-1)
50 return(1); //空栈则返回1
51 else
52 return(0);
53 }
54//****************************************************************display()
55 void display(stacktype *S)
56 {
57 int i;
58 printf("栈中元素:");
59 for (i=S->top;i>=0;i--)
60 printf("%c ",S->stack[i]);
61 printf("\n");
62 }
63
64 void main()
65 {
66 stacktype L;
67 stacktype *st=&L;
68 printf("建立一空栈\n");
69 initstack(st);
70 printf("栈空:%d\n",empty(st));
71 printf("依次插入a,b,c,d元素\n");
72 push(st,'a');
73 push(st,'b');
74 push(st,'c');
75 push(st,'d');
76 display(st);
77 printf("退一次栈\n");
78 pop(st);
79 printf("栈顶元素:%c\n",gettop(st));
80 printf("退一次栈\n");
81 pop(st);
82 display(st);
83 }
84
85/*运行结果如下:
86
87 建立一空栈
88 栈空:1
89 依次插入a,b,c,d元素
90 栈中元素:d c b a
91 退一次栈
92 栈顶元素:c
93 退一次栈
94 栈中元素:b a
95
96*/
97
98
99
100