共有回帖数 0 个
-
面上理解,队列就是一个队列一样的数据结构 -_-!
那就想象一下排队买火车票吧,你到了售票窗口,你要从队尾排到这个队伍里,当轮到你买票时,你就是这个队列的队头了,当你买完票,你要从你队头的位置离开;这其实也就是队列这种数据结构的操作原则——先进先出,队尾插入,队头删除。
说起来队列和栈都是一样的,只不过操作方式不一样, 所以很好理解。
队列也有五种基本操作:队尾插入,队头删除,判断是否为空,访问队头元素还有构造函数。而这几种基本操作和栈的基本操作的作用都是一样的,首先判断队列是否为空是否为满,再进行插入、删除、访问队头元素操作,而构造函数的作用和栈的构造函数作用一样,也是建立队列并将队列初始化为空。
队列的实现一般用循环队列的方式实现。所谓循环队列,就是可以把队列看作一个环,首尾相接。


剩下的就是队列的实现了:
typedef int Queue_entry;//定义int的别名为Queue_entryconst int maxqueue=10;//定义队列的最大容量为10class Queue//定义队列的类
{
public:
Queue();
bool empty() const;
Error_code serve();
Error_code append(const Queue_entry &item);
Error_code retrieve(Queue_entry &item)const;
private:
int count;
int frount,rear;
Queue_entry entry[maxstack];
};Queue::Queue()//构造函数,用来创建队列并初始化为空
{
count=0;
rear=maxqueue-1;
frount=0;
}bool Queue::empty()const//判断队列是否为空
{
return count==0;
}Error_code Queue::append(const Queue_entry &item)//队尾插入
{
if(count=maxqueue)
return overflow;
count++;
rear=rear+1;
entry[rear]=item;
return success;
}Error_code Queue::serve()//队头删除
{
if(count=0)
return underflow;
count--;
frount=frount+1;
return success;
}Error_code Queue::retrieve(Queue_entry &item)const//访问队头元素
{
if(count=0)
return underflow;
item=entry[frount];
return success;
} 本人新菜一个,语言表达不清楚,图画的也不是很准确,请大神轻喷~
其实感觉Allen Weiss的表达方式不错 简单直接
struct QueueRecord{
int Rear;
int Front;
int Capacity;
int CurrentSize;
ElementType *Array;
};
楼主 2016-04-28 11:10 回复
Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号
意见反馈 |
关于直线 |
版权声明 |
会员须知