Stack和Queue的C#实现

一. Stack:

1. 接口:

 

     interface  IStack < T >
    {
        
void  Push(T item);
        T Pop();
        
bool  IsEmpty();
        T GetTopItem();
    }

 

 

2. 顺序栈:

 

ExpandedBlockStart.gif 代码
     public   class  SequenceStack < T >  : IStack < T >
    {
        
private   const   int  MAX_COUNT  =   1000 ;
        
private  T[] data;
        
private   int  top;

        
private  SequenceStack()
        {
            data 
=   new  T[MAX_COUNT];
            top 
=   - 1 ;
        }

        
public   static  SequenceStack < T >  CreateStack()
        {
            
return   new  SequenceStack < T > ();
        }

        
public   bool  IsEmpty()
        {
            
return  top  <   0 ;
        }

        
public   bool  IsFull()
        {
            
return  top  ==  MAX_COUNT;
        }

        
public   void  Push(T item)
        {
            
if  (IsFull())
            {
                
throw   new  Exception( " Stack is full! " );
            }

            data[
++ top]  =  item;
        }

        
public  T Pop()
        {
            
if  (IsEmpty())
            {
                
throw   new  Exception( " Stack is empty. " );
            }

            
return  data[top -- ];
        }

        
public  T GetTopItem()
        {
            
if  (IsEmpty())
            {
                
throw   new  Exception( " Stack is empty. " );
            }

            
return  data[top];
        }
    }

 

 

3. 链表栈:

 

ExpandedBlockStart.gif 代码
     public   class  LinkStackNode < T >
    {
        
public  T Data {  set get ; }
        
public  LinkStackNode < T >  Next {  set get ; }
    }

    
public   class  LinkStack < T >  : IStack < T >
    {
        
private  LinkStackNode < T >  top;

        
private  LinkStack()
        {
            top 
=   null ;
        }

        
public   static  LinkStack < T >  CreateStack()
        {
            
return   new  LinkStack < T > ();
        }

        
public   bool  IsEmpty()
        {
            
return  top  ==   null ;
        }

        
public   void  Push(T item)
        {
            
if  (top  ==   null )
            {
                top 
=   new  LinkStackNode < T >
                {
                    Data 
=  item,
                    Next 
=   null
                };
            }
            
else
            {
                top 
=   new  LinkStackNode < T >
                {
                    Data 
=  item,
                    Next 
=  top
                };
            }
        }

        
public  T Pop()
        {
            
if  (IsEmpty())
            {
                
throw   new  Exception( " Stack is empty. " );
            }

            T data 
=  top.Data;
            top 
=  top.Next;

            
return  data;
        }

        
public  T GetTopItem()
        {
            
if  (IsEmpty())
            {
                
throw   new  Exception( " Stack is empty. " );
            }

            
return  top.Data;
        }
    }

 

 

4. 测试:

 

ExpandedBlockStart.gif 代码
             // IStack<int> stack = SequenceStack<int>.CreateStack();
            
// bool firstIsEmpty = stack.IsEmpty();
            
// stack.Push(1);
            
// bool secondIsEmpty = stack.IsEmpty();
            
// stack.Push(2);
            
// stack.Push(3);
            
// int popItem = stack.Pop();
            
// stack.Push(4);
            
// int topItem = stack.GetTopItem();

            IStack
< int >  stack  =  LinkStack < int > .CreateStack();
            
bool  firstIsEmpty  =  stack.IsEmpty();
            stack.Push(
1 );
            
bool  secondIsEmpty  =  stack.IsEmpty();
            stack.Push(
2 );
            stack.Push(
3 );
            
int  popItem  =  stack.Pop();
            stack.Push(
4 );
            
int  topItem  =  stack.GetTopItem();

 

 

 二. Queue:

1. 接口: 

     interface  IQueue < T >
    {
        
bool  IsEmpty();
        
void  InQueue(T item);
        T DeQueue();
        T GetTopItem();
    }

 

 

2. 顺序队列:

 

ExpandedBlockStart.gif 代码
     class  SequenceQueue < T >  : IQueue < T >
    {
        
private   const   int  MAX_COUNT  =   1000 ;
        
private   int  front, rear;
        
private  T[] data;

        
private  SequenceQueue()
        {
            data 
=   new  T[MAX_COUNT];
            front 
=  rear  =   - 1 ;
        }

        
public   static  SequenceQueue < T >  CreateQueue()
        {
            
return   new  SequenceQueue < T > ();
        }

        
public   bool  IsEmpty()
        {
            
return  front  ==  rear;
        }

        
public   bool  IsFull()
        {
            
return  rear  ==  MAX_COUNT;
        }

        
public   void  InQueue(T item)
        {
            
if  (IsFull())
            {
                
throw   new  Exception( " Queue is full. " );
            }

            data[
++ rear]  =  item;
        }

        
public  T DeQueue()
        {
            
if (IsEmpty())
            {
                
throw   new  OverflowException( " Queue is empty. " );
            }

            
return  data[ ++ front];
        }

        
public  T GetTopItem()
        {
            
if  (IsEmpty())
            {
                
throw   new  OverflowException( " Queue is empty. " );
            }

            
return  data[rear];
        }
    }

 

 

3. 链表队列:

 

ExpandedBlockStart.gif 代码
     public   class  LinkQueueNode < T >
    {
        
public  T Data {  set get ; }
        
public  LinkQueueNode < T >  Next {  set get ; }
    }

    
class  LinkQueue < T >  : IQueue < T >
    {
        
private  LinkQueueNode < T >  front, rear;

        
private  LinkQueue()
        {
            front 
=  rear  =   null ;
        }

        
public   static  LinkQueue < T >  CreateQueue()
        {
            
return   new  LinkQueue < T > ();
        }

        
public   bool  IsEmpty()
        {
            
return  front  ==   null ;
        }

        
public   void  InQueue(T item)
        {
            LinkQueueNode
< T >  node  =   new  LinkQueueNode < T >
            {
                Data 
=  item,
                Next 
=   null
            };

            
if  (front  ==   null )
            {
                front 
=  node;
            }
            
else
            {
                rear.Next 
=  node;
            }

            rear 
=  node;
        }

        
public  T DeQueue()
        {
            
if  (IsEmpty())
            {
                
throw   new  OverflowException( " Queue is empty. " );
            }

            var data 
=  front.Data;
            
if  (rear  ==  front)
            {
                rear 
=  front.Next;
            }
            front 
=  front.Next;

            
return  data;
        }

        
public  T GetTopItem()
        {
            
if  (IsEmpty())
            {
                
throw   new  OverflowException( " Queue is empty. " );
            }

            
return  rear.Data;
        }
    }

 

 

4. 测试:

 

ExpandedBlockStart.gif 代码
             // IQueue<int> queue = SequenceQueue<int>.CreateQueue();
            
// bool firstIsQueueEmpty = queue.IsEmpty();
            
// queue.InQueue(1);
            
// queue.InQueue(2);
            
// bool secondIsQueueEmpty = queue.IsEmpty();
            
// int deQueueItem = queue.DeQueue();
            
// queue.InQueue(3);
            
// int topQueueItem = queue.GetTopItem();

            IQueue
< int >  queue  =  LinkQueue < int > .CreateQueue();
            
bool  firstIsQueueEmpty  =  queue.IsEmpty();
            queue.InQueue(
1 );
            queue.InQueue(
2 );
            
bool  secondIsQueueEmpty  =  queue.IsEmpty();
            
int  deQueueItem  =  queue.DeQueue();
            queue.InQueue(
3 );
            
int  topQueueItem  =  queue.GetTopItem();

 

Download

 

转载于:https://www.cnblogs.com/Langzi127/archive/2010/07/12/1775794.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值