//队列
using System;
namespace QueueExample
{
 public class Queue<T>
 {
  T[] data;
  int head;
  int rear;
  int count;
  
  public Queue(int length)
  {
   data = new T[length];
   head=rear=0;
   count = 0;
  }
  
  public void EnQueue(T item)
  {
   if(!IsFull())
   {
    data[rear]=item;
    rear= (rear+1)%data.Length;
    count++;
   }
   else
   {
    throw new ApplicationException("????!");
   }
  }
  
  public T DeQueue()
  {
   if(!IsEmpty())
   {
    T item = data[head];
    head++;
    count--;
    return item;
   }
   else
   {
    throw new ApplicationException("????!");   
   }
  }
  
  public bool IsFull()
  {
   return count==data.Length;
  }
  
  public bool IsEmpty()
  {
   return count==0;
  }
 }
}
//////////
using System;
using System.Collections.Generic;
namespace QueueExample
{
 class MainClass
 {
  public static void Main(string[] args)
  {
   Queue<string> q = new Queue<string>(4);
   q.EnQueue("11");
   q.EnQueue("22");
   q.EnQueue("33");
   q.EnQueue("44");
   if(q.IsFull())
   {
    Console.WriteLine("已满");
   }
   else
   {
    Console.WriteLine("未满");    
   }
   
   while(!q.IsEmpty())
   {
    Console.WriteLine(q.DeQueue());
   }
   Console.ReadKey();
  }
 }
}
//栈
using System;
using System.Collections.Generic;
namespace stacktest
{
 class MainClass
 {
  public static void Main(string[] args)
  {
   Stack<int> stk=new Stack<int>(5);
   //stk.Pop();
   try{
   stk.Push(1);
   stk.Push(2);
   stk.Push(21);
   stk.Push(22);
   Console.WriteLine(stk.Pop());
   Console.WriteLine(stk.Pop());
   Console.WriteLine(stk.Pop());
   Console.WriteLine(stk.Pop());
   }catch(Exception e)
   {
    Console.WriteLine(e.Message);
   }
   Console.ReadKey();
  }
 }
 
 public class Stack<T>{
 T[] data;
 int top;//战顶指针
 //int bottom;//战地指针
 int count;//元素个数
 //int i=0;//元素位置
 public Stack(int length){
  data =new T[length];
 }
 
 //取站顶元素
 public T Pop(){
  if(isEmpty()==true){
  
   throw new ApplicationException("栈为空!");
  }
  else{
   T topTemp=data[top-1];
   top--;
   return topTemp;
     
         // return data[top];
   
   }
  }
 //压站
 public void Push(T item){
  if(isFull()==true){
   throw new ApplicationException("栈已满!");
  }
  else{
  data[top]=item;
  count++;
  top++;
  }
 }
 //获取元素个数
 public int getCount(){
    return  data.Length;
 }
 
 //判断是否为空
 public bool isEmpty(){
 return count==0;
 }
 
 //判断是否满
 public bool isFull(){
 return data.Length==count;
 }
 }
}
posted on 2009-05-21 15:43  arong.NET  阅读(380)  评论(0编辑  收藏  举报