-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreeinonestacks.cs
54 lines (53 loc) · 1.54 KB
/
threeinonestacks.cs
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
using System;
using System.Collections;
public class ThreeStacksWithArray
{
private int[] stackpointers = new int[3];
int[] array = new int[1]();
public ThreeAtacksWithArray(int stacksizes)
{
if(stacksizes % 3 != 0)
{
stacksizes *= 3;
//Guarantees that the size of the stack will be divisble by three and thus my implementation works.
}
array = new int[stacksizes];
stackpointers[1] = 1;
stackpointers[2] = 2;
}
public void Push(int stack, int value)
{ //Checks for bad values, there is probably a better way to do this.
if(stack >= 3)
{
throw new ArgumentOutOfRangeException("stack out of bounds");
}
if(stackpointers[stack] >= array.Length - 4)
{
throw new ArgumentOutOfRangeException("Stack is out of space");
}
array[stackpointers[stack]] = value;
stackpointers[stack] += 3;
}
public int Pop(int stack)
{
if((stack == 0) && (stackpointers[0] == 0))
{
return 0;
}
else if((stack == 1) && (stackpointers[1] == 1))
{
return 0;
}
else if((stack == 2) && (stackpointers[2] == 2))
{
return 0;
}
else
{
stackpointers[stack] -= 3;
int temp = array[stackpointers[stack]];
array[stackpointers[stack]] = 0;
}
}
//Source: Written by Connor Alexander Matza
}