棧的實現——連結串列和陣列
阿新 • • 發佈:2018-12-23
C語言(列印函式採用的c++):
棧的連結串列實現—— 棧的初始化(建立||清空)、入棧、出棧(獲取棧頂元素)
棧的陣列實現——初始化、入棧、出棧、清空棧
參考資料:《資料結構與演算法分析——C語言描述》 P46
一. 棧的連結串列實現
StackLinkList.cpp
- /*
- 功能:棧的連結串列實現: 棧的初始化(建立||清空)、入棧、出棧(獲取棧頂元素)
- Date: 2015/01/23
- Author : quinn
- */
- #include <iostream>
- #include <stdlib.h>
- usingnamespace std;
-
typedef
- typedefstruct Node Node;
- typedef Node* Stack;
- struct Node
- {
- Item item;
- Node* next;
- };
- void StackPop(Stack s);
- Stack StackInit(Stack s) //建立或清空(初始化)
- {
- if (s == NULL) //建立
- {
- s = (Stack)malloc(sizeof(*s));
- s->next = NULL;
- }
-
else
- while(s->next != NULL)
- StackPop(s);
- cout << "初始化成功!" <<endl;
- return s;
- }
- void StackPush(Stack s, Item item) //入棧
- {
- Node* tmpNode = (Node*)malloc(sizeof(*tmpNode));
- tmpNode->item = item;
- tmpNode->next = s->next;
-
s->next = tmpNode;
- cout << "PUSH: " << item <<endl;
- }
- Item StackPopAndTop(Stack s) //出棧並返回棧頂元素值
- {
- if (s->next == NULL)
- {
- cout << "空棧,POPAndTop失敗" <<endl;
- return -1; //返回-1作警告
- }
- else
- {
- Node* firstNode = s->next;
- s->next = s->next->next;
- return firstNode->item;
- }
- }
- void StackPop(Stack s) //出棧
- {
- if (s->next == NULL)
- {
- cout << "空棧,POP失敗" <<endl;
- }
- else
- {
- Node* firstNode = s->next;
- s->next = s->next->next;
- free(firstNode);
- }
- }
- Item StackTop(Stack s) //僅獲取棧頂元素
- {
- if (s->next == NULL)
- {
- cout << "空棧,獲取棧頂元素失敗" <<endl;
- }
- return (s->next)->item;
- }
- int main()
- {
- Stack s = NULL;
- s = StackInit(s);
- for(int i = 0; i < 10; i++)
- StackPush(s, i);
- for(int i = 0; i < 5; i++)
- cout << StackPopAndTop(s) <<endl;
- StackInit(s);
- cout << StackPopAndTop(s) <<endl;
- system("pause");
- return 0;
- }
二. 棧的陣列實現
StackArray.cpp- /*
- 功能:棧的陣列實現——初始化、入棧、出棧、清空棧
- 注: 定義棧為空時,棧頂index為 -1;棧滿時,棧頂index為棧的長度-1
- Date:2015/01/23
- Author : quinn
- */
- #include <iostream>
- #include "item.h"
- #define STACK_SIZE 10 //認為設定棧的長度為10
- #define FULL_STACK (STACK_SIZE - 1)
- #define EMPTY_STACK (-1)
- usingnamespace std;
- typedefstruct stack stack;
- typedefint Item;
- struct stack
- {
- Item *stackItem;
- int stackTop;
- };
- void Error(constchar* str) //異常時輸出提示
- {
- cout << "Error: " << str << endl;
- }
- int IsFull(stack* s) //判斷是否棧滿
- {
- if (s->stackTop == FULL_STACK)
- return 1;
- else
- return 0;
- }
- int IsEmpty(stack* s) //判斷是否空棧
- {
- if (s->stackTop == EMPTY_STACK)
- return 1;
- else
- return 0;
- }
- stack* StackInit( stack* s, int maxN) //初始化棧空間
- {
- s->stackTop = -1; //空棧初始化 Top = -1
- s->stackItem = (Item*)malloc(maxN * sizeof(Item)); //分配棧空間
- return s;
- }
- void StackMakeEmpty(stack* s) //清空棧
- {
- if (!IsEmpty(s))
- s->stackTop = EMPTY_STACK;
- }
- void StackPush(stack* s, Item item) //入棧
- {
- if (!IsFull(s))
- {
- s->stackItem[++(s->stackTop)] = item;
- cout << "PUSH: " << item <<endl;
- }
- else
- Error("棧已滿,push失敗");
- }
- Item StackPop(stack *s) //出棧
- {
- if (!IsEmpty(s))
- {
- return s->stackItem[(s->stackTop)--];
- }
- else
- Error("空棧,pop失敗");
- }
- int main()
- {
- stack *s = (stack*)malloc(sizeof(*s));
- StackInit(s, STACK_SIZE);
- for (int i = 0; i < 11; i++)
- {
- StackPush(s, i);
- }
- cout << "POP: " << StackPop(s) << endl;
- cout << "POP: " << StackPop(s) << endl;
- StackPush(s, 20);
- cout << "POP: " << StackPop(s) << endl;
- StackMakeEmpty(s);
- StackPop(s);
- StackPush(s, 30);
- cout << "POP: " << StackPop(s) << endl;