C++实现的链表队列
来源:互联网 发布:c语言prime 编辑:程序博客网 时间:2024/05/18 00:47
<pre code_snippet_id="1954965" snippet_file_name="blog_20161028_1_5985205" name="code" class="cpp">//头文件如下
#ifndef QUEUE_H#define QUEUE_H#include<iostream>template<class T>class Queue{private: struct Node { T item; Node *next; }; Node *head = NULL; Node *tail = NULL; int N = 0; //记录队列中有多少元素
public:
Queue(){}; bool isEmpty() {return head == NULL;}; int size() {return N;}; void push(T t); T pop(); ~Queue();};
template<class T>void Queue<T>::push(T t){ Node *n = new Node; n->item = t; n->next = NULL; if(isEmpty()) head = tail = n; else{ tail->next = n; tail = n; } N++;}template<class T>T Queue<T>::pop() { T result = head->item; Node *d = head; head = head->next; N--; if(isEmpty()) tail = NULL; delete d; // 注意避免内存泄漏 return result;}template<class T>Queue<T>::~Queue(){ while(!isEmpty()) //通过遍历每个节点的方式来删除节点 { Node *n = head; head = head->next; N--; delete n; } tail = NULL; // 记得将尾节点设置为空}#endif
0 0
- 【C++】队列的实现
- 双向链表、队列与栈的C/C++实现
- c语言实现多线程下的链表队列
- 队列-C语言-链表的实现方式
- 队列的C语言实现(通过内核链表)
- 链队列的实现 C语言
- 链队列的C语言实现
- 链队列的C语言实现
- 链队列的c语言实现
- c语言:链队列的实现
- Linux C下链队列的实现
- 链队列的C/C++实现
- 链队列的c语言实现
- 数据结构---队列链表c实现
- C语言链表实现队列操作
- 链表实现队列 c语言
- 队列的C语言实现
- C语言队列的实现
- Linux常用命令
- Noip 2013 codevs 3286 火柴排队
- C语言学习笔记之数组
- 文本分类(六):使用fastText对文本进行分类--小插曲
- Android之内容提供器Content Provider详解(二)
- C++实现的链表队列
- AssetManager的释放跟踪
- My First CSDN
- java中的gridbaglayout的简单用法
- 全排列(STL)
- 【PS】字体
- 【Kruskal】 洛谷1991 无线通讯网
- Flower Pots (CTU Open 2013 E题)
- shell sleep的使用