Skip to content

Commit ca2b2fd

Browse files
committed
Finally solved the "Make a Queue out of two stacks" problem. Whew! now to clean it up and refactor it for the video!
1 parent 19068db commit ca2b2fd

File tree

1 file changed

+111
-48
lines changed

1 file changed

+111
-48
lines changed

Assignment5/Problem2.cs

+111-48
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public class MyQueue<T> where T : IEquatable<T>
6969

7070
private readonly Queue<T> debug_queue;
7171

72+
73+
private bool whenEvenPushAndPopLeft;
74+
7275
//private bool popLeftNext;
7376

7477
// TODO: FIGURE OUT WHAT'S GOING ON WITH THIS ENUM NOT WORKING
@@ -93,7 +96,7 @@ public MyQueue()
9396
//popLeftNext = true;
9497

9598

96-
whenEvenPopLeft = true; ;
99+
whenEvenPushAndPopLeft = true; ;
97100

98101
debug_queue = new Queue<T>();
99102
}
@@ -235,21 +238,33 @@ private void PadWithWhitespace(StringBuilder[] sbArr)
235238

236239

237240

238-
private bool whenEvenPopLeft;
241+
239242

240243
public T Dequeue()
241244
{
242245
if (Count == 0)
243246
throw new InvalidOperationException("Queue is empty.");
244247

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
251248

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+
253268

254269
//if (popLeftNext == true)
255270
//{
@@ -269,32 +284,32 @@ public T Dequeue()
269284
// popLeftNext = true;
270285

271286

272-
// After popping
287+
//// After popping
273288

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
282297

283298

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);
293308

294-
whenEvenPopLeft = false;
295-
}
309+
// whenEvenPushAndPopLeft = false;
310+
//}
296311

297-
// Maybe it is a simple alternation
312+
//// Maybe it is a simple alternation
298313

299314

300315

@@ -359,29 +374,77 @@ public T Dequeue()
359374

360375
public void Enqueue(T item)
361376
{
362-
// Enqueue and Dequeue designed to always leave the left stack
363-
// taller when Count is odd
364377

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)
369380

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)
371385

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() );
379386

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+
}
381447

382-
while (s1_left.Count <= s2_right.Count)
383-
s1_left.Push(s2_right.Pop());
384-
}
385448

386449

387450
debug_queue.Enqueue(item);

0 commit comments

Comments
 (0)