栈和队列(补)

栈是限定仅在表的一端进行插入和删除操作的线性表,允许插入和删除的一端称为栈顶,​另一端称为栈底,不含任何数据元素的栈称为空栈。
​栈中元素除了具有线性关系外,还具有后进先出的特性。
.栈的顺序储存结构称为顺序栈。顺序栈本质上是顺序表的简化,唯一需要确定的是用数组的哪一端表示栈底。

顺序栈
​const  int  MAX_SIZE=100;

template  <class T>
class  seqStack
{  
public:        
seqStack ( ) ;        
~seqStack ( );       
void  Push ( T  x );        
T   Pop ( );        
T   GetTop ( );        
bool  Empty ( );    
private:        
T  data[MAX_SIZE];      
int  top;
}

​入栈
​template <class T>

void  seqStack<T>::Push ( T  x)
{     
if (top==MAX_SIZE-1)  throw  “溢出”;     
top++;    
data[top]=x;
 }
判断是否是空栈
​template <class T>
bool  seqStack<T>::Empty ()
{     
if (top==-1)          
return true;    
return false;
 }

​取栈顶
​template <class T>
T  seqStack<T>::GetTop ( )
{     
if (Empty()) throw  ”空栈” ;     
return data[top];
 }

​出栈
​template  <class  T>
T  seqStack<T>:: Pop ( )
{     
if (top==-1)  throw  “溢出”;      
x=data[top];      
top--;    
 return  x;
}

循环队列
​const int QueueSize=100; 
template <class T>    
class CirQueue{ 
public:      
CirQueue( );       
~ CirQueue( );     
void EnQueue(T x);      
T DeQueue( );                  
T GetQueue( );      
bool Empty( ){
      if (rear==front) return true;
       return false;
   };  
private:     
T data[QueueSize];        
int front, rear;
};
​入队
​emplate <class T>
 void CirQueue<T>::EnQueue(T x)
 {
   
if ((rear+1) % QueueSize ==front) throw "上溢";
   rear=(rear+1) % QueueSize;       
data[rear]=x;}
出队
​template <class T>
T CirQueue<T>::DeQueue( )
{
     if (rear==front) throw "下溢"; 
     front=(front+1) % QueueSize;      
return data[front];
}
​读队头元素
​template <class T>
T CirQueue<T>::GetQueue( )
{
    if (rear==front) throw "下溢";     
i=(front+1) % QueueSize;      
return data[i];
}
​4.链队列
​template <class T>
class LinkQueue
{    
public:      
LinkQueue( );     
      ~LinkQueue( );       
void EnQueue(T x);      
T DeQueue( );             
T GetQueue( );      
bool Empty( );  
private:    
Node<T> *front, *rear;
};
​构造函数
​template <class T>
LinkQueue<T>::LinkQueue( )
{     
front=new Node<T>;      
front->next=NULL;  
   rear=front;
}
​入队
​template <class T>
void LinkQueue<T>::EnQueue(T x)
{     
s=new Node<T>;     
s->data=x;     
s->next=NULL;    
rear->next=s;    
rear=s;
}
​出队
​template <class T>
T LinkQueue<T>::DeQueue( )
{     
if (rear==front) throw "下溢";     
p=front->next;     
x=p->data;     
front->next=p->next;            
delete p;     
if (front->next==NULL) rear=front;   
     return x;
}
​5.
循环队列:必须预先确定一个固定的长度,所以有存储元素个数的限制和空间浪费的问题。
链队列:没有队列满的问题,只有当内存没有可用空间时才会出现队列满,但是每个元素都需要一个指针域,从而产生了结构性开销。

2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值