順序棧的基本操作-完整代碼和拆開分析


 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執行入棧操作:\n");
21     Push(&S,15);
22     Print(&S);
23     Push(&S,10);
24     Print(&S);
25     if(GetTop(&S,&x)==1)
26         printf("當前棧頂元素為:%d\n",x);
27     if(Pop(&S,&x)==1)
28         printf("執行一次出棧操作,刪除元素:%d\n",x);
29     if(GetTop(&S,&x)==1){
30         printf("當前棧頂元素為:%d\n",x);
31     }
32     printf("請輸入待入棧元素:");
33     scanf("%d",&x);
34     Push(&S,x);
35     if(Empty(&S)==1)
36         printf("棧為空\n");
37     else
38         printf("棧非空\n");
39     return 0;
40 }
41 
42 void InitStack(SeqStack * S){
43     S->top=-1;
44     printf("初始化成功!\n");
45 }
46 void Push(SeqStack * S,DataType x){
47     if(S->top==StackSize-1){
48         printf("上溢錯誤,插入失敗\n");
49     }
50     S->data[++S->top]=x;
51     printf("入棧成功!\n"); 
52 }
53 int Pop(SeqStack * S,DataType * ptr){
54     if(S->top==-1){
55         printf("下溢錯誤,刪除失敗\n");
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("下溢錯誤,取棧頂失敗\n");
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("棧內元素:\n");
77     for(int i=0;i<=S->top;i++){
78         printf("%d ",S->data[i]);
79     }
80     printf("\n");
81 }

 1.初始化:

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

將順序棧頂Top設為-1

2.入棧:

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

(1)判斷棧是否已滿-----棧頂Top【和數組下標對應】與棧最大長度StackSize是否相等

(2)如果不滿足(1),將棧頂Top加一

(3)將待入棧數賦值給Top處的位置

3.出棧:

53 int Pop(SeqStack * S,DataType * ptr){
54     if(S->top==-1){
55         printf("下溢錯誤,刪除失敗\n");
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("下溢錯誤,取棧頂失敗\n");
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("棧內元素:\n");
77     for(int i=0;i<=S->top;i++){
78         printf("%d ",S->data[i]);
79     }
80     printf("\n");
81 }

循環輸出

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

Tips:(1)通常把數組下標為零的一端作為棧底;(2)順序棧的時間復雜度為 o(1);(3)順序棧是靜態存儲分配;(4)由於(3),順序棧變量退出作用域時,自動釋放順序棧所占存儲單元,無需銷毀


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM