最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年09月27日 10:18:04   作者:魂靈序曲  
這篇文章主要為大家詳細(xì)介紹了C++利用鏈表模板類實現(xiàn)一個簡易隊列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++利用鏈表模板類實現(xiàn)一個隊列的具體代碼,供大家參考,具體內(nèi)容如下

設(shè)計思想:MyQueue.h中對模板類進(jìn)行聲明和實現(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

孟州市| 和田县| 册亨县| 乐亭县| 平罗县| 营口市| 孝感市| 赣榆县| 抚宁县| 上杭县| 河间市| 武宁县| 万全县| 江北区| 龙井市| 保山市| 汉阴县| 多伦县| 厦门市| 垫江县| 南漳县| 年辖:市辖区| 汝州市| 吉木乃县| 池州市| 德格县| 三江| 惠安县| 齐齐哈尔市| 汉沽区| 玉环县| 桐乡市| 依安县| 永和县| 大洼县| 甘洛县| 宁河县| 将乐县| 边坝县| 溆浦县| 大港区|