C++用数组和链表分别实现Queue

简介:

C++用数组和链表分别实现Queue

昨天写了《C++用数组和链表分别实现Stack》,今天就是《C++用数组和链表分别实现Queue》,

队列就是先来的先被处理掉,后来的就等,直到成为先来的,实现起来感觉和栈差不多。

模板好用的,功能强大,有些东东还是写成模板的好,废话昨天都说了,今天是不想说的,

博客园的哥们说我的博客不符合推荐到首页的要求,只好加几句废话。

ExpandedBlockStart.gif
复制代码
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 ;

};
复制代码
ExpandedBlockStart.gif
复制代码

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 ;
* 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 ;
}
};



复制代码

作者:陈太汉


本文转自啊汉博客园博客,原文链接:http://www.cnblogs.com/hlxs/archive/2011/07/12/2104386.html


目录
相关文章
|
1月前
|
设计模式 算法 C++
【C++初阶】12. Stack(栈)和Queue(队列)
【C++初阶】12. Stack(栈)和Queue(队列)
43 3
|
22天前
|
存储 算法 C++
c++的学习之路:17、stack、queue与priority_queue
c++的学习之路:17、stack、queue与priority_queue
31 0
|
5天前
|
存储 算法 Java
数据结构与算法 数组和链表
数据结构与算法 数组和链表
11 0
|
5天前
|
设计模式 算法 编译器
【C++】开始使用stack 与 queue
队列的相关习题大部分是子啊BFS中使用,这里就不在说明了
11 3
|
8天前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
13 1
|
存储 算法 前端开发
[C++基础]-stack和queue
[C++基础]-stack和queue
|
16天前
|
存储 C++
【C++模板】模板实现通用的数组
【C++模板】模板实现通用的数组
|
21天前
|
存储 人工智能 C++
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
【重学C++】【指针】详解让人迷茫的指针数组和数组指针
34 1
|
28天前
|
C++ 容器
约瑟夫经典问题C++,STL容器queue解法
约瑟夫经典问题C++,STL容器queue解法
14 0
|
1月前
|
C++ 容器
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
19 1