一. Stack:
1. 接口:
interface
IStack
<
T
>
{
void Push(T item);
T Pop();
bool IsEmpty();
T GetTopItem();
}
{
void Push(T item);
T Pop();
bool IsEmpty();
T GetTopItem();
}
2. 顺序栈:
代码
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];
}
}
{
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. 链表栈:
代码
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;
}
}
{
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. 测试:
代码
//
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();
// 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();
}
{
bool IsEmpty();
void InQueue(T item);
T DeQueue();
T GetTopItem();
}
2. 顺序队列:
代码
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];
}
}
{
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. 链表队列:
代码
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;
}
}
{
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. 测试:
代码
//
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();
// 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();