順序堆棧和鏈式堆棧的實現,用一個數組實現兩個堆棧的例子

 

順序堆棧的實現:數組

#include <stdio.h>
#include <stdlib.h> #include <stdbool.h> #define ERROR -999 typedef int ElementType; typedef int Position; typedef struct SNode* PtrToNode; struct SNode { ElementType *Data; //存儲元素的數組 Position top; //棧頂指針 int MaxSize; //堆棧最大容量 }; typedef PtrToNode Stack; //生成一個空棧 Stack CreateStack(int MaxSize) { Stack S = (Stack)malloc(sizeof(struct SNode)); S->Data = (ElementType*)malloc(sizeof(sizeof(struct SNode) * MaxSize)); S->top = -1; S->MaxSize = MaxSize; return S; } //判斷一個棧是否已滿 bool IsFull(Stack S) { return S->top == S->MaxSize - 1; } //判斷一個棧是否爲空 bool IsEmpty(Stack S) { return S->top == -1;    //這裏我已開始寫成了return S->top = -1;因此一直得不到正確結果 } //壓棧,返回是否壓棧成功 bool Push(Stack S, ElementType X) { if (IsFull(S)) { printf("The stack is full!\n"); return false; } else { S->Data[++(S->top)] = X; return true; } } //出棧,彈棧,返回彈出的元素 ElementType Pop(Stack S) { if (IsEmpty(S)) { printf("The Stack is Empty!\n"); return ERROR; } else return (S->Data[(S->top)--]); }
剛開始對Pop操做的返回值語句return (S->Data[(S->top)--]);存在一點疑惑,爲何程序已經把S->Data[S->top]返回回去了,
S->top還會執行自減操做(我一直認爲後置版本的--運算符執行的操做是,先返回-1前的原值,再原值-1),事實上這個運算符是先把原值拷貝一份,而後原值-1,
最後返回副本,因此這條返回語句是沒有問題的,由於返回副本以前原值就已經-1了

--運算符的重載實現爲:函數

1 ClassA operator -- (int a)        //爲了與前置版本相區別,參數列表中加上int a,可是int a在函數體中不起任何做用,只起區分的做用
2 {
3     ClassA copy = *this; 4 --*this; //先自加 5 return copy; //再返回前置版本 6 }

 

鏈式堆棧的實現:this

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4 
 5 #define ERROR -999
 6 
 7 typedef int ElementType;
 8 typedef struct SNode* PtrToSNode;
 9 struct SNode {
10     ElementType Data;
11     PtrToSNode Next;
12 };
13 
14 typedef PtrToSNode Stack;
15 
16 Stack CreateStack()
17 {
18     Stack S = (Stack)malloc(sizeof(struct SNode));
19     S->Next = NULL;
20     return S;
21 }
22 
23 bool IsEmpty(Stack S)
24 {
25     return (S->Next == NULL);
26 }
27 
28 bool Push(Stack S, ElementType X)
29 {
30     PtrToSNode TmpCell;
31 
32     TmpCell = (PtrToSNode)malloc(sizeof(struct SNode));
33     TmpCell->Data = X;
34     TmpCell->Next = S->Next;
35     S->Next = TmpCell;
36     return true;
37 }
38 
39 ElementType Pop(Stack S)
40 {
41     PtrToSNode FirstCell;
42     ElementType TopElem;
43     if (IsEmpty(S))
44     {
45         printf("Stack is empty!\n");
46         return ERROR;
47     }
48     else
49     {
50         FirstCell = S->Next;
51         TopElem = FirstCell -> Data;
52         S->Next = FirstCell->Next;
53         free(FirstCell);
54         return TopElem;
55     }
56 }
57 
58 int main()
59 {
60     Stack S = CreateStack();
61     Push(S, 3);
62     int a = Pop(S);
63     printf("%d\n", a);
64     return 0;
65 
66 }

 

用一個數組實現兩個堆棧的例子:spa

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <stdbool.h>
 4 
 5 #define ERROR -999
 6 
 7 typedef int ElementType;
 8 typedef int Position;
 9 typedef struct SNode* PtrToNode;
10 struct SNode {
11     ElementType *Data;    //存儲元素的數組
12     Position top1;        //棧頂指針
13     Position top2;
14     int MaxSize;        //堆棧最大容量
15 };
16 typedef PtrToNode Stack;
17 
18 //生成一個空棧
19 Stack CreateStack(int MaxSize)
20 {
21     Stack S = (Stack)malloc(sizeof(struct SNode));
22     S->Data = (ElementType*)malloc(sizeof(sizeof(struct SNode) * MaxSize));
23     S->top1 = -1;
24     S->top2 = MaxSize;
25     S->MaxSize = MaxSize;
26     return S;
27 }
28 
29 
30 //壓棧,返回是否壓棧成功
31 bool Push(Stack S, ElementType X, int Tag)
32 {
33     if (S->top2 - S->top1 == 1)
34     {
35         printf("The stack is full!\n");
36         return false;
37     }
38     else
39     {
40         if (Tag == 1)
41             S->Data[++(S->top1)] = X;
42         else
43             S->Data[--(S->top2)] = X;
44         return true;
45     }
46 }
47 
48 //出棧,彈棧,返回彈出的元素
49 ElementType Pop(Stack S, int Tag)
50 {
51     if (Tag == 1)
52     {
53         if (S->top1 == -1)
54         {
55             printf("The Stack is Empty!\n");
56             return ERROR;
57         }
58         else
59             return S->Data[(S->top1)--];
60     }
61     else
62     {
63         if (S->top2 == S->MaxSize)
64         {
65             printf("Stack 2 is empty!\n");
66             return ERROR;
67         }
68         else
69             return S->Data[(S->top2)++];
70     }
71 }
72 
73 
74 int main()
75 {
76     Stack S = CreateStack(10);
77     Push(S, 2, 1);
78     int a = Pop(S, 1);
79     Push(S, 3, 2);
80     int b = Pop(S, 2);
81     printf("%d %d\n", a, b);
82     return 0;
83 }
相關文章
相關標籤/搜索