雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++利用鏈表模板類實現(xiàn)簡易隊列

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

本文實例為大家分享了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)鍵字的使用示例

本文標題:C++利用鏈表模板類實現(xiàn)簡易隊列

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/566.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有