数据结构笔记6-栈和队列

栈的逻辑结构 

栈:限定仅在表尾进行插入和删除操作的线性表。  空栈:不含任何数据元素的栈。允许插入和删除的一端称为栈顶,另一端称为栈底。(a1, a2, ……, an)a1为栈底,an为栈顶。             栈的操作特性:后进先出                                                                           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;
}

链栈:栈的链接存储结构

template <class T>
class LinkStack
{    
   public:
         LinkStack( ) {top=NULL;};
         ~LinkStack( );            
         void Push(T x); 
         T Pop( ); 
         T GetTop( );
         bool Empty( );
   private:
         Node<T> *top; 
}
template <class T>
void LinkStack<T>::Push(T x)
{
    s=new Node<T>;
    s->data=x; 
    s->next=top; 
   top=s; 
}
template <class T>
T LinkStack<T>::Pop( )
{
    if (top==NULL)
     throw "下溢";
    x=top->data; 
    p=top; 
    top=top->next;   
    delete p;
    return x;
}
template <class T>
LinkStack<T>::~LinkStack( )
{
    while (top)
    {
        Node<T> *p;
        p=top->next;
                delete top;
        top=p;
    }
}

顺序栈和链栈的比较              时间性能:相同,都是常数时间O(1) 。               空间性能: 顺序栈:有元素个数的限制和空间浪费的问题。 链栈:没有栈满的问题,只有当内存没有可用空间时才会出现栈满,但是每个元素都需要一个指针域,从而产生了结构性开销。                       结论:当栈的使用过程中元素个数变化较大时,用链栈是适宜的,反之,应该采用顺序栈。

  队列:只允许在一端进行插入操作,而另一端进行删除操作的线性表。       空队列:不含任何数据元素的队列。 允许插入(也称入队、进队)的一端称为队尾,允许删除(也称出队)的一端称为队头。(a1, a2, ……, an),a1为队头,an为队尾                队列的操作特性:先进先出

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;
};
template <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];
}
template <class T>
int CirQueue<T>::GetLength( )
{
    if (rear==front) throw "下溢";
    len=(rear-front+ QueueSize) % QueueSize;
    return len;
}

链队列:队列的链接存储结构

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十只兔子OVO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值