实现原理:
1、利用Stack的先进后出的特性,实现一个MaxStack,MaxStack中用一个Stack记录当前的值,一个Stack记录当前的最大值。
2、用2个MaxStack实现MaxQueue,其中一个MaxStack记录出栈记录,一个MaxStack记录入栈记录,取2个MaxStack中的最大值作为当前MaxQueue的最大值。
1 using System; 2 using System.Data; 3 using System.Collections; 4 using System.Collections.Generic; 5 6 namespace HelloWorld 7 { 8 public class MaxStack<T> 9 { 10 private Stack<T> data; 11 private Stack<T> maxStack; 12 13 public MaxStack() 14 { 15 data = new Stack<T>(); 16 maxStack = new Stack<T>(); 17 } 18 19 public void Push(T element) 20 { 21 if (data.Count == 0) 22 { 23 data.Push(element); 24 maxStack.Push(element); 25 } 26 else 27 { 28 var current = maxStack.Peek(); 29 var compare = Comparer<T>.Default; 30 if (compare.Compare(current, element) < 0) 31 { 32 maxStack.Push(element); 33 } 34 data.Push(element); 35 } 36 } 37 38 public T Pop() 39 { 40 if (this.IsEmpty) 41 throw new Exception("Queue is empty."); 42 else 43 { 44 var current = data.Pop(); 45 var compare = Comparer<T>.Default; 46 if (compare.Compare(current, maxStack.Peek()) == 0) 47 { 48 maxStack.Pop(); 49 } 50 return current; 51 } 52 } 53 54 public bool IsEmpty 55 { 56 get 57 { 58 return data.Count == 0; 59 } 60 } 61 62 public T GetMax() 63 { 64 if (maxStack.Count > 0) 65 return maxStack.Peek(); 66 else 67 return default(T); 68 } 69 } 70 }
1 using System; 2 using System.Data; 3 using System.Collections; 4 using System.Collections.Generic; 5 6 namespace HelloWorld 7 { 8 public class MaxQueue<T> 9 { 10 private MaxStack<T> popStack; 11 private MaxStack<T> pushStack; 12 public MaxQueue() 13 { 14 popStack = new MaxStack<T>(); 15 pushStack = new MaxStack<T>(); 16 } 17 18 public void Enqueue(T element) 19 { 20 pushStack.Push(element); 21 } 22 23 public T Dequeue() 24 { 25 if (popStack.IsEmpty && pushStack.IsEmpty) 26 throw new Exception("Queue is empty."); 27 if (popStack.IsEmpty) 28 { 29 while (!pushStack.IsEmpty) 30 { 31 var element = pushStack.Pop(); 32 popStack.Push(element); 33 } 34 } 35 return popStack.Pop(); 36 } 37 38 public T GetMax() 39 { 40 Comparer comparer = Comparer.Default; 41 if (comparer.Compare(popStack.GetMax(), pushStack.GetMax()) > 0) 42 { 43 return popStack.GetMax(); 44 } 45 else 46 { 47 return pushStack.GetMax(); 48 } 49 } 50 } 51 }
1 using System; 2 using System.Text; 3 4 namespace HelloWorld 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 MaxQueueTest(); 11 } 12 13 static void MaxQueueTest() 14 { 15 MaxQueue<int> queue = new MaxQueue<int>(); 16 SELECT: 17 var selected = ShowMenu(); 18 switch (selected) 19 { 20 case "1": 21 Console.Write("请输入入队值(int):"); 22 var value = Console.ReadLine(); 23 int v = 0; 24 if (int.TryParse(value, out v)) 25 { 26 queue.Enqueue(v); 27 Console.WriteLine("输入入队值:{0}后,,当前最大值为:{1}", value,queue.GetMax()); 28 } 29 goto SELECT; 30 case "2": 31 try 32 { 33 var element = queue.Dequeue(); 34 Console.WriteLine("出队值:{0},当前最大值为:{1}",element,queue.GetMax()); 35 } 36 catch(Exception ex) 37 { 38 Console.WriteLine(ex.Message); 39 } 40 goto SELECT; 41 case "3": 42 break; 43 default: 44 Console.WriteLine("输入有误,请重新选择。"); 45 goto SELECT; 46 } 47 } 48 49 static string ShowMenu() 50 { 51 MaxQueue<int> queue = new MaxQueue<int>(); 52 Console.WriteLine("*****************************************"); 53 Console.WriteLine("1、入队"); 54 Console.WriteLine("2、出队"); 55 Console.WriteLine("3、退出"); 56 Console.WriteLine("*****************************************"); 57 Console.Write("请选择:"); 58 var selected = Console.ReadLine(); 59 return selected; 60 } 61 } 62 }