1 /*
2 顺序栈的创建
3
4 Levi.
5 */
6
7 #include <stdio.h>
8 #define DataType char
9 #define StackSize 100
10
11
12 typedef struct{
13 DataType stack[StackSize];
14 int top;
15 }SeqStack;
16
17
18 void InitStack(SeqStack *S){
19 S->top=0;
20 }
21
22 int StackEmpty(SeqStack S){
23 if(S.top==0)
24 return 1;
25 else
26 return 0;
27 }
28
29 int GetTop(SeqStack S,DataType *e){
30 if(S.top<=0){
31 printf("null!\n");
32 return 0;
33 }
34 else{
35 *e=S.stack[S.top-1];
36 return 1;
37 }
38 }
39
40 int PushStack(SeqStack *S,DataType e){
41 if(S->top>=StackSize){
42 printf("Fail !\n");
43 return 0;
44 }
45 else{
46 S->stack[S->top]=e;
47 S->top++;
48 return 1;
49 }
50 }
51
52 int PopStack(SeqStack *S,DataType *e){
53 if(S->top<=0){
54 printf("Fail !\n");
55 return 0;
56 }
57 else{
58 S->top--;
59 *e=S->stack[S->top];
60 return 1;
61
62 }
63 }
64
65 int StackLength(SeqStack S){
66 return S.top;
67 }
68
69 void ClearStack(SeqStack *S){
70 S->top=0;
71 }
72
73 void conversion(){
74 SeqStack s;
75 unsigned n;
76 int e;
77 InitStack(&s);
78 printf("n converse %d jinzhi,Please input : n(>=0)=",2);
79 scanf("%u",&n);
80 while(n){
81 PushStack(&s,n%2);
82 n=n/2;
83 }
84
85 while(!StackEmpty(s)){
86 PopStack(&s,&e);
87 printf("%d",e);
88 }
89 printf("\n");
90 }
91
92 void Print(SeqStack s){
93 int base=0;
94 while(base<s.top){
95 printf("%c",s.stack[base]);
96 base++;
97 }
98 }