@@ -69,6 +69,9 @@ public class MyQueue<T> where T : IEquatable<T>
69
69
70
70
private readonly Queue < T > debug_queue ;
71
71
72
+
73
+ private bool whenEvenPushAndPopLeft ;
74
+
72
75
//private bool popLeftNext;
73
76
74
77
// TODO: FIGURE OUT WHAT'S GOING ON WITH THIS ENUM NOT WORKING
@@ -93,7 +96,7 @@ public MyQueue()
93
96
//popLeftNext = true;
94
97
95
98
96
- whenEvenPopLeft = true ; ;
99
+ whenEvenPushAndPopLeft = true ; ;
97
100
98
101
debug_queue = new Queue < T > ( ) ;
99
102
}
@@ -235,21 +238,33 @@ private void PadWithWhitespace(StringBuilder[] sbArr)
235
238
236
239
237
240
238
- private bool whenEvenPopLeft ;
241
+
239
242
240
243
public T Dequeue ( )
241
244
{
242
245
if ( Count == 0 )
243
246
throw new InvalidOperationException ( "Queue is empty." ) ;
244
247
245
- // Stacks are maintained after each Enqueue and Dequeue operation
246
- // for the next item for Dequeue to be on the top of the left stack
247
- // and the item-after-that for Dequeue to be on the top of the right
248
- // stack
249
- // Means we don't need a seperate tracking variable to remember
250
- // where to Dequeue items from or where to Enqueue items to
251
248
252
- T item = s1_left . Pop ( ) ;
249
+ T item ;
250
+
251
+ if ( s1_left . Count > s2_right . Count )
252
+ {
253
+ item = s1_left . Pop ( ) ;
254
+ whenEvenPushAndPopLeft = false ;
255
+ }
256
+ else if ( s1_left . Count < s2_right . Count )
257
+ {
258
+ item = s2_right . Pop ( ) ;
259
+ whenEvenPushAndPopLeft = true ;
260
+ }
261
+ else if ( whenEvenPushAndPopLeft )
262
+ item = s1_left . Pop ( ) ;
263
+ else
264
+ item = s2_right . Pop ( ) ;
265
+
266
+
267
+
253
268
254
269
//if (popLeftNext == true)
255
270
//{
@@ -269,32 +284,32 @@ public T Dequeue()
269
284
// popLeftNext = true;
270
285
271
286
272
- // After popping
287
+ //// After popping
273
288
274
- // If right stack is taller
275
- // Fixup: Move the top item of the right stack to the left stack
276
- // Thus making the stacks equal height
277
- if ( s1_left . Count < s2_right . Count )
278
- s1_left . Push ( s2_right . Pop ( ) ) ;
279
- // Now the next item to Dequeue is on the top of the left stack
280
- // and the item-after-that to Dequeue is on the top of the right
281
- // stack
289
+ //// If right stack is taller
290
+ //// Fixup: Move the top item of the right stack to the left stack
291
+ //// Thus making the stacks equal height
292
+ // if (s1_left.Count < s2_right.Count)
293
+ // s1_left.Push(s2_right.Pop());
294
+ //// Now the next item to Dequeue is on the top of the left stack
295
+ //// and the item-after-that to Dequeue is on the top of the right
296
+ //// stack
282
297
283
298
284
- // My guess is that if the stacks are the same height, that sometimes
285
- // we should swap the top ones and sometimes we should not (based on
286
- // something! maybe we do need an additional variable) Let's experiment...
287
- else if ( s1_left . Count == s2_right . Count && s1_left . Count > 0 && whenEvenPopLeft )
288
- {
289
- // Let's try swapping them all of the time and see what happen
290
- var temp = s1_left . Pop ( ) ;
291
- s1_left . Push ( s2_right . Pop ( ) ) ;
292
- s2_right . Push ( temp ) ;
299
+ //// My guess is that if the stacks are the same height, that sometimes
300
+ //// we should swap the top ones and sometimes we should not (based on
301
+ //// something! maybe we do need an additional variable) Let's experiment...
302
+ // else if (s1_left.Count == s2_right.Count && s1_left.Count > 0 && whenEvenPushAndPopLeft )
303
+ // {
304
+ // // Let's try swapping them all of the time and see what happen
305
+ // var temp = s1_left.Pop();
306
+ // s1_left.Push(s2_right.Pop());
307
+ // s2_right.Push(temp);
293
308
294
- whenEvenPopLeft = false ;
295
- }
309
+ // whenEvenPushAndPopLeft = false;
310
+ // }
296
311
297
- // Maybe it is a simple alternation
312
+ //// Maybe it is a simple alternation
298
313
299
314
300
315
@@ -359,29 +374,77 @@ public T Dequeue()
359
374
360
375
public void Enqueue ( T item )
361
376
{
362
- // Enqueue and Dequeue designed to always leave the left stack
363
- // taller when Count is odd
364
377
365
- if ( Count % 2 == 1 )
366
- {
367
- while ( s2_right . Count > 0 )
368
- s1_left . Push ( s2_right . Pop ( ) ) ;
378
+ // When odd, push onto the shorter stack (evening it up)
379
+ // Bookkeep where to push/pop next (since now even)
369
380
370
- s2_right . Push ( item ) ;
381
+ // When even, check bookkeeping to see where to push
382
+ // Note that no bookkeeping is necessary, since now the stacks
383
+ // are odd (and you always pop from the taller stack and
384
+ // push onto the shorter stack to maintain the zig-zag ordering)
371
385
372
- while ( s1_left . Count != s2_right . Count )
373
- s2_right . Push ( s1_left . Pop ( ) ) ;
374
- }
375
- else
376
- {
377
- while ( s1_left . Count > 0 )
378
- s2_right . Push ( s1_left . Pop ( ) ) ;
379
386
380
- s1_left . Push ( item ) ;
387
+ if ( s1_left . Count > s2_right . Count )
388
+ {
389
+ // Push onto the right stack
390
+ while ( s2_right . Count > 0 )
391
+ s1_left . Push ( s2_right . Pop ( ) ) ;
392
+
393
+ s2_right . Push ( item ) ;
394
+
395
+ while ( s1_left . Count != s2_right . Count )
396
+ s2_right . Push ( s1_left . Pop ( ) ) ;
397
+
398
+ // Remember that the left stack still has the top
399
+ whenEvenPushAndPopLeft = true ;
400
+ }
401
+ else if ( s1_left . Count < s2_right . Count )
402
+ {
403
+ // Push onto the left stack
404
+ while ( s1_left . Count > 0 )
405
+ s2_right . Push ( s1_left . Pop ( ) ) ;
406
+
407
+ s1_left . Push ( item ) ;
408
+
409
+ while ( s1_left . Count != s2_right . Count )
410
+ s1_left . Push ( s2_right . Pop ( ) ) ;
411
+
412
+ // Remember that the right stack now has the top
413
+ whenEvenPushAndPopLeft = false ;
414
+ }
415
+ else if ( whenEvenPushAndPopLeft )
416
+ {
417
+ // REFACTOR OUT THIS COPYPASTA FROM ABOVE
418
+
419
+ // Push onto the left stack
420
+ while ( s1_left . Count > 0 )
421
+ s2_right . Push ( s1_left . Pop ( ) ) ;
422
+
423
+ s1_left . Push ( item ) ;
424
+
425
+ while ( s1_left . Count <= s2_right . Count )
426
+ s1_left . Push ( s2_right . Pop ( ) ) ;
427
+
428
+ // No bookkeeping necessary...now the left stack
429
+ // is taller and obviously the top
430
+ }
431
+ else
432
+ {
433
+ // REFACTOR OUT THIS COPYPASTA FROM ABOVE
434
+
435
+ // Push onto the right stack
436
+ while ( s2_right . Count > 0 )
437
+ s1_left . Push ( s2_right . Pop ( ) ) ;
438
+
439
+ s2_right . Push ( item ) ;
440
+
441
+ while ( s1_left . Count >= s2_right . Count )
442
+ s2_right . Push ( s1_left . Pop ( ) ) ;
443
+
444
+ // No bookkeeping necessary...now the right stack
445
+ // is taller and obviously the top
446
+ }
381
447
382
- while ( s1_left . Count <= s2_right . Count )
383
- s1_left . Push ( s2_right . Pop ( ) ) ;
384
- }
385
448
386
449
387
450
debug_queue . Enqueue ( item ) ;
0 commit comments