两种方法实现队列---顺序表和链表
2016-08-07 21:03
330 查看
队列(queue)在计算机科学中,是一种先进先出的线性表。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。队列主要有push()(尾插),pop()(头删),size()(求队列大小),empty()(是否为空队列)几个基本的操作函数。
顺序表实现队列:
顺序表代码实现:
链表实现:
链表代码实现:
#include <iostream>
#include <assert.h>
using namespace std;
template<typename T>
struct Node
{
T _data;
Node<T>* _next;
Node(const T& x)
:_data(x)
, _next(NULL)
{}
};
template<typename T>
class Queue
{
public:
Queue()
:_head(NULL)
, _tail(NULL)
, _size(0)
{}
~Queue()
{}
void Push(const T& x)
{
if (_head == NULL)
{
_head = _tail = new Node<T>(x);
}
else
{
/*Node<T>* _tmp = new Node<T>*(x);
_tail->_next = _tmp;
_tail = _tmp;*/
_tail->_next = new Node<T>(x);
_tail = _tail->_next;
}
_size++;
}
void Pop()
{
assert(_head);
if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node<T>* del = _head;
_head = _head->_next;
delete del;
}
--_size;
}
bool Empty()
{
/*return _size == 0;*/
return _head == NULL;
}
size_t Size()
{
return _size;
}
T& Front()
{
assert(_head);
return _head->_data;
}
T& Back()
{
assert(_tail);
return _tail->_data;
}
private:
Node<T>* _head;
Node<T>* _tail;
size_t _size;
};
void Test()
{
Queue<int> q1;
q1.Push(0);
q1.Push(1);
q1.Push(2);
q1.Push(3);
q1.Push(4);
q1.Push(5);
q1.Pop();
q1.Pop();
q1.Pop();
size_t front = q1.Front();
size_t back = q1.Back();
size_t size = q1.Size();
cout << q1.Empty() << endl;
}
int main()
{
Test();
system("pause");
return 0;
}
队列(queue)在计算机科学中,是一种先进先出的线性表。它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。队列主要有push()(尾插),pop()(头删),size()(求队列大小),empty()(是否为空队列)几个基本的操作函数。
顺序表实现队列:
顺序表代码实现:
#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <assert.h> using namespace std; template<typename T> class Queue { public: Queue() :_a(NULL) , _size(0) , _capacity(0) {} ~Queue() { if (_a) { delete[] _a; } } void Push(const T& x) { _CheckCapacity(); _a[_size++] = x; } void Pop() { assert(_size != 0); size_t i = 0; for (i = 0; i < _size; i++) { _a[i] = _a[i + 1]; } --_size; } bool Empty() { return _size == 0; } size_t Size() { return _size; } T& Front() { return _a[0]; } T& Back() { return _a[_size-1]; } protected: void _CheckCapacity() { if (_capacity <= _size) { _capacity = 3; _a = new T[_capacity]; } else { _capacity *= 2; T* tmp = new T[_capacity]; for (size_t i = 0; i < _size; i++) { tmp[i] = _a[i]; } delete[] _a; _a = tmp; } } private: T* _a; size_t _size; size_t _capacity; }; void Test() { Queue<int> q1; q1.Push(0); q1.Push(1); q1.Push(2); q1.Push(3); q1.Push(4); q1.Push(5); q1.Pop(); q1.Pop(); q1.Pop(); size_t front = q1.Front(); size_t back = q1.Back(); size_t size = q1.Size(); cout << q1.Empty() << endl; } int main() { Test(); system("pause"); return 0; }
链表实现:
链表代码实现:
#include <iostream>
#include <assert.h>
using namespace std;
template<typename T>
struct Node
{
T _data;
Node<T>* _next;
Node(const T& x)
:_data(x)
, _next(NULL)
{}
};
template<typename T>
class Queue
{
public:
Queue()
:_head(NULL)
, _tail(NULL)
, _size(0)
{}
~Queue()
{}
void Push(const T& x)
{
if (_head == NULL)
{
_head = _tail = new Node<T>(x);
}
else
{
/*Node<T>* _tmp = new Node<T>*(x);
_tail->_next = _tmp;
_tail = _tmp;*/
_tail->_next = new Node<T>(x);
_tail = _tail->_next;
}
_size++;
}
void Pop()
{
assert(_head);
if (_head == _tail)
{
delete _head;
_head = _tail = NULL;
}
else
{
Node<T>* del = _head;
_head = _head->_next;
delete del;
}
--_size;
}
bool Empty()
{
/*return _size == 0;*/
return _head == NULL;
}
size_t Size()
{
return _size;
}
T& Front()
{
assert(_head);
return _head->_data;
}
T& Back()
{
assert(_tail);
return _tail->_data;
}
private:
Node<T>* _head;
Node<T>* _tail;
size_t _size;
};
void Test()
{
Queue<int> q1;
q1.Push(0);
q1.Push(1);
q1.Push(2);
q1.Push(3);
q1.Push(4);
q1.Push(5);
q1.Pop();
q1.Pop();
q1.Pop();
size_t front = q1.Front();
size_t back = q1.Back();
size_t size = q1.Size();
cout << q1.Empty() << endl;
}
int main()
{
Test();
system("pause");
return 0;
}
相关文章推荐
- 用两种方法实现栈---顺序表和链表
- 两种方法实现约瑟夫环(链表,顺序表)
- 递归求集合子集(两种方法实现(数组,链表))
- Linux平台上C语言实现异步队列的两种方法
- C#通过链表实现队列的方法
- 线性表 及Java实现 顺序表、链表、栈、队列
- 队列的两种实现:数组和链表
- 队列的两种实现方法
- C++_队列的两种实现方法
- 链表的两种实现方法
- 数据结构与算法 ---- 线性表 及Java实现 顺序表、链表、栈、队列
- C语言:链表的逆序,两种思考方法,第一种用rev_1()实现,第二种用rev_2( )实现.
- Java数组实现循环队列的两种方法
- 用两种方法来实现循环队列
- 经典数据结构之队列的链表实现方法
- 队列应用银行排队问题模拟:计算客户的平均停留时间和等待时间以及每个客户的时间信息,两种方法实现
- 链式顺序表-单链表的实现-------包括(单链表反转的两种方法)
- 链表反转的两种实现方法
- 链表反转的两种实现方法
- 学习笔记之约瑟夫环的两种实现方法(数组&链表)