数据结构 -- 栈(一)

 

 

 1 /******************************
 2 * 
 3 * 用数组表示栈
 4 *
 5 *******************************/
 6 
 7 
 8 #include <stdio.h>
 9 
10 #define MAXSIZE 100
11 
12 //元素elem 进栈
13 int push(char* a, int top, char elem)
14 {
15     if(top >= (MAXSIZE-1))
16     {
17         printf("栈满");
18         return -1;
19     }
20     a[++top] = elem;
21     return top;
22 }
23 
24 //数据元素出栈
25 int pop(char* a, int top)
26 {
27     if(top == -1)
28     {
29         printf("空栈");
30         return -1;
31     }
32     printf("出栈元素:%c \n",a[top]);
33     top--;
34     return top;
35 }
36 
37 int main()
38 {
39     char a[MAXSIZE];
40     int top = -1;
41     top = push(a, top, 'a');
42     top = push(a, top, 'b');
43     top = push(a, top, 'c');
44     top = push(a, top, 'd');
45     
46     top = pop(a,top);
47     top = pop(a,top);
48     top = pop(a,top);
49     top = pop(a,top);
50     
51     return 0;
52 }

 

posted @ 2018-01-23 16:43  西门吹雪~~~  阅读(144)  评论(0编辑  收藏  举报