数据结构学习笔记五

队列

队列(Queue)是插入操作限定在表的尾部而其它操作限定在表的头部进行的线性表。把进行插入操作的表尾称为队尾(Rear),把进行其它操作的头部称为队头(Front)。当对列中没有数据元素时称为空对列(Empty Queue)。

 

1.顺序队列

用一片连续的存储空间来存储队列中的数据元素,这样的队列称为顺序队列(Sequence Queue)。类似于顺序栈,用一维数组来存放顺序队列中的数据元素。队头位置设在数组下标为0的端,用front表示;队尾位置设在数组的另一端,用rear表示。front和rear随着插入和删除而变化。当队列为空时,front=rear=-1。

因为遵循的是队尾入,队首出,所以随着操作的进行,就会出现队列前面有空而后面的空间占满的情况,我们称之为假溢出。解决假溢出的方法是将顺序队列看成是首尾相接的循环结构,头尾指示器的关系不变,这种队列叫循环顺序队列(Circular sequence Queue)。所以后面我们主要讨论的都是循环顺序队列。

队尾指示器的加1操作修改为:rear = (rear + 1) % maxsize

队头指示器的加1操作修改为:front = (front + 1) % maxsize

判断队空的条件是:rear==front,判断队满的条件是:(rear + 1) % maxsize==front。

代码
public   class  CSeqQueue < T >
    {
        
private   int  maxSize;
        
private  T[] data;
        
private   int  front;
        
private   int  rear;

        
public  T  this [ int  index]
        {
            
get
            {
                
return  data[index];
            }
            
set
            {
                data[index] 
=  value;
            }
        }
        
public   int  MaxSize
        {
            
get
            {
                
return  maxSize;
            }
            
set
            {
                maxSize 
=  value;
            }


        }
        
public   int  Front
        {
            
get
            {
                
return  front;
            }
            
set
            {

                front 
=  value;
            }


        }
        
public   int  Rear
        {
            
get
            {
                
return  rear;
            }
            
set
            {
                rear 
=  value;
            }

        }

        
public  CSeqQueue( int  size)
        {
            data 
=   new  T[size];
            maxSize 
=  size;
            front 
=  rear  =   - 1 ;
        }

        
public   int  GetLength()
        {

            
return  (rear  -  front  +  maxSize)  %  maxSize;
        }

        
public   void  Clear()
        {
            front 
=  rear  =   - 1 ;
        }

        
public   bool  IsEmpty()
        {
            
if  (front  ==  rear)
            {
                
return   true ;
            }
            
else
            {
                
return   false ;
            }
        }

        
public   bool  IsFull()
        {
            
if  ((rear  +   1 %  maxSize  ==  front)
            {
                
return   true ;
            }
            
else
            {
                
return   false ;
            }
        }

        
public   void  In(T item)
        {
            
if  (IsFull())
            {
                
return ;
            }
            
else
            {
                data[
++ rear]  =  item;
            }
        }

        
public  T Out(T item)
        {
            T temp 
=   default (T);
            
if  (IsEmpty())
            {
                
return  temp;
            }
            
else
            {
                temp
= data[ ++ front];
                
return  temp;
            }
        }
        
public  T GetFront()
        {
            
if  (IsEmpty())
            {
                
return   default (T);
            }
            
else
            {
                
return  data[front  +   1 ];
            }
        }

    }

 2.链队列

 

代码
  public   class  Node < T >
    {
        
private  T data;
        
private  Node < T >  next;

        
public  Node(T val, Node < T >  p)
        {
            data 
=  val;
            next 
=  p;
        }

        
public  Node(Node < T >  p)
        {
            next 
=  p;
        }

        
public  Node(T val)
        {
            data 
=  val;
            next 
=   null ;
        }

        
public  Node()
        {
            data 
=   default (T);
            next 
=   null ;
        }

        
public  T Data
        {
            
get
            {
                
return  data;
            }
            
set
            {
                data 
=  value;
            }
        }

        
public  Node < T >  Next
        {
            
get
            {
                
return  next;
            }
            
set
            {
                next 
=  value;
            }

        }



    }
    
public   class  LinkQueue < T >
    {
        
private  Node < T >  front;

        
public  Node < T >  Front
        {
            
get  {  return  front; }
            
set  { front  =  value; }
        }
        
private  Node < T >  rear;

        
public  Node < T >  Rear
        {
            
get  {  return  rear; }
            
set  { rear  =  value; }
        }
        
private   int  num;

        
public   int  Num
        {
            
get  {  return  num; }
            
set  { num  =  value; }
        }

        
public  LinkQueue()
        {
            front 
=  rear  =   null ;
            num 
=   0 ;
        }
        
public   int  GetLength()
        {
            
return  num;
        }

        
public   void  Clear()
        {
            front 
=  rear  =   null ;
            num 
=   0 ;
        }

        
// 判断链队列是否为空
         public   bool  IsEmpty()
        {
            
if  ((front  ==  rear)  &&  (num  ==   0 ))
            {
                
return   true ;
            }
            
else
            {
                
return   false ;
            }
        }

        
public   void  Insert(T item)
        {
            Node
< T >  p  =   new  Node < T > (item);
            
if  (rear  ==   null )
            {
                rear 
=  p;
            }
            
else
            {
                rear.Next 
=  p;
                rear 
=  p;
            }
            
++ num;
        }

        
// 出队
         public  T Out()
        {
            
if  (IsEmpty())
            {
                
return   default (T);
            }
            Node
< T >  p  =  front;
            front 
=  front.Next;
            
if  (front  ==   null )
            {
                rear 
=   null ;
            }
            
-- num;
            
return  p.Data;
        }

        
// 获取链队列头结点的值
         public  T GetFront()
        {
            
if  (IsEmpty())
            {
                
return   default (T);
            }
            
return  front.Data;
        }



    }

 

 

 

 

你可能感兴趣的:(数据结构)