-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDualStack.cpp
More file actions
67 lines (66 loc) · 1.51 KB
/
DualStack.cpp
File metadata and controls
67 lines (66 loc) · 1.51 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
#include "DualStack.h"
#include <iostream>
DualStack::DualStack(int n)
{
dual_stack = new int[n];
dual_stack_length = n;
stack_one_length = 0;
stack_two_length = 0;
}
bool DualStack::push1(int x)
{
if (!isFull1()) //For consistency sake, we'll use isFullx() of the stack we're trying to push to.
{
dual_stack[stack_one_length++] = x;
return true;
}
return false;
}
bool DualStack::push2(int x)
{
if (!isFull2()) //For consistency sake, we'll use isFullx() of the stack we're trying to push to.
{
dual_stack[(dual_stack_length - 1) - stack_two_length] = x;
stack_two_length++;
return true;
}
return false;
}
int DualStack::peek1()
{
if (isEmpty1()) throw("Cannot peek, stack is empty");
return dual_stack[stack_one_length - 1];
}
int DualStack::peek2()
{
if (isEmpty2()) throw("Cannot peek, stack is empty");
return dual_stack[dual_stack_length - stack_two_length];
}
bool DualStack::pop1()
{
if (!isEmpty1()) stack_one_length--;
}
bool DualStack::pop2()
{
if (!isEmpty2()) stack_two_length--;
}
bool DualStack::isFull1()
{
if (stack_one_length + stack_two_length == dual_stack_length) return true;
return false;
}
bool DualStack::isFull2()
{
if (stack_one_length + stack_two_length == dual_stack_length) return true;
return false;
}
bool DualStack::isEmpty1()
{
if (stack_one_length == 0) return true;
return false;
}
bool DualStack::isEmpty2()
{
if (stack_two_length == 0) return true;
return false;
}