/// <summary> /// 创建栈的浅表副本。 /// </summary> /// <returns></returns> public LinkedStack <Item> Copy() { var temp = new LinkedStack <Item>(); temp.first = first; temp.count = count; return(temp); }
/// <summary> /// 创建栈的浅表副本。 /// </summary> /// <returns></returns> public LinkedStack <Item> Copy() { LinkedStack <Item> temp = new LinkedStack <Item>(); temp.first = this.first; temp.count = this.count; return(temp); }
/// <summary> /// 复制构造函数。 /// </summary> /// <param name="s"></param> public LinkedStack(LinkedStack <Item> s) { if (s.first != null) { first = new Node <Item>(s.first); for (var x = first; x.next != null; x = x.next) { x.next = new Node <Item>(x.next); } } count = s.count; }
/// <summary> /// 复制构造函数。 /// </summary> /// <param name="s"></param> public LinkedStack(LinkedStack <Item> s) { if (s.first != null) { this.first = new Node <Item>(s.first); for (Node <Item> x = this.first; x.next != null; x = x.next) { x.next = new Node <Item>(x.next); } } this.count = s.count; }
/// <summary> /// 使用给定的数组对链栈进行一次测试,返回耗时(毫秒)。 /// </summary> /// <param name="a">测试用的数组。</param> /// <returns>耗时(毫秒)。</returns> public static double TimeTrialLinkedStack(int[] a) { var stack = new LinkedStack <int>(); var n = a.Length; var timer = new Stopwatch(); for (var i = 0; i < n; i++) { stack.Push(a[i]); } for (var i = 0; i < n; i++) { stack.Pop(); } return(timer.ElapsedTimeMillionSeconds()); }
/// <summary> /// 将两个栈连接。 /// </summary> /// <param name="s1">第一个栈。</param> /// <param name="s2">第二个栈(将被删除)。</param> /// <returns></returns> public static LinkedStack <Item> Catenation(LinkedStack <Item> s1, LinkedStack <Item> s2) { if (s1.IsEmpty()) { s1.first = s2.first; s1.count = s2.count; } else { var last = s1.first; while (last.next != null) { last = last.next; } last.next = s2.first; s1.count += s2.count; } s2 = null; return(s1); }