/// <summary> /// 将两个 Steque 连接。 /// </summary> /// <param name="s1">第一个 Steque </param> /// <param name="s2">第二个 Steque (将被删除)</param> /// <returns></returns> public static Steque <Item> Catenation(Steque <Item> s1, Steque <Item> s2) { if (s1.IsEmpty()) { s1.first = s2.first; s1.last = s2.last; s1.count = s2.count; } else { s1.last.next = s2.first; s1.count += s2.count; } s2 = null; return(s1); }
static void Main(string[] args) { var q1 = new Queue <string>(); q1.Enqueue("first"); q1.Enqueue("second"); var q2 = new Queue <string>(); q2.Enqueue("third"); q2.Enqueue("fourth"); q1 = Queue <string> .Catenation(q1, q2); Console.WriteLine(q1); var s1 = new Stack <string>(); s1.Push("first"); s1.Push("second"); var s2 = new Stack <string>(); s2.Push("third"); s2.Push("fourth"); s2 = Stack <string> .Catenation(s2, s1); Console.WriteLine(s2); var sq1 = new Steque <string>(); sq1.Push("first"); sq1.Enqueue("second"); var sq2 = new Steque <string>(); sq2.Push("third"); sq2.Enqueue("fourth"); sq1 = Steque <string> .Catenation(sq1, sq2); Console.WriteLine(sq1); }