@@ -223,201 +223,45 @@ func (q *DoubleEndsQueue) IsEmpty() bool {
223223
224224> 利用双向链表(双端队列)封装栈和队列
225225
226- ``` Java
227- package class02 ;
228-
229- import java.util.LinkedList ;
230- import java.util.Queue ;
231- import java.util.Stack ;
232-
233- public class Code03_DoubleEndsQueueToStackAndQueue {
234-
235- public static class Node <T> {
236- public T value;
237- public Node<T > last;
238- public Node<T > next;
239-
240- public Node (T data ) {
241- value = data;
242- }
243- }
244-
245- public static class DoubleEndsQueue <T> {
246- public Node<T > head;
247- public Node<T > tail;
248-
249- // 从头部加节点
250- public void addFromHead (T value ) {
251- Node<T > cur = new Node<T > (value);
252- if (head == null ) {
253- head = cur;
254- tail = cur;
255- } else {
256- cur. next = head;
257- head. last = cur;
258- head = cur;
259- }
260- }
261-
262- // 从尾部加节点
263- public void addFromBottom (T value ) {
264- Node<T > cur = new Node<T > (value);
265- if (head == null ) {
266- head = cur;
267- tail = cur;
268- } else {
269- cur. last = tail;
270- tail. next = cur;
271- tail = cur;
272- }
273- }
274-
275- // 从头部弹出节点
276- public T popFromHead () {
277- if (head == null ) {
278- return null ;
279- }
280- Node<T > cur = head;
281- if (head == tail) {
282- head = null ;
283- tail = null ;
284- } else {
285- head = head. next;
286- cur. next = null ;
287- head. last = null ;
288- }
289- return cur. value;
290- }
291-
292- // 从尾部弹出节点
293- public T popFromBottom () {
294- if (head == null ) {
295- return null ;
296- }
297- Node<T > cur = tail;
298- if (head == tail) {
299- head = null ;
300- tail = null ;
301- } else {
302- tail = tail. last;
303- tail. next = null ;
304- cur. last = null ;
305- }
306- return cur. value;
307- }
308-
309- // 该双向链表结构是否为空
310- public boolean isEmpty () {
311- return head == null ;
312- }
313-
314- }
315-
316- // 用上述双向链表结构实现栈
317- public static class MyStack <T> {
318- private DoubleEndsQueue<T > queue;
319-
320- public MyStack () {
321- queue = new DoubleEndsQueue<T > ();
322- }
323-
324- public void push (T value ) {
325- queue. addFromHead(value);
326- }
327-
328- public T pop () {
329- return queue. popFromHead();
330- }
331-
332- public boolean isEmpty () {
333- return queue. isEmpty();
334- }
335-
336- }
337-
338- // 用上述双向链表结构实现队列
339- public static class MyQueue <T> {
340- private DoubleEndsQueue<T > queue;
341-
342- public MyQueue () {
343- queue = new DoubleEndsQueue<T > ();
344- }
345-
346- public void push (T value ) {
347- queue. addFromHead(value);
348- }
226+ ``` Go
227+ // Stack 利用双端队列实现栈
228+ type Stack struct {
229+ qu *DoubleEndsQueue
230+ }
349231
350- public T poll ( ) {
351- return queue . popFromBottom();
352- }
232+ func ( s * Stack ) push ( v int ) {
233+ s. qu . AddFromHead (v)
234+ }
353235
354- public boolean isEmpty ( ) {
355- return queue . isEmpty();
356- }
236+ func ( s * Stack ) pop () (int, bool ) {
237+ return s. qu . PopFromHead ()
238+ }
357239
358- }
240+ func (s *Stack )IsEmpty () bool {
241+ return s.qu .IsEmpty ()
242+ }
243+ ```
359244
360- public static boolean isEqual (Integer o1 , Integer o2 ) {
361- if (o1 == null && o2 != null ) {
362- return false ;
363- }
364- if (o1 != null && o2 == null ) {
365- return false ;
366- }
367- if (o1 == null && o2 == null ) {
368- return true ;
369- }
370- return o1. equals(o2);
371- }
245+ ``` Go
246+ // Queue 利用双端队列实现队列
247+ type Queue struct {
248+ qu *DoubleEndsQueue
249+ }
372250
373- public static void main (String [] args ) {
374- int oneTestDataNum = 100 ;
375- int value = 10000 ;
376- int testTimes = 100000 ;
377- for (int i = 0 ; i < testTimes; i++ ) {
378- MyStack<Integer > myStack = new MyStack<> ();
379- MyQueue<Integer > myQueue = new MyQueue<> ();
380- Stack<Integer > stack = new Stack<> ();
381- Queue<Integer > queue = new LinkedList<> ();
382- for (int j = 0 ; j < oneTestDataNum; j++ ) {
383- int nums = (int ) (Math . random() * value);
384- if (stack. isEmpty()) {
385- myStack. push(nums);
386- stack. push(nums);
387- } else {
388- if (Math . random() < 0.5 ) {
389- myStack. push(nums);
390- stack. push(nums);
391- } else {
392- if (! isEqual(myStack. pop(), stack. pop())) {
393- System . out. println(" oops!" );
394- }
395- }
396- }
397- int numq = (int ) (Math . random() * value);
398- if (stack. isEmpty()) {
399- myQueue. push(numq);
400- queue. offer(numq);
401- } else {
402- if (Math . random() < 0.5 ) {
403- myQueue. push(numq);
404- queue. offer(numq);
405- } else {
406- if (! isEqual(myQueue. poll(), queue. poll())) {
407- System . out. println(" oops!" );
408- }
409- }
410- }
411- }
412- }
413- System . out. println(" finish!" );
414- }
251+ func (q *Queue ) push (v int ) {
252+ q.qu .AddFromHead (v)
253+ }
415254
255+ func (q *Queue ) poll () (int , bool ) {
256+ return q.qu .PopFromBottom ()
416257}
417258
259+ func (q *Queue )IsEmpty () bool {
260+ return q.qu .IsEmpty ()
261+ }
418262```
419263
420- > 数组实现, 对于栈特别简单,对于队列,如下
264+ > 数组实现栈和队列, 对于栈特别简单,对于队列,如下
421265
422266``` Java
423267package class02 ;
0 commit comments