服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++利用链表模板类实现简易队列

C++利用链表模板类实现简易队列

2021-07-15 15:44魂灵序曲 C/C++

这篇文章主要为大家详细介绍了C++利用链表模板类实现一个简易队列,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++利用链表模板类实现一个队列的具体代码,供大家参考,具体内容如下

设计思想:MyQueue.h中对模板类进行声明和实现。首先定义结点的结构体,包含数据和指针域两部分。队列类定义中声明和实现了元素入队,出队,打印队首元素和队列等方法。

注意:

1)模板类的声明和定义不能分开(即不能分别放在.h和.cpp文件里)。

2)声明新节点时,如果声明的节点是辅助操作的,可以不用new关键字,例如在析构函数中,直接用:Node<T>* temp;定义即可。如果声明一个新节点加入队列,则要用new关键字,否则会报出nullptr异常。

​ConsoleApplication.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
#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();//获取队列元素数量
 myq.top();
 myq.top();
 myq.outPrint();
 myq.top();
 myq.top();
 myq.pop();
  return 0;
}

MyQueue.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
#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;//队列元素数量
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 << "析构函数已完成"<< endl;
 cout << "队列元素个数为" << count << endl;
 }
}
template<class T>
int MyQueue<T>::getCount(){
 cout << "队列元素个数为: "<<count<< endl;
 return count;
}
 
template<class T>
void MyQueue<T>::push(const T&e) {//元素从链表头入队列
 if (e <=0) {
 cout << "请输入正数值" << endl;
 return;
 }
 if (count == 0) {
 Node<T>*node = new Node<T>;//此处留意,声明新节点,不使用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++;//队列元素数量加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;
 }
}

运行结果:

C++利用链表模板类实现简易队列

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u014645508/article/details/78642997

延伸 · 阅读

精彩推荐