歡迎您光臨本站 註冊首頁

C++使用模板類實現鏈式棧

←手機掃碼閱讀     zmcjlove @ 2020-05-03 , reply:0

一、實現程序:
1.Stack.h
#ifndef Stack_h #define Stack_h template

class Stack { public: Stack(){}; // 構造函數 void Push(const T x); // 新元素進棧 bool Pop(); // 棧頂元素出棧 virtual bool getTop(T &x) const = 0; // 讀取棧頂元素,由x返回 virtual bool isEmpty() const = 0; // 判斷棧空否 // virtual bool isFull() const = 0; // 判斷棧滿否,因為鏈式棧不存在不滿的情況 virtual int getSize() const = 0; // 計算棧中元素個數 }; #endif /* Stack_h */
2.LinkedStack.h
#ifndef LinkedStack_h #define LinkedStack_h #include#include "Stack.h" using namespace std; templatestruct LinkNode { T data; LinkNode*link; }; //類的前置聲明 templateclass LinkedStack; //友元函數的聲明 templateostream& operator<<(ostream& out, LinkedStack& s); templateclass LinkedStack: public Stack{ public: LinkedStack(); // 構造函數 ~LinkedStack();// 析構函數 void Push(const T x); // 進棧 bool Pop(); // 出棧 bool getTop(T &x) const; // 讀取棧頂元素 bool isEmpty()const; // 判斷棧是否為空 int getSize()const; // 求棧的元素個數 void makeEmpty(); // 清空棧的內容 friend ostream& operator <<(ostream& out, LinkedStack& s); // 重載輸出函數 private: LinkNode*top; // 棧頂指針,即鏈頭指針 }; templateLinkedStack::LinkedStack() { // 構造函數,置空棧 top = new LinkNode(); // 引入頭指針:不存放數據 top->link = NULL; } templateLinkedStack::~LinkedStack() { // 析構函數,釋放內存空間 makeEmpty(); } templatevoid LinkedStack::Push(const T x) { // 進棧:將元素值x插入到鏈式棧的棧頂,即鏈頭 LinkNode*newNode = new LinkNode(); // 創建包含x的新結點 if(newNode == NULL) { cerr << "內存空間分配失敗!" << endl; exit(1); } newNode->data = x; newNode->link = top->link; // 指向頭指針的下一個結點:即棧中第一個存放有效數據的結點 top->link = newNode; // 頭指針往前移 } templatebool LinkedStack::Pop() { // 出棧:刪除棧頂結點 if(isEmpty()) return false; // 棧空,不出棧 LinkNode*p = top->link; // 暫存棧頂元素 top->link = p->link; // 棧頂指針退到新的棧頂位置 delete p; p = NULL; return true; } templatebool LinkedStack::getTop(T &x) const { // 讀取棧頂元素 if(isEmpty()) return false; x = top->link->data; // 棧不空,返回棧頂元素的值。這裡top為頭指針,所以棧頂元素為:top->link return true; } templatebool LinkedStack::isEmpty()const { // 判斷棧是否為空 if(top->link == NULL) // 棧為空 return true; return false; } templateint LinkedStack::getSize()const { // 求棧的元素個數 int len = 0; LinkNode*current = top->link; while(current != NULL) { len++; current = current->link; } return len; } templatevoid LinkedStack::makeEmpty() { // 清空棧的內容 LinkNode*current = top->link; while(current != NULL) { top->link = current->link; // 保存鏈式棧準備要刪除的結點的下一個結點,防止丟失 delete current; // 釋放 current = NULL; // 先指向空 current = top->link; // 再指向剩下鏈表的首結點 } } templateostream& operator<<(ostream& out, LinkedStack& s) { // 重載輸出函數 LinkNode*current = s.top->link; while(current != NULL) { out << current->data << " "; current = current->link; } return out; } #endif /* LinkedStack_h */
3.main.cpp
#include "LinkedStack.h" using namespace std; int main(int argc, const char * argv[]) { int n, x, choice, len; // val存儲值,choose存儲用戶的選擇 bool finished = false; LinkedStackL; // 對象 while(!finished) { cout << "1:建棧:" << endl; cout << "2:進棧" << endl; cout << "3:出棧:" << endl; cout << "4:讀取棧頂元素:" << endl; cout << "5:棧是否為空:" << endl; cout << "6:棧中的元素個數:" << endl; cout << "7:清空棧的內容:" << endl; cout << "8:輸出棧中元素的值:" << endl; cout << "9:退出" << endl; cout << "請輸入你的選擇[1-9]:" << endl; cin >> choice; switch(choice) { case 1: cout << "請輸入要進棧的數的個數:"; cin >> n; cout << "請輸入要進棧的數(以空格隔開):" << endl; for(int i=0; i < n; i++) { cin >> x; L.Push(x); } break; case 2: cout << "請輸入要進棧的數:"; cin >> x; L.Push(x); break; case 3: if(L.Pop()) cout << "出棧成功!" << endl; else cout << "棧為空!" << endl; break; case 4: if(L.getTop(x)) cout << "棧頂元素的值為:" << x << endl; else cout << "棧為空!" << endl; break; case 5: if(L.isEmpty()) cout << "棧為空!" << endl; else cout << "棧不為空!" << endl; break; case 6: len = L.getSize(); cout << "棧中的元素個數為:" << len << endl; break; case 7: L.makeEmpty(); // 清空棧 break; case 8: if(L.isEmpty()) cout << "棧為空!" << endl; else cout << L << endl; break; case 9: finished = true; break; default: cout << "輸入錯誤,請重新輸入!" << endl; } // switch } // while return 0; }

[zmcjlove ] C++使用模板類實現鏈式棧已經有423次圍觀

http://coctec.com/docs/c/language/show-post-232702.html