-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackQueue.cs
More file actions
190 lines (171 loc) · 3.8 KB
/
StackQueue.cs
File metadata and controls
190 lines (171 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Stack queue (a stack that also function queue at the same time) created by Will Gardner a long time ago based on a stackoverflow solution for making
//a queue out of a stack.
//Mit liscense in git
public class StackQueue<T>
{
//
private Stack<T> inbox;
private Stack<T> outbox;
// Start is called before the first frame update
public StackQueue(){
inbox = new Stack<T>();
outbox = new Stack<T>();
}
public StackQueue(StackQueue<T> sQ){
inbox = new Stack<T>();
outbox = sQ.CopyFullQueue();
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
}
public void push(T t){
inbox.Push(t);
}
public void pushToFront(T t){
while(!(inbox.Count==0)){
outbox.Push(inbox.Pop());
}
outbox.Push(t);
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
}
public T dequeueStack(int stackNum){
Stack<T> stack = (stackNum==0)?inbox:outbox;
Stack<T> tempStack = new Stack<T>();
while(!(stack.Count==0)){
tempStack.Push(stack.Pop());
}
T retVal = tempStack.Pop();
while(!(tempStack.Count==0)){
stack.Push(tempStack.Pop());
}
if(stackNum==0){
inbox = stack;
}
else{
outbox = stack;
}
return retVal;
}
public T dequeue(){
while(!(inbox.Count==0)){
outbox.Push(inbox.Pop());
}
T retVal = outbox.Pop();
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
return retVal;
}
public int Count{get{return inbox.Count;}}
public T pop(){
return inbox.Pop();
}
//bottom value peak
public T topQPeek(){
while(!(inbox.Count==0)){
outbox.Push(inbox.Pop());
}
T retVal = outbox.Peek();
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
return retVal;
}
public T topQNPeek(int N){
if(N ==1){
return topQ2Peek();
}else{
if(Count>N){
while(!(inbox.Count==0)){
outbox.Push(inbox.Pop());
}
T retVal = default;
for(int i = 0; i < N;i++){
// Debug.Log(i);
T temp1 = outbox.Pop();
inbox.Push(temp1);
}
retVal = outbox.Peek();
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
// Dibug.Log("Count:"+Count);
return retVal;
}
else{
return default;
}
}
}
//second bottommost value Peek
public T topQ2Peek(){
if(Count>1){
while(!(inbox.Count==0)){
outbox.Push(inbox.Pop());
}
T temp1 = outbox.Pop();
T retVal = outbox.Peek();
outbox.Push(temp1);
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
return retVal;
}
else{
return default;
}
}
//top most value
public T topSPeek(){
return inbox.Peek();
}
//second top most value peek
public T topSPeekN(int N){
if(N == 1){
return topSPeek2();
}
else{
if(Count<N){
return default;
}
//T temp1 = default;
T temp2 = default;
for(int i = 0; i < N; i++){
//Debug.Log(i);
outbox.Push(inbox.Pop());
temp2 = inbox.Peek();
}
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
//Debug.Log("Count:"+Count);
return temp2;
}
}
public T topSPeek2(){
if(Count<2){
return default;
}
T temp1 = inbox.Pop();
T temp2 = inbox.Peek();
inbox.Push(temp1);
return temp2;
}
public Stack<T> CopyFullQueue(){
Stack<T> retStack = new Stack<T>();
while(!(inbox.Count==0)){
T temp = inbox.Pop();
outbox.Push(temp);
retStack.Push(temp);
}
while(!(outbox.Count==0)){
inbox.Push(outbox.Pop());
}
return retStack;
}
}