本文實例為大家分享了C++使用模板類實現鏈式棧的具體代碼,供大家參考,具體內容如下
一、實現程序:
1.Stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#ifndef Stack_h #define Stack_h template < class T> 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#ifndef LinkedStack_h #define LinkedStack_h #include <iostream> #include "Stack.h" using namespace std; template < class T> struct LinkNode { T data; LinkNode<T> *link; }; //類的前置聲明 template < class T> class LinkedStack; //友元函數的聲明 template < class T> ostream& operator<<(ostream& out, LinkedStack<T>& s); template < class T> class LinkedStack: public Stack<T> { 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 << <T>(ostream& out, LinkedStack<T>& s); // 重載輸出函數 private : LinkNode<T> *top; // 棧頂指針,即鏈頭指針 }; template < class T> LinkedStack<T>::LinkedStack() { // 構造函數,置空棧 top = new LinkNode<T>(); // 引入頭指針:不存放數據 top->link = NULL; } template < class T> LinkedStack<T>::~LinkedStack() { // 析構函數,釋放內存空間 makeEmpty(); } template < class T> void LinkedStack<T>::Push( const T x) { // 進棧:將元素值x插入到鏈式棧的棧頂,即鏈頭 LinkNode<T> *newNode = new LinkNode<T>(); // 創建包含x的新結點 if (newNode == NULL) { cerr << "內存空間分配失敗!" << endl; exit (1); } newNode->data = x; newNode->link = top->link; // 指向頭指針的下一個結點:即棧中第一個存放有效數據的結點 top->link = newNode; // 頭指針往前移 } template < class T> bool LinkedStack<T>::Pop() { // 出棧:刪除棧頂結點 if (isEmpty()) return false ; // 棧空,不出棧 LinkNode<T> *p = top->link; // 暫存棧頂元素 top->link = p->link; // 棧頂指針退到新的棧頂位置 delete p; p = NULL; return true ; } template < class T> bool LinkedStack<T>::getTop(T &x) const { // 讀取棧頂元素 if (isEmpty()) return false ; x = top->link->data; // 棧不空,返回棧頂元素的值。這里top為頭指針,所以棧頂元素為:top->link return true ; } template < class T> bool LinkedStack<T>::isEmpty() const { // 判斷棧是否為空 if (top->link == NULL) // 棧為空 return true ; return false ; } template < class T> int LinkedStack<T>::getSize() const { // 求棧的元素個數 int len = 0; LinkNode<T> *current = top->link; while (current != NULL) { len++; current = current->link; } return len; } template < class T> void LinkedStack<T>::makeEmpty() { // 清空棧的內容 LinkNode<T> *current = top->link; while (current != NULL) { top->link = current->link; // 保存鏈式棧準備要刪除的結點的下一個結點,防止丟失 delete current; // 釋放 current = NULL; // 先指向空 current = top->link; // 再指向剩下鏈表的首結點 } } template < class T> ostream& operator<<(ostream& out, LinkedStack<T>& s) { // 重載輸出函數 LinkNode<T> *current = s.top->link; while (current != NULL) { out << current->data << " " ; current = current->link; } return out; } #endif /* LinkedStack_h */ |
3.main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include "LinkedStack.h" using namespace std; int main( int argc, const char * argv[]) { int n, x, choice, len; // val存儲值,choose存儲用戶的選擇 bool finished = false ; LinkedStack< int > L; // 對象 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; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/chuanzhouxiao/article/details/86081748