目錄
-
棧
FILO,即先進後出,類似我們去取一摞碗,我們是不是從最上面的那個碗開始取,而我們用完洗淨後放回去的第一個碗被最後拿了出來,但是最後放進去的被最先拿了出來。
-
順序存儲
順序存儲結構的主要優點是節省存儲空間,因爲分配給數據的存儲單元全用存放結點的數據(不考慮c/c++語言中數組需指定大小的情況),結點之間的邏輯關係沒有佔用額外的存儲空間。採用這種方法時,可實現對結點的隨機存取,即每一個結點對應一個序號,由該序號可以直接計算出來結點的存儲地址。但順序存儲方法的主要缺點是不便於修改,對結點的插入、刪除運算時,可能要移動一系列的結點。
優點:隨機存取表中元素、儲存密度大。
缺點:插入和刪除操作需要移動元素。
-
定義基本數據結構
typedef char ElemetType; //這裏的char的數據類型可以根據自己需要設置 const int Max_dataSize = 512; //定義數組的大小,用define也可以,但在c++中不提倡這麼寫 typedef struct Dstack //定義棧的結構體 { ElemetType Data[Max_dataSize]; int top; }st_stack;
-
初始化
void Init(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } s_ptr->top = -1; //棧空,top = -1 }
-
入棧
void Push(st_stack* s_ptr, ElemetType data) { if (NULL == s_ptr) { exit(0); } if (Max_dataSize == s_ptr->top) //判棧是否滿 { cout << "棧滿" << endl; exit(0); } s_ptr->Data[++(s_ptr->top)] = data; //依次入棧 }
-
出棧
void Pop(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } if (-1 == s_ptr->top) //判空 { cout << "棧空" << endl; exit(0); } while (s_ptr->top != -1) { cout << s_ptr->Data[(s_ptr->top)--] << endl; //棧的輸出元素 } }
-
判棧是否爲空
bool IsEmpty(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } if (-1 == s_ptr->top) //判空 { return true; } else return false; }
-
把棧置空
bool SetEmpty(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } while (s_ptr->top != -1) { if (-1 == s_ptr->top) { cout << "棧空" << endl; } s_ptr->Data[s_ptr->top] = -1; s_ptr->top--; } if (-1 == s_ptr->top) { return true; } else return false; }
-
獲取棧頂元素
ElemetType GetTop(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } return s_ptr->Data[s_ptr->top]; }
-
頭文件
#ifndef __SATCK_H__ //防止頭文件重複包含 #include <iostream> #include <string> using namespace std; typedef char ElemetType; const int Max_dataSize = 512; typedef struct Dstack { ElemetType Data[Max_dataSize]; int top; }st_stack; void Init(st_stack* s_ptr); void Push(st_stack* s_ptr, ElemetType data); void Pop(st_stack* s_ptr); ElemetType GetTop(st_stack* s_ptr); bool SetEmpty(st_stack* s_ptr); #endif // !__SATCK_H__
-
源文件
#include "stack.h" void Init(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } s_ptr->top = -1; } bool IsEmpty(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } if (-1 == s_ptr->top) { return true; } else return false; } void Push(st_stack* s_ptr, ElemetType data) { if (NULL == s_ptr) { exit(0); } if (Max_dataSize == s_ptr->top) { cout << "棧滿" << endl; exit(0); } s_ptr->Data[++(s_ptr->top)] = data; } void Pop(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } if (-1 == s_ptr->top) { cout << "棧空" << endl; exit(0); } while (s_ptr->top != -1) { cout << s_ptr->Data[(s_ptr->top)--] << endl; } } ElemetType GetTop(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } return s_ptr->Data[s_ptr->top]; } bool SetEmpty(st_stack* s_ptr) { if (NULL == s_ptr) { exit(0); } while (s_ptr->top != -1) { if (-1 == s_ptr->top) { cout << "棧空" << endl; } s_ptr->Data[s_ptr->top] = -1; s_ptr->top--; } if (-1 == s_ptr->top) { return true; } else return false; } void test() { ElemetType val = -1; st_stack* arr = new st_stack; Init(arr); if (IsEmpty(arr)) { cout << "棧空" << endl; } else { cout << "棧不爲空" << endl; } Push(arr, 'A'); Push(arr, 'B'); Push(arr, 'C'); Push(arr, 'D'); if (IsEmpty(arr)) { cout << "棧空" << endl; } else cout << "棧不爲空" << endl; val = GetTop(arr); cout << "棧頂元素爲:" << val << endl; Pop(arr); if (SetEmpty(arr)) { cout << "置空成功" << endl; } else cout << "置空失敗" << endl; if (IsEmpty(arr)) { cout << "棧空" << endl; } else cout << "棧不爲空" << endl; } int main(int argc, char** argv) { test(); system("pause"); return 0; }
-
運行結果
-
運行結果分析
- 第一次判斷爲空,只是初始化爲push;
- push之後棧不爲空;(入棧順序ABCD)
- 獲取棧頂元素;
- 出棧;
- 把棧置空;
- 判斷棧是否爲空。
注:如有不足及需要改進的地方,敬請指正,謝謝!