순서 창고의 C 코드 구현

13878 단어 코드
  1 /*
2
3
4
5 ADT (Stack)
6 Data:
7 , 。
8 Operation:
9 InitStack(*S);
10 DestroyStack(*S);
11 IsEmpty(*S);
12 ClearStack(*S);
13 GetTop(*S, *e);
14 Push(*S, e);
15 Pop(*S, e);
16 StackLength(*S);
17 EndADT
18 */
19
20 #include<stdio.h>
21 #include<stdlib.h>
22 #define MAXSIZE 5
23 #define TRUE 1
24 #define FALSE 0
25 #define OK 1
26 #define ERROR 0
27
28 typedef int Status;
29 typedef int ElemType;
30 typedef struct
31 {
32 ElemType data[MAXSIZE];
33 int top;
34 }SqStack;
35
36 /*
37
38 */
39 void InitStack(SqStack *s)
40 {
41 s->top = -1;
42 }
 43 /*
44 : , , true
45 */
46 Status IsEmpty(SqStack *S)
47 {
48 if(S->top == -1)
49 return TRUE;
50 return FALSE;
51 }
52
53 /*
54
55 */
56 void ClearStack(SqStack *s)
57 {
58 s->top = -1;
59 }
60
61 /*
62 : e
63 */
64 Status Push(SqStack *S, ElemType e)
65 {
66 if(S->top == MAXSIZE-1)
67 return ERROR;
68 S->top++;
69 S->data[S->top] = e;
70 return OK;
71 }
72 /*
73 : , , e
74 */
75 Status Pop(SqStack *S, ElemType *e)
76 {
77 if(S->top == -1)
78 return ERROR;
79 *e = S->data[S->top];
80 S->top--;
81 return OK;
82 }
83 /*
84
85 */
86 Status StackLength(SqStack *s)
87 {
88 return s->top+1;
89 }
90
91 /*
92 : , e
93 */
94 Status GetTop(SqStack *s, ElemType *e)
95 {
96 if(s->top == -1)
97 return ERROR;
98 *e = s->data[s->top];
99 return OK;
100 }
101
102 /*
103 : main
104 */
105
106 int main()
107 {
108 SqStack s;
109 InitStack(&s);
110 printf("%d
", s.top);
111
112 ElemType a = 100;
113 Push(&s, a);
114 a = 333;
115 Push(&s, a);
116 a = 45;
117 Push(&s, a);
118 printf("len = %d
", StackLength(&s));
119
120 printf("data =\t");
121 for(int i=0; i<StackLength(&s); i++)
122 printf("%d\t", s.data[i]);
123 printf("
");
124
125 ElemType b;
126 Pop(&s, &b);
127 printf("b = %d
", b);
128 printf("len = %d
", StackLength(&s));
129
130 GetTop(&s, &b);
131 printf("b = %d
", b);
132
133 printf("%d
", IsEmpty(&s));
134
135 ClearStack(&s);
136 printf("top = %d
", s.top);
137 printf("len = %d
", StackLength(&s));
138
139 DestroyStack(&s);
140 Push(&s, a);
141 Push(&s, a);
142 printf("top = %d
", s.top);
143 system("pause");
144 return 0;
145 }

 
【Stay hungry, Stay foolish】
  ^_^  Eric

좋은 웹페이지 즐겨찾기