顺序栈的基本操作-完整代码和拆开分析

 1 #include <stdio.h>    //增+删+取栈顶+初始化+判空+输出
 2 #define StackSize 100
 3 typedef int DataType;
 4 typedef struct{
 5     DataType data[StackSize];
 6     int top;
 7 }SeqStack;
 8 
 9 void InitStack(SeqStack * S);
10 void Push(SeqStack *S,DataType x);
11 int Pop(SeqStack * S,DataType * ptr);
12 int GetTop(SeqStack * S,DataType*ptr);
13 int Empty(SeqStack * S);
14 int Print(SeqStack * S);
15 
16 int main(){
17     DataType x;
18     SeqStack S;
19     InitStack(&S);
20     printf("对5和10执行入栈操作:
");
21     Push(&S,15);
22     Print(&S);
23     Push(&S,10);
24     Print(&S);
25     if(GetTop(&S,&x)==1)
26         printf("当前栈顶元素为:%d
",x);
27     if(Pop(&S,&x)==1)
28         printf("执行一次出栈操作,删除元素:%d
",x);
29     if(GetTop(&S,&x)==1){
30         printf("当前栈顶元素为:%d
",x);
31     }
32     printf("请输入待入栈元素:");
33     scanf("%d",&x);
34     Push(&S,x);
35     if(Empty(&S)==1)
36         printf("栈为空
");
37     else
38         printf("栈非空
");
39     return 0;
40 }
41 
42 void InitStack(SeqStack * S){
43     S->top=-1;
44     printf("初始化成功!
");
45 }
46 void Push(SeqStack * S,DataType x){
47     if(S->top==StackSize-1){
48         printf("上溢错误,插入失败
");
49     }
50     S->data[++S->top]=x;
51     printf("入栈成功!
"); 
52 }
53 int Pop(SeqStack * S,DataType * ptr){
54     if(S->top==-1){
55         printf("下溢错误,删除失败
");
56         return 0;
57     }
58     *ptr = S->data[S->top--];
59     return *ptr;
60 }
61 int GetTop(SeqStack * S,DataType*ptr){
62     if(S->top==-1){
63         printf("下溢错误,取栈顶失败
");
64         return 0;
65     }
66     *ptr = S->data[S->top];
67     return 1;
68 }
69 int Empty(SeqStack * S){
70     if(S->top==-1)
71         return 1;
72     else
73         return 0;
74 }
75 int Print(SeqStack * S){
76     printf("栈内元素:
");
77     for(int i=0;i<=S->top;i++){
78         printf("%d ",S->data[i]);
79     }
80     printf("
");
81 }

 1.初始化:

42 void InitStack(SeqStack * S){
43     S->top=-1;
44     printf("初始化成功!
");
45 }

将顺序栈顶Top设为-1

2.入栈:

46 void Push(SeqStack * S,DataType x){
47     if(S->top==StackSize-1){
48         printf("上溢错误,插入失败
");
49     }
50     S->data[++S->top]=x;
51     printf("入栈成功!
"); 
52 }

(1)判断栈是否已满-----栈顶Top【和数组下标对应】与栈最大长度StackSize是否相等

(2)如果不满足(1),将栈顶Top加一

(3)将待入栈数赋值给Top处的位置

3.出栈:

53 int Pop(SeqStack * S,DataType * ptr){
54     if(S->top==-1){
55         printf("下溢错误,删除失败
");
56         return 0;
57     }
58     *ptr = S->data[S->top--];
59     return *ptr;
60 }

(1)判断是否是空栈-----栈顶Top是否等于-1

(2)如果不满足(1),将栈顶Top减一

(3)被删元素通过指针参数ptr返回【由于是利用指针不需要return也会将该值传回给主函数

Tips:这里要将删除的元素返回,故先赋给*ptr后,栈顶Top加一

4.取栈顶:

61 int GetTop(SeqStack * S,DataType*ptr){
62     if(S->top==-1){
63         printf("下溢错误,取栈顶失败
");
64         return 0;
65     }
66     *ptr = S->data[S->top];
67     return 1;
68 }

(1)判断是否是空栈-----栈顶Top是否等于-1

(2)如果不满足(1),利用数组下标,将栈顶值传给*ptr【仅取值,并不修改栈顶位置

5.判空:

69 int Empty(SeqStack * S){
70     if(S->top==-1)
71         return 1;
72     else
73         return 0;
74 }

(1)判断是否是空栈-----栈顶Top是否等于-1

(2)依据(1)中判断的情况结果决定return的返回值

6.输出:

75 int Print(SeqStack * S){
76     printf("栈内元素:
");
77     for(int i=0;i<=S->top;i++){
78         printf("%d ",S->data[i]);
79     }
80     printf("
");
81 }

循环输出

****************************************************************

Tips:(1)通常把数组下标为零的一端作为栈底;(2)顺序栈的时间复杂度为 o(1);(3)顺序栈是静态存储分配;(4)由于(3),顺序栈变量退出作用域时,自动释放顺序栈所占存储单元,无需销毁

原文地址:https://www.cnblogs.com/wy0526/p/11788516.html