|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace _1._3._48 |
| 9 | +{ |
| 10 | + public class Deque<Item> : IEnumerable<Item> |
| 11 | + { |
| 12 | + private class DoubleNode<T> |
| 13 | + { |
| 14 | + public T item; |
| 15 | + public DoubleNode<T> next; |
| 16 | + public DoubleNode<T> prev; |
| 17 | + } |
| 18 | + |
| 19 | + DoubleNode<Item> first; |
| 20 | + DoubleNode<Item> last; |
| 21 | + int count; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// 默认构造函数,建立一个双端队列。 |
| 25 | + /// </summary> |
| 26 | + public Deque() |
| 27 | + { |
| 28 | + this.first = null; |
| 29 | + this.last = null; |
| 30 | + this.count = 0; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// 检查队列是否为空。 |
| 35 | + /// </summary> |
| 36 | + /// <returns></returns> |
| 37 | + public bool IsEmpty() |
| 38 | + { |
| 39 | + return this.count == 0; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// 返回队列中元素的数量。 |
| 44 | + /// </summary> |
| 45 | + /// <returns></returns> |
| 46 | + public int Size() |
| 47 | + { |
| 48 | + return this.count; |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// 向左端添加一个元素。 |
| 53 | + /// </summary> |
| 54 | + /// <param name="item">要添加的元素。</param> |
| 55 | + public void PushLeft(Item item) |
| 56 | + { |
| 57 | + DoubleNode<Item> oldFirst = this.first; |
| 58 | + this.first = new DoubleNode<Item>() |
| 59 | + { |
| 60 | + item = item, |
| 61 | + prev = null, |
| 62 | + next = oldFirst |
| 63 | + }; |
| 64 | + if (oldFirst == null) |
| 65 | + { |
| 66 | + this.last = this.first; |
| 67 | + } |
| 68 | + this.count++; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// 向右端添加一个元素。 |
| 73 | + /// </summary> |
| 74 | + /// <param name="item">要添加的元素。</param> |
| 75 | + public void PushRight(Item item) |
| 76 | + { |
| 77 | + DoubleNode<Item> oldLast = this.last; |
| 78 | + this.last = new DoubleNode<Item>() |
| 79 | + { |
| 80 | + item = item, |
| 81 | + prev = oldLast, |
| 82 | + next = null |
| 83 | + }; |
| 84 | + |
| 85 | + if (oldLast == null) |
| 86 | + { |
| 87 | + this.first = this.last; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + oldLast.next = this.last; |
| 92 | + } |
| 93 | + this.count++; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// 从右端删除并返回一个元素。 |
| 98 | + /// </summary> |
| 99 | + /// <returns></returns> |
| 100 | + public Item PopRight() |
| 101 | + { |
| 102 | + if (IsEmpty()) |
| 103 | + { |
| 104 | + throw new InvalidOperationException(); |
| 105 | + } |
| 106 | + |
| 107 | + Item temp = this.last.item; |
| 108 | + this.last = this.last.prev; |
| 109 | + this.count--; |
| 110 | + |
| 111 | + if (this.last == null) |
| 112 | + { |
| 113 | + this.first = null; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + this.last.next.item = default(Item); |
| 118 | + this.last.next = null; |
| 119 | + } |
| 120 | + return temp; |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// 从左端删除并返回一个元素。 |
| 125 | + /// </summary> |
| 126 | + /// <returns></returns> |
| 127 | + public Item PopLeft() |
| 128 | + { |
| 129 | + if (IsEmpty()) |
| 130 | + { |
| 131 | + throw new InvalidOperationException(); |
| 132 | + } |
| 133 | + |
| 134 | + Item temp = this.first.item; |
| 135 | + this.first = this.first.next; |
| 136 | + this.count--; |
| 137 | + |
| 138 | + if (this.first == null) |
| 139 | + { |
| 140 | + this.last = null; |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + this.first.prev.item = default(Item); |
| 145 | + this.first.prev = null; |
| 146 | + } |
| 147 | + |
| 148 | + return temp; |
| 149 | + } |
| 150 | + |
| 151 | + public IEnumerator<Item> GetEnumerator() |
| 152 | + { |
| 153 | + return new DequeEnumerator(this.first); |
| 154 | + } |
| 155 | + |
| 156 | + IEnumerator IEnumerable.GetEnumerator() |
| 157 | + { |
| 158 | + return GetEnumerator(); |
| 159 | + } |
| 160 | + |
| 161 | + private class DequeEnumerator : IEnumerator<Item> |
| 162 | + { |
| 163 | + private DoubleNode<Item> current; |
| 164 | + private DoubleNode<Item> first; |
| 165 | + |
| 166 | + public DequeEnumerator(DoubleNode<Item> first) |
| 167 | + { |
| 168 | + this.current = new DoubleNode<Item>(); |
| 169 | + this.current.next = first; |
| 170 | + this.current.prev = null; |
| 171 | + this.first = this.current; |
| 172 | + } |
| 173 | + |
| 174 | + public Item Current => current.item; |
| 175 | + |
| 176 | + object IEnumerator.Current => current.item; |
| 177 | + |
| 178 | + public void Dispose() |
| 179 | + { |
| 180 | + this.current = null; |
| 181 | + this.first = null; |
| 182 | + } |
| 183 | + |
| 184 | + public bool MoveNext() |
| 185 | + { |
| 186 | + if (this.current.next == null) |
| 187 | + return false; |
| 188 | + this.current = this.current.next; |
| 189 | + return true; |
| 190 | + } |
| 191 | + |
| 192 | + public void Reset() |
| 193 | + { |
| 194 | + this.current = this.first; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments