C++利用鏈表模板類實現(xiàn)簡易隊列
本文實例為大家分享了C++利用鏈表模板類實現(xiàn)一個隊列的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)計思想:MyQueue.h中對模板類進行聲明和實現(xiàn)。首先定義結(jié)點的結(jié)構(gòu)體,包含數(shù)據(jù)和指針域兩部分。隊列類定義中聲明和實現(xiàn)了元素入隊,出隊,打印隊首元素和隊列等方法。
注意:
1)模板類的聲明和定義不能分開(即不能分別放在.h和.cpp文件里)。
2)聲明新節(jié)點時,如果聲明的節(jié)點是輔助操作的,可以不用new關(guān)鍵字,例如在析構(gòu)函數(shù)中,直接用:Node<T>* temp;定義即可。如果聲明一個新節(jié)點加入隊列,則要用new關(guān)鍵字,否則會報出nullptr異常。
ConsoleApplication.cpp
#include "stdafx.h"
#include "MyQueue.h"
int main()
{
MyQueue<int>myq ;
int a[] = { 3,59,21,54,7 };
for (int i = 0; i < 5; i++) {
myq.push(a[i]);
}
myq.outPrint();// 打印隊列
myq.top();//彈出隊首元素
myq.pop();//打印隊首元素
myq.top();
myq.getCount();//獲取隊列元素數(shù)量
myq.top();
myq.top();
myq.outPrint();
myq.top();
myq.top();
myq.pop();
return 0;
}
MyQueue.h
#include <iostream>
using namespace std;
template<class T>
struct Node{
T data;
Node<T> * next;
};
template<class T>
class MyQueue{
private:
Node<T>* head;//頭指針
int count;//隊列元素數(shù)量
public:
MyQueue();
~MyQueue();
void outPrint();//打印隊列元素
void push(const T &e);//元素入隊
void top();// 返回隊首元素
void pop();//彈出隊首元素
int getCount();
};
template <class T>
MyQueue<T>::MyQueue(){
count = 0;
head = nullptr;
}
template <class T>
MyQueue<T>::~MyQueue(){
if (head == nullptr) {
cout << "已為空隊列"<< endl;
}
else {
Node<T> *temp;
while (head!= nullptr) {
temp = head;
head = head->next;
delete temp;
count -= 1;
}
cout << "析構(gòu)函數(shù)已完成"<< endl;
cout << "隊列元素個數(shù)為" << count << endl;
}
}
template<class T>
int MyQueue<T>::getCount(){
cout << "隊列元素個數(shù)為: "<<count<< endl;
return count;
}
template<class T>
void MyQueue<T>::push(const T&e) {//元素從鏈表頭入隊列
if (e <=0) {
cout << "請輸入正數(shù)值" << endl;
return;
}
if (count == 0) {
Node<T>*node = new Node<T>;//此處留意,聲明新節(jié)點,不使用new初始化會報錯
node->data = e;
node->next = nullptr;
head = node;
}
else {
Node<T>* temp = head;
for (int i = 0; i < count - 1;i++) {
temp = temp->next;
}
Node<T>* node=new Node<T>;
temp->next=node;
node->data = e;
node->next = nullptr;
}
count++;//隊列元素數(shù)量加1
}
template <class T>
void MyQueue<T>::outPrint() {
if (head == nullptr) {
cout << "空隊列" << endl;
}
else {
Node<T>* temp=head;
cout << "隊列元素為:";
while (temp!=nullptr) {
cout << temp->data<<" ";
temp = temp->next;
}
cout << endl;
}
}
template <class T>
void MyQueue<T>::top() {
if (head == nullptr) {
cout << "這是空隊列,無隊首元素。"<< endl;
}
else {
Node<T>* temp;
temp = head;
head = head->next;
cout << "彈出隊首元素:" <<temp->data<< endl;
delete temp;
count -= 1;
}
}
template <class T>
void MyQueue<T>::pop() {
if (head == nullptr) {
cout << "此為空隊列,無隊首元素。" << endl;
return;
}
else {
cout << "隊首元素為:" << head->data << endl;
}
}
運行結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:Windows注冊表中修改UAC(用戶賬號控制)及批處理腳本
欄 目:C語言
下一篇:C++11關(guān)于auto關(guān)鍵字的使用示例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/566.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10深入理解鏈表的各類操作詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10用C語言實現(xiàn)單鏈表的各種操作(一)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


