顺序栈创建

 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 }

 

转载于:https://www.cnblogs.com/suiyun/archive/2013/01/24/2875307.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值