队列的链式存储结构C++实现
2015-03-24 15:59
423 查看
采用单链表实现队列,实质上为单链表的一种简化。只需要在链表的头部删除和尾部加入元素即可
/*************************************************************************
> File Name: LinkQueue.cpp
> Author: Shorey
> Mail: shoreybupt@gmail.com
> Created Time: 2015年03月24日 星期二 15时31分18秒
************************************************************************/
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkQueue
{
public:
LinkQueue(); //构造函数,初始化一个空队列
~LinkQueue(); //析构函数,释放队列中的结点
void EnQueue(int x); //把元素x入队
int DeQueue(); //把队头元素出队
int GetQueue() //获取队头元素,但不删除
{
if(front!=rear) //判断队列不为空
return front->next->data;
}
bool Empty() //判断队列是否为空
{
if(front==rear) return 1;
else return 0;
}
private:
Node *front,*rear;
};
LinkQueue::LinkQueue()
{
front=new Node;
front->next=NULL;
rear=front;
}
LinkQueue::~LinkQueue()
{
Node *p=front;
while(!front)
{
front=front->next;
delete p;
p=front;
}
}
void LinkQueue::EnQueue(int x)
{
Node *s=new Node;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
int LinkQueue::DeQueue()
{
if(!Empty())
{
Node *p=front->next;
int x;
front->next=p->next;
x=p->data;
delete p;
return x;
}
}
int main()
{
LinkQueue s;
s.EnQueue(4);
s.EnQueue(6);
s.EnQueue(7);
cout<<s.DeQueue()<<endl;
cout<<s.GetQueue()<<endl;
return 0;
}
/*************************************************************************
> File Name: LinkQueue.cpp
> Author: Shorey
> Mail: shoreybupt@gmail.com
> Created Time: 2015年03月24日 星期二 15时31分18秒
************************************************************************/
#include<iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkQueue
{
public:
LinkQueue(); //构造函数,初始化一个空队列
~LinkQueue(); //析构函数,释放队列中的结点
void EnQueue(int x); //把元素x入队
int DeQueue(); //把队头元素出队
int GetQueue() //获取队头元素,但不删除
{
if(front!=rear) //判断队列不为空
return front->next->data;
}
bool Empty() //判断队列是否为空
{
if(front==rear) return 1;
else return 0;
}
private:
Node *front,*rear;
};
LinkQueue::LinkQueue()
{
front=new Node;
front->next=NULL;
rear=front;
}
LinkQueue::~LinkQueue()
{
Node *p=front;
while(!front)
{
front=front->next;
delete p;
p=front;
}
}
void LinkQueue::EnQueue(int x)
{
Node *s=new Node;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
int LinkQueue::DeQueue()
{
if(!Empty())
{
Node *p=front->next;
int x;
front->next=p->next;
x=p->data;
delete p;
return x;
}
}
int main()
{
LinkQueue s;
s.EnQueue(4);
s.EnQueue(6);
s.EnQueue(7);
cout<<s.DeQueue()<<endl;
cout<<s.GetQueue()<<endl;
return 0;
}
相关文章推荐
- 单链队列,队列的链式存储结构C++实现
- 队列链式存储结构的C++模板类头文件源代码实现
- c++模板实现的队列
- C++ 学习练手 - 数组型队列的模板实现
- C++ 大根堆 实现优先队列
- 算法导论 第六章优先队列C++实现
- 算法导论 第六章优先队列C++实现
- 循环队列的实现(Queue, C++版)
- 简单循环队列(C++模版技术实现)
- 循环队列的定义、入队、出队等操作 C++代码实现
- c++实现的循环队列和栈
- 杨辉三角之c++队列实现
- 程序员面试题精选(48):两个堆栈模拟队列c++代码实现
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 链队列的C++ 实现以及循环队列
- 链队列的实现,C++代码实现
- 最小堆优先队列C++实现
- C++队列实现和队列简介(不是循环队列)
- 【C++】队列——顺序表实现V1.0(源码提供)
- 优先级队列——用C++模板实现