C++使用模板類實現(xiàn)鏈?zhǔn)綏?/h1>
更新時間:2020年04月27日 14:39:23 作者:ChanJose
這篇文章主要為大家詳細(xì)介紹了C++使用模板類實現(xiàn)鏈?zhǔn)綏?,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++使用模板類實現(xiàn)鏈?zhǔn)綏5木唧w代碼,供大家參考,具體內(nèi)容如下
一、實現(xiàn)程序:
1.Stack.h
#ifndef Stack_h
#define Stack_h
template <class T>
class Stack {
public:
Stack(){}; // 構(gòu)造函數(shù)
void Push(const T x); // 新元素進(jìn)棧
bool Pop(); // 棧頂元素出棧
virtual bool getTop(T &x) const = 0; // 讀取棧頂元素,由x返回
virtual bool isEmpty() const = 0; // 判斷??辗?
// virtual bool isFull() const = 0; // 判斷棧滿否,因為鏈?zhǔn)綏2淮嬖诓粷M的情況
virtual int getSize() const = 0; // 計算棧中元素個數(shù)
};
#endif /* Stack_h */
2.LinkedStack.h
#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;
//友元函數(shù)的聲明
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s);
template <class T>
class LinkedStack: public Stack<T> {
public:
LinkedStack(); // 構(gòu)造函數(shù)
~LinkedStack();// 析構(gòu)函數(shù)
void Push(const T x); // 進(jìn)棧
bool Pop(); // 出棧
bool getTop(T &x) const; // 讀取棧頂元素
bool isEmpty()const; // 判斷棧是否為空
int getSize()const; // 求棧的元素個數(shù)
void makeEmpty(); // 清空棧的內(nèi)容
friend ostream& operator << <T>(ostream& out, LinkedStack<T>& s); // 重載輸出函數(shù)
private:
LinkNode<T> *top; // 棧頂指針,即鏈頭指針
};
template <class T>
LinkedStack<T>::LinkedStack() {
// 構(gòu)造函數(shù),置空棧
top = new LinkNode<T>(); // 引入頭指針:不存放數(shù)據(jù)
top->link = NULL;
}
template <class T>
LinkedStack<T>::~LinkedStack() {
// 析構(gòu)函數(shù),釋放內(nèi)存空間
makeEmpty();
}
template <class T>
void LinkedStack<T>::Push(const T x) {
// 進(jìn)棧:將元素值x插入到鏈?zhǔn)綏5臈m敚存滎^
LinkNode<T> *newNode = new LinkNode<T>(); // 創(chuàng)建包含x的新結(jié)點
if(newNode == NULL) {
cerr << "內(nèi)存空間分配失??!" << endl;
exit(1);
}
newNode->data = x;
newNode->link = top->link; // 指向頭指針的下一個結(jié)點:即棧中第一個存放有效數(shù)據(jù)的結(jié)點
top->link = newNode; // 頭指針往前移
}
template <class T>
bool LinkedStack<T>::Pop() {
// 出棧:刪除棧頂結(jié)點
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 {
// 求棧的元素個數(shù)
int len = 0;
LinkNode<T> *current = top->link;
while(current != NULL) {
len++;
current = current->link;
}
return len;
}
template <class T>
void LinkedStack<T>::makeEmpty() {
// 清空棧的內(nèi)容
LinkNode<T> *current = top->link;
while(current != NULL) {
top->link = current->link; // 保存鏈?zhǔn)綏?zhǔn)備要刪除的結(jié)點的下一個結(jié)點,防止丟失
delete current; // 釋放
current = NULL; // 先指向空
current = top->link; // 再指向剩下鏈表的首結(jié)點
}
}
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s) {
// 重載輸出函數(shù)
LinkNode<T> *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;
LinkedStack<int> L; // 對象
while(!finished) {
cout << "1:建棧:" << endl;
cout << "2:進(jìn)棧" << endl;
cout << "3:出棧:" << endl;
cout << "4:讀取棧頂元素:" << endl;
cout << "5:棧是否為空:" << endl;
cout << "6:棧中的元素個數(shù):" << endl;
cout << "7:清空棧的內(nèi)容:" << endl;
cout << "8:輸出棧中元素的值:" << endl;
cout << "9:退出" << endl;
cout << "請輸入你的選擇[1-9]:" << endl;
cin >> choice;
switch(choice) {
case 1:
cout << "請輸入要進(jìn)棧的數(shù)的個數(shù):";
cin >> n;
cout << "請輸入要進(jìn)棧的數(shù)(以空格隔開):" << endl;
for(int i=0; i < n; i++) {
cin >> x;
L.Push(x);
}
break;
case 2:
cout << "請輸入要進(jìn)棧的數(shù):";
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 << "棧中的元素個數(shù)為:" << 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;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
c語言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
這篇文章主要介紹了c語言鏈表基本操作,大家參考使用吧 2013-12-12
-
C++歸并法+快速排序?qū)崿F(xiàn)鏈表排序的方法
這篇文章主要介紹了C++歸并法+快速排序?qū)崿F(xiàn)鏈表排序的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧 2021-04-04
-
盤點分析C語言中少見卻強(qiáng)大的字符串函數(shù)
這篇文章主要為大家盤點及分析C語言中少見卻強(qiáng)大的字符串函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步 2022-02-02
-
C++設(shè)計模式編程中Facade外觀模式的使用實例解析
這篇文章主要介紹了C++設(shè)計模式編程中Facade外觀模式的使用實例解析,外觀模式的主要用途就是為子系統(tǒng)的復(fù)雜處理過程提供方便的調(diào)用方法,需要的朋友可以參考下 2016-03-03
最新評論
本文實例為大家分享了C++使用模板類實現(xiàn)鏈?zhǔn)綏5木唧w代碼,供大家參考,具體內(nèi)容如下
一、實現(xiàn)程序:
1.Stack.h
#ifndef Stack_h
#define Stack_h
template <class T>
class Stack {
public:
Stack(){}; // 構(gòu)造函數(shù)
void Push(const T x); // 新元素進(jìn)棧
bool Pop(); // 棧頂元素出棧
virtual bool getTop(T &x) const = 0; // 讀取棧頂元素,由x返回
virtual bool isEmpty() const = 0; // 判斷??辗?
// virtual bool isFull() const = 0; // 判斷棧滿否,因為鏈?zhǔn)綏2淮嬖诓粷M的情況
virtual int getSize() const = 0; // 計算棧中元素個數(shù)
};
#endif /* Stack_h */
2.LinkedStack.h
#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;
//友元函數(shù)的聲明
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s);
template <class T>
class LinkedStack: public Stack<T> {
public:
LinkedStack(); // 構(gòu)造函數(shù)
~LinkedStack();// 析構(gòu)函數(shù)
void Push(const T x); // 進(jìn)棧
bool Pop(); // 出棧
bool getTop(T &x) const; // 讀取棧頂元素
bool isEmpty()const; // 判斷棧是否為空
int getSize()const; // 求棧的元素個數(shù)
void makeEmpty(); // 清空棧的內(nèi)容
friend ostream& operator << <T>(ostream& out, LinkedStack<T>& s); // 重載輸出函數(shù)
private:
LinkNode<T> *top; // 棧頂指針,即鏈頭指針
};
template <class T>
LinkedStack<T>::LinkedStack() {
// 構(gòu)造函數(shù),置空棧
top = new LinkNode<T>(); // 引入頭指針:不存放數(shù)據(jù)
top->link = NULL;
}
template <class T>
LinkedStack<T>::~LinkedStack() {
// 析構(gòu)函數(shù),釋放內(nèi)存空間
makeEmpty();
}
template <class T>
void LinkedStack<T>::Push(const T x) {
// 進(jìn)棧:將元素值x插入到鏈?zhǔn)綏5臈m敚存滎^
LinkNode<T> *newNode = new LinkNode<T>(); // 創(chuàng)建包含x的新結(jié)點
if(newNode == NULL) {
cerr << "內(nèi)存空間分配失??!" << endl;
exit(1);
}
newNode->data = x;
newNode->link = top->link; // 指向頭指針的下一個結(jié)點:即棧中第一個存放有效數(shù)據(jù)的結(jié)點
top->link = newNode; // 頭指針往前移
}
template <class T>
bool LinkedStack<T>::Pop() {
// 出棧:刪除棧頂結(jié)點
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 {
// 求棧的元素個數(shù)
int len = 0;
LinkNode<T> *current = top->link;
while(current != NULL) {
len++;
current = current->link;
}
return len;
}
template <class T>
void LinkedStack<T>::makeEmpty() {
// 清空棧的內(nèi)容
LinkNode<T> *current = top->link;
while(current != NULL) {
top->link = current->link; // 保存鏈?zhǔn)綏?zhǔn)備要刪除的結(jié)點的下一個結(jié)點,防止丟失
delete current; // 釋放
current = NULL; // 先指向空
current = top->link; // 再指向剩下鏈表的首結(jié)點
}
}
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s) {
// 重載輸出函數(shù)
LinkNode<T> *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;
LinkedStack<int> L; // 對象
while(!finished) {
cout << "1:建棧:" << endl;
cout << "2:進(jìn)棧" << endl;
cout << "3:出棧:" << endl;
cout << "4:讀取棧頂元素:" << endl;
cout << "5:棧是否為空:" << endl;
cout << "6:棧中的元素個數(shù):" << endl;
cout << "7:清空棧的內(nèi)容:" << endl;
cout << "8:輸出棧中元素的值:" << endl;
cout << "9:退出" << endl;
cout << "請輸入你的選擇[1-9]:" << endl;
cin >> choice;
switch(choice) {
case 1:
cout << "請輸入要進(jìn)棧的數(shù)的個數(shù):";
cin >> n;
cout << "請輸入要進(jìn)棧的數(shù)(以空格隔開):" << endl;
for(int i=0; i < n; i++) {
cin >> x;
L.Push(x);
}
break;
case 2:
cout << "請輸入要進(jìn)棧的數(shù):";
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 << "棧中的元素個數(shù)為:" << 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;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c語言鏈表基本操作(帶有創(chuàng)建鏈表 刪除 打印 插入)
這篇文章主要介紹了c語言鏈表基本操作,大家參考使用吧2013-12-12
C++歸并法+快速排序?qū)崿F(xiàn)鏈表排序的方法
這篇文章主要介紹了C++歸并法+快速排序?qū)崿F(xiàn)鏈表排序的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
盤點分析C語言中少見卻強(qiáng)大的字符串函數(shù)
這篇文章主要為大家盤點及分析C語言中少見卻強(qiáng)大的字符串函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
C++設(shè)計模式編程中Facade外觀模式的使用實例解析
這篇文章主要介紹了C++設(shè)計模式編程中Facade外觀模式的使用實例解析,外觀模式的主要用途就是為子系統(tǒng)的復(fù)雜處理過程提供方便的調(diào)用方法,需要的朋友可以參考下2016-03-03

