c++队列
来源:互联网 发布:乐视为什么倒闭 知乎 编辑:程序博客网 时间:2024/06/05 09:37
// MyQueue.cpp : 定义控制台应用程序的入口点。
//实现链式队列(queue),包括一个头结点。队列操作包括在队头出队(pop)、在队尾入队(push)、
//取得队头元素(front_element)、取得队尾元素(back_element)、队列元素个数(size)、
//队列是否为空(empty)。
#include "stdafx.h"
#include <iostream>
using namespace std;
//定义队列的节点结构
template <class T>
struct NODE
{
NODE<T>* next;
T data;
};
template <class T>
class MyQueue
{
public:
MyQueue()
{
NODE<T>* p = new NODE<T>;
if (NULL == p)
{
cout << "Failed to malloc the node." << endl;
}
p->data = NULL;
p->next = NULL;
front = p;
rear = p;
}
//在队尾入队
void push(T e)
{
NODE<T>* p = new NODE<T>;
if (NULL == p)
{
cout << "Failed to malloc the node." << endl;
}
p->data = e;
p->next = NULL;
rear->next = p;
rear = p;
}
//在队头出队
T pop()
{
T e;
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
NODE<T>* p = front->next;
front->next = p->next;
e = p->data;
//注意判断当只有一个元素,且删除它之后,rear指向的node被删除
//应将其指向头结点
if (rear == p)
{
rear = front;
}
delete p; p = NULL;
return e;
}
}
//取得队头元素
T front_element()
{
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
NODE<T>* p = front->next;
return p->data;
}
}
T back_element()
{
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
return rear->data;
}
}
//取得队列元素个数
int size()
{
int count(0);
NODE<T>* p = front;
while (p != rear)
{
p = p->next;
count++;
}
return count;
}
//判断队列是否为空
bool empty()
{
if (front == rear)
{
return true;
}
else
{
return false;
}
}
private:
NODE<T>* front; //指向头结点的指针。 front->next->data是队头第一个元素。
NODE<T>* rear;//指向队尾(最后添加的一个元素)的指针
};
int _tmain(int argc, _TCHAR* argv[])
{
MyQueue<int> myqueue;
cout << myqueue.size() << endl;
myqueue.push(10);
myqueue.push(20);
myqueue.push(30);
cout << myqueue.front_element() << endl;
cout << myqueue.back_element() << endl;
myqueue.pop();
if (myqueue.empty())
{
cout << "The queue is empty now." << endl;
}
else
{
cout << "The queue has " << myqueue.size() << " elements now." << endl;
}
myqueue.pop();
myqueue.pop();
if (myqueue.empty())
{
cout << "The queue is empty now." << endl;
}
else
{
cout << "The queue has " << myqueue.size() << " elements now." << endl;
}
return 0;
}
//实现链式队列(queue),包括一个头结点。队列操作包括在队头出队(pop)、在队尾入队(push)、
//取得队头元素(front_element)、取得队尾元素(back_element)、队列元素个数(size)、
//队列是否为空(empty)。
#include "stdafx.h"
#include <iostream>
using namespace std;
//定义队列的节点结构
template <class T>
struct NODE
{
NODE<T>* next;
T data;
};
template <class T>
class MyQueue
{
public:
MyQueue()
{
NODE<T>* p = new NODE<T>;
if (NULL == p)
{
cout << "Failed to malloc the node." << endl;
}
p->data = NULL;
p->next = NULL;
front = p;
rear = p;
}
//在队尾入队
void push(T e)
{
NODE<T>* p = new NODE<T>;
if (NULL == p)
{
cout << "Failed to malloc the node." << endl;
}
p->data = e;
p->next = NULL;
rear->next = p;
rear = p;
}
//在队头出队
T pop()
{
T e;
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
NODE<T>* p = front->next;
front->next = p->next;
e = p->data;
//注意判断当只有一个元素,且删除它之后,rear指向的node被删除
//应将其指向头结点
if (rear == p)
{
rear = front;
}
delete p; p = NULL;
return e;
}
}
//取得队头元素
T front_element()
{
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
NODE<T>* p = front->next;
return p->data;
}
}
T back_element()
{
if (front == rear)
{
cout << "The queue is empty." << endl;
return NULL;
}
else
{
return rear->data;
}
}
//取得队列元素个数
int size()
{
int count(0);
NODE<T>* p = front;
while (p != rear)
{
p = p->next;
count++;
}
return count;
}
//判断队列是否为空
bool empty()
{
if (front == rear)
{
return true;
}
else
{
return false;
}
}
private:
NODE<T>* front; //指向头结点的指针。 front->next->data是队头第一个元素。
NODE<T>* rear;//指向队尾(最后添加的一个元素)的指针
};
int _tmain(int argc, _TCHAR* argv[])
{
MyQueue<int> myqueue;
cout << myqueue.size() << endl;
myqueue.push(10);
myqueue.push(20);
myqueue.push(30);
cout << myqueue.front_element() << endl;
cout << myqueue.back_element() << endl;
myqueue.pop();
if (myqueue.empty())
{
cout << "The queue is empty now." << endl;
}
else
{
cout << "The queue has " << myqueue.size() << " elements now." << endl;
}
myqueue.pop();
myqueue.pop();
if (myqueue.empty())
{
cout << "The queue is empty now." << endl;
}
else
{
cout << "The queue has " << myqueue.size() << " elements now." << endl;
}
return 0;
}
0 0
- C 队列
- 队列(C#)
- C 队列
- C+队列
- C 队列
- C#:消息队列应用程序
- C#:消息队列应用程序
- queue-c链式队列
- c循环队列
- C动态队列
- C#:消息队列应用程序
- 链式队列(数据结构C#)
- c 队列线性存储
- c 环状队列
- 数据结构---队列(C#)
- C 宏实现队列
- C的循环队列
- C语言:队列
- java实现的MySQL自动备份和还原(struts2+Hibernate)---兼容 window+Linux
- redmine访问速度慢的处理方法
- java--根据URL获取信息
- mina学习基础-入门实例-传输以回车做为结尾的字符串(一)
- ffmpeg编译生成ffplay
- c++队列
- PHP面向对象基础
- java集合类总结
- Tornado编译vxworks.bin镜像
- 【垃圾菜鸟笔记】ajax +用户注册小小实例
- Linux中cp和scp命令的使用
- Discuz!X3 修改在线人数在线会员以及统计数据的方法
- Unity3D研究院之与Android相互传递消息
- python爬虫 根据关键字在新浪网站查询跟关键字有关的新闻条数(按照时间查询)