Skip to content

Commit 4f3d316

Browse files
committed
Set up the Debug_View for Prob 3
(Make a Stack out of two Queues) Haven't tested yet, but hopes are high
1 parent ca2b2fd commit 4f3d316

File tree

2 files changed

+166
-4
lines changed

2 files changed

+166
-4
lines changed

Assignment5/Problem2.cs

-4
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ public MyQueue()
105105

106106
public string Debug_View
107107
{
108-
// This approach should work as long as it is executed in-between
109-
// Enqueue and Dequeue method calls
110-
// Using this during one of those calls may mess up the formatting
111-
// May need to return and make more robust if that is the goal
112108
get
113109
{
114110
var tempStack = new Stack<T>();

Assignment5/Problem3.cs

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Assignment5
7+
{
8+
class Problem3
9+
{
10+
public static void RunInteractiveTesting()
11+
{
12+
string intro =
13+
"==============\n" +
14+
"= Problem #3 =\n" +
15+
"==============\n" +
16+
"\n" +
17+
"Implement a Stack using two queues, q1 and q2.\n";
18+
19+
Console.WriteLine(intro);
20+
21+
var stack = new MyStack<string>();
22+
23+
string input = "default";
24+
25+
while (input != "done")
26+
{
27+
Console.WriteLine(stack.Debug_View);
28+
29+
Console.WriteLine("\nEnter Stack command or \"done\"\n");
30+
input = Console.ReadLine();
31+
string[] commands = input.Split(' ');
32+
33+
if (commands[0] == "push")
34+
{
35+
var item = commands[1];
36+
stack.Push(item);
37+
Console.WriteLine($"Enqueued: {item}\n");
38+
}
39+
else if (commands[0] == "pop")
40+
{
41+
Console.WriteLine($"Dequeued: {stack.Pop()}\n");
42+
}
43+
}
44+
}
45+
46+
}
47+
48+
public class MyStack<T>
49+
{
50+
private readonly Queue<T> q1_above;
51+
private readonly Queue<T> q2_below;
52+
53+
private readonly Stack<T> debug_stack;
54+
55+
56+
public MyStack()
57+
{
58+
q1_above = new Queue<T>();
59+
q2_below = new Queue<T>();
60+
61+
debug_stack = new Stack<T>();
62+
}
63+
64+
public string Debug_View
65+
{
66+
get
67+
{
68+
69+
// Using screen coordinates convention for
70+
// the visualization: 0,0 is at upper left
71+
72+
// Above the baseline
73+
const int VERT_QUEUE_LAYOUT_LINES = 4;
74+
75+
var heightOfTallestSection =
76+
debug_stack.Count > VERT_QUEUE_LAYOUT_LINES ?
77+
debug_stack.Count : VERT_QUEUE_LAYOUT_LINES;
78+
79+
// +1 since we want a row below to label the queues and stack
80+
var sbArr = new StringBuilder[heightOfTallestSection + 1];
81+
// For simplicity, put the SB objects in first
82+
for (var i = 0; i < sbArr.Length; ++i)
83+
sbArr[i] = new StringBuilder();
84+
85+
86+
var sb_q1_above = new StringBuilder();
87+
88+
for (var i = 1; i <= q1_above.Count; ++i)
89+
{
90+
var item = q1_above.Dequeue();
91+
92+
sb_q1_above.Insert(0, $" {item}");
93+
94+
q1_above.Enqueue(item);
95+
}
96+
97+
var sb_q2_below = new StringBuilder();
98+
99+
for (var i = 1; i <= q2_below.Count; ++i)
100+
{
101+
var item = q2_below.Dequeue();
102+
103+
sb_q2_below.Insert(0, $" {item}");
104+
105+
q2_below.Enqueue(item);
106+
}
107+
108+
109+
sbArr[^5].Append(sb_q1_above);
110+
sbArr[^4].Append(nameof(q1_above));
111+
//sbArr[^3] is a blank line
112+
sbArr[^2].Append(sb_q2_below);
113+
sbArr[^1].Append(nameof(q2_below));
114+
115+
116+
const int IN_BETWEEN_PADDING = 5;
117+
118+
// Because the queue strings all have a leading space on them
119+
const int LEADING_PADDING = IN_BETWEEN_PADDING - 1;
120+
121+
var lengthOfLongestSB = sbArr.Max(sb => sb.Length);
122+
123+
foreach (var sb in sbArr)
124+
{
125+
sb.Insert(0, " ", LEADING_PADDING + lengthOfLongestSB - sb.Length);
126+
sb.Append(' ', IN_BETWEEN_PADDING);
127+
}
128+
129+
130+
131+
132+
133+
var tempStack = new Stack<T>();
134+
135+
// Get the items ready to read
136+
while (debug_stack.Count > 0)
137+
tempStack.Push(debug_stack.Pop());
138+
139+
for (var i = sbArr.Length - 2; tempStack.Count > 0; --i)
140+
{
141+
var item = tempStack.Pop();
142+
143+
sbArr[i].Append($"{item}");
144+
145+
// Put the items back
146+
debug_stack.Push(item);
147+
}
148+
sbArr[^1].Append(nameof(debug_stack));
149+
150+
151+
152+
153+
var finalSB = new StringBuilder();
154+
// Must disambiguate the call to AppendJoin
155+
finalSB.AppendJoin("\n", sbArr as IEnumerable<StringBuilder>);
156+
157+
return finalSB.ToString();
158+
}
159+
}
160+
161+
162+
163+
}
164+
165+
166+
}

0 commit comments

Comments
 (0)