2个堆栈实现自定义队列的入队出队方法 - 调用者定义2个栈的容量

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace MyQueue  
  7. {  
  8.     public class MyQueue2<T>  
  9.     {  
  10.         private int _bigStackFlag = 0;  
  11.         private int _bigCapacity;  
  12.         private int _smallCapacity;  
  13.         private bool _isSameCapacity;  
  14.         private Stack<T> _big;  
  15.         private Stack<T> _small;  
  16.         public int Count { getprivate set; }  
  17.         public bool IsEmpty { get { return this.Count == 0; } }  
  18.         public bool IsFull  
  19.         {  
  20.             get  
  21.             {  
  22.                 if (this._isSameCapacity)  
  23.                 {  
  24.                     return this.Count >= this._smallCapacity + 1;  
  25.                 }  
  26.                 else  
  27.                 {  
  28.                     return this.Count >= this._smallCapacity + 2;  
  29.                 }  
  30.             }  
  31.         }  
  32.   
  33.         public MyQueue2(int capacity1, int capacity2)  
  34.         {  
  35.             if (capacity1 >= capacity2)  
  36.             {  
  37.                 this._bigCapacity = capacity1;  
  38.                 this._smallCapacity = capacity2;  
  39.             }  
  40.             else  
  41.             {  
  42.                 this._bigCapacity = capacity2;  
  43.                 this._smallCapacity = capacity1;  
  44.             }  
  45.             this._big = new Stack<T>(this._bigCapacity);  
  46.             this._small = new Stack<T>(this._smallCapacity);  
  47.             this._isSameCapacity = (this._bigCapacity == this._smallCapacity);  
  48.             this.Count = 0;  
  49.         }  
  50.   
  51.         public void Enqueue(T item)  
  52.         {  
  53.             if (this.IsFull)  
  54.             {  
  55.                 throw new System.InvalidOperationException("Queue full");  
  56.             }  
  57.   
  58.             if (this._small.Count < this._smallCapacity)  
  59.             {  
  60.                 this._small.Push(item);  
  61.             }  
  62.             else  
  63.             {  
  64.                 this._big.Push(item);  
  65.                 this._bigStackFlag++;  
  66.             }  
  67.             this.Count++;  
  68.             Console.WriteLine("Enqueue " + item.ToString());  
  69.         }  
  70.   
  71.         public T Dequeue()  
  72.         {  
  73.             if (this.IsEmpty)  
  74.             {  
  75.                 throw new System.InvalidOperationException("Queue empty");  
  76.             }  
  77.   
  78.             while (this._small.Count != 1)  
  79.             {  
  80.                 this._big.Push(this._small.Pop());  
  81.             }  
  82.             T result = this._small.Pop();  
  83.   
  84.             while (this._big.Count > this._bigStackFlag)  
  85.             {  
  86.                 this._small.Push(this._big.Pop());  
  87.             }  
  88.   
  89.             if (this._bigStackFlag != 0)  
  90.             {  
  91.                 if (this._bigStackFlag == 1)  
  92.                 {  
  93.                     this._small.Push(this._big.Pop());  
  94.                 }  
  95.                 else if (this._bigStackFlag == 2)  
  96.                 {  
  97.                     T tmp = this._big.Pop();  
  98.                     this._small.Push(this._big.Pop());  
  99.                     this._big.Push(tmp);  
  100.                 }  
  101.                 else  
  102.                 {  
  103.                     throw new Exception("inner expection: this._bigStackFlag is larger than 2!");  
  104.                 }  
  105.                 this._bigStackFlag--;  
  106.             }  
  107.   
  108.             this.Count--;  
  109.             Console.WriteLine("Dequeue " + result.ToString());  
  110.             return result;  
  111.         }  
  112.     }  
  113.   
  114.     class Test  
  115.     {  
  116.         static void Main(string[] args)  
  117.         {  
  118.             MyQueue2<int> q = new MyQueue2<int>(2, 6);  
  119.             //MyQueue2<int> q = new MyQueue2<int>(3, 3);  
  120.             for (int i = 1; i < 5; i++)  
  121.             {  
  122.                 q.Enqueue(i);  
  123.             }  
  124.   
  125.             q.Dequeue();  
  126.             q.Enqueue(5);  
  127.   
  128.             Console.WriteLine("before clear");  
  129.             while (!q.IsEmpty)  
  130.             {  
  131.                 q.Dequeue();  
  132.             }  
  133.             //q.Dequeue();  
  134.             Console.WriteLine("clear");  
  135.   
  136.             for (int i = 6; i < 11; i++)  
  137.             {  
  138.                 q.Enqueue(i);  
  139.             }  
  140.             Console.WriteLine("done");  
  141.         }  
  142.     }  
  143. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值