deque源代码循环数组实现

deque
The word deque (pronounced either "deck" or "DQ") is a shortend form of double-ended queue
and denoted a list in which entries can be added or removed from either the first or the last
position of the list, but no changes can be made elsewhere in the list. Thus a deque is a
generalization of both a stack and a queue. The fundmantal operations on a deque are
appedn_front, append_rear, serve_front, serve_rear, retrieve_front, and retrieve_rear.


// implemented in circular array

#define  maxque 15    //   small number for test
enum  Error_code  {success, fail, underflow, overflow} ;
typedef  
int  deque_entry;


class  Deque  {
public:
    Deque();    
// constructor
    Error_code append_front (const deque_entry &item);
    Error_code append_rear (
const deque_entry &item);
    Error_code serve_front ();
    Error_code serve_rear ();
    Error_code retrieve_front ( deque_entry 
&item);
    Error_code retrieve_rear ( deque_entry 
&item);
    
int size() const;
    
bool is_empty() const;
protected:
    deque_entry entry[maxque];
    
int front, rear;
    
int count;
}
;


Deque::Deque()
/*  Post: The Deque is initialized to be empty.*/
{
    count
=0;
      front
=0
    rear 
= maxque-1;
}



int  Deque::size()  const
{
    
return count;
}


bool  Deque::is_empty()  const
{
    
if(count==0)
        
return true;
    
else return false;
}



Error_code Deque::append_front (
const  deque_entry  & item)
{
    
if (count==maxque) return overflow;
    front 
= (front==0? maxque-1:front-1;  
   entry[front]
=item;
   count
++;
   
return success;
}


Error_code Deque::serve_front()
/* Post: The front of the Deque is removed. If the Deque 
is empty return an Error_code of underflow. 
*/

{
   
if (is_empty()) return underflow;
    front 
= (front==maxque-1? 0: front+1;
   count
--;  
   
return success;
}


Error_code Deque::append_rear (
const  deque_entry  & item)
{
    
if (count==maxque) return overflow;
    rear 
= (rear==maxque-1? 0 :rear+1;
   count
++;  
   
return success;

}


Error_code Deque::serve_rear()
/*   Post: The rear of the Deque is removed. If the Deque 
is empty return an Error_code of underflow.  
*/

{
   
if (is_empty()) return underflow;
    rear 
= (rear==0? maxque-1:rear-1;
   count
--;  
   
return success;
}


Error_code Deque::retrieve_rear ( deque_entry 
& item)
{
    
if (is_empty()) return underflow;
    item
=entry[rear];
    rear
=(rear==0? maxque-1: rear-1;
    count
--;
    
return success;
}


Error_code Deque::retrieve_front ( deque_entry 
& item)
{
    
if (is_empty()) return underflow;
    item
=entry[front];
    rear
=(front==maxque) ? 0: front+1;
    count
--;
    
return success;
}

 


width="728" scrolling="no" height="90" frameborder="0" align="middle" src="http://kofreestyler.googlepages.com/csdnGGad.htm" marginheight="0" marginwidth="0">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值