自己实现 STL Queue(用数组和链表)
2013-08-20 08:31
267 查看
队列就是先来的先被处理掉,后来的就等,直到成为先来的,实现起来感觉和栈差不多。
模板好用的,功能强大,有些东东还是写成模板的好,
链表版
template<typename T,typename container>
class queue
{
public:
bool empty() const
{
return len==0;
}
void checkEmpty()
{
if(empty())
{
throw new exception("队列中没有数据");
}
}
T& back()
{
checkEmpty();
return cur->val;
}
const T& back() const
{
return back();
}
void pop()
{
checkEmpty();
if(head->next==cur)
{
delete head->next;
head->next=NULL;
}else
{
node* tmp=head->next;
head->next=tmp->next;
delete tmp;
}
--len;
}
T& front()
{
checkEmpty();
return head->next->val;
}
const T& front() const
{
return front();
}
void push(const T& val)
{
node *tmp=new node(val);
cur->next=tmp;
cur=tmp;
++len;
}
queue()
{
initialize();
}
explicit queue(const container& cont)
{
initialize();
vector <int>::const_iterator iter=cont.begin();
while(iter!=cont.end())
{
push(*iter++);
}
}
~queue()
{
node *tmp;
while(tmp!=NULL)
{
tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
}
delete cur;
}
int size()
{
return len;
}
protected:
typedef struct node1
{
node1 *next;
T val;
node1(T v):val(v),next(NULL){}
}node;
private :
int len;
node *head;
node *cur;
void initialize()
{
head=new node(-1);
cur=head;
len=0;
}
};
模板好用的,功能强大,有些东东还是写成模板的好,
链表版
template<typename T,typename container>
class queue
{
public:
bool empty() const
{
return len==0;
}
void checkEmpty()
{
if(empty())
{
throw new exception("队列中没有数据");
}
}
T& back()
{
checkEmpty();
return cur->val;
}
const T& back() const
{
return back();
}
void pop()
{
checkEmpty();
if(head->next==cur)
{
delete head->next;
head->next=NULL;
}else
{
node* tmp=head->next;
head->next=tmp->next;
delete tmp;
}
--len;
}
T& front()
{
checkEmpty();
return head->next->val;
}
const T& front() const
{
return front();
}
void push(const T& val)
{
node *tmp=new node(val);
cur->next=tmp;
cur=tmp;
++len;
}
queue()
{
initialize();
}
explicit queue(const container& cont)
{
initialize();
vector <int>::const_iterator iter=cont.begin();
while(iter!=cont.end())
{
push(*iter++);
}
}
~queue()
{
node *tmp;
while(tmp!=NULL)
{
tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
}
delete cur;
}
int size()
{
return len;
}
protected:
typedef struct node1
{
node1 *next;
T val;
node1(T v):val(v),next(NULL){}
}node;
private :
int len;
node *head;
node *cur;
void initialize()
{
head=new node(-1);
cur=head;
len=0;
}
};
数组版 template<typename T,typename container> class queue { public: bool empty() const { return head==rail; } void checkEmpty() { if(empty()) { throw new exception("队列中没有数据"); } } //队尾元素 T& back() { checkEmpty(); return arr[rail-1]; } const T& back() const { return back(); } //出队 void pop() { checkEmpty(); arr[head++]=0; } //队头元素 T& front() { checkEmpty(); return arr[head]; } const T& front() const { return front(); } //入队 void push(const T& val) { if(rail>=capacity){ capacity=(rail-head)*2; T *tmp=new T[capacity]; int j=0; for(int i=head;i<rail;i++) { tmp[j++]=arr[i]; } delete arr; arr=tmp; rail=rail-head; head=0; } arr[rail++]=val; } queue() { initialize(4); } queue(int capacity) { initialize(capacity); } explicit queue(const container& cont) { initialize(cont.size()); vector <int>::const_iterator iter=cont.begin(); while(iter!=cont.end()) { push(*iter++); } } ~queue() { delete arr; } //队列中元素个数 int size() { return rail-head; } protected: typedef struct node1 { node1 *next; T val; node1(T v):val(v),next(NULL){} }node; private : int capacity; int head;//对头元素的位置 int rail;//对尾元素的位置 int *arr; void initialize(int cap) { capacity=cap; arr=new int[capacity]; head=rail=0; } };
相关文章推荐
- 自己实现 STL Stack(用数组和链表)
- 107-109_容器_自己实现HashMap_Map底层实现_哈希算法实现_使用数组和链表
- 根据数组+链表的原理,自己实现一个简易版的HashMap
- 栈的链表和数组实现
- //4.创建一个数组, //实现函数init()初始化数组、 //实现empty()清空数组、 //实现reverse()函数完成数组元素的逆置。 //要求:自己设计函数的参数,返回值。
- C++类模板 实现两栈共享数组空间的算法 《数据结构》(北京科海) 自己摘抄完成
- 根据链表的原理,自己实现一个简易版的LinkedList
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- java 实现数组队列和链表队列
- 基于数组和链表两种方式实现栈
- 约瑟夫环-链表和数组表示实现及分析(Josephus Problem)
- 链表实践—多项式ADT(数组实现)
- 两路归并的数组与链表的实现方法
- 基于数组和链表的队列实现
- 学习:erlang用链表实现大容量的List或者数组。
- 两个一元多项式相乘,数组与链表实现
- 基于数组或链表的堆栈实现
- 排序算法集:冒泡、插入、希尔、快速(数组实现、链表实现)
- 数据结构 23 哈希 哈希链表 自己实现
- 约瑟夫环问题的两种实现[链表+数组]