-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.h
79 lines (66 loc) · 1.38 KB
/
stack.h
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
#pragma once
#include "StackADT.h"
//Unless spesificed by the stack user, the default size is 100
template<typename T>
class stack : public StackADT<T>
{
private:
T* items; // Array of stack items
int top; // Index to top of stack
const int STACK_SIZE =500;
public:
stack()
{
//STACK_SIZE = 500;
items = new T[STACK_SIZE];
top = -1;
} // end default constructor
//Function getCapacity() returns the stack max size
//added for array implementaion only
int getCapacity()
{
return STACK_SIZE;
}
bool isEmpty() const
{
return top == -1;
} // end isEmpty
bool push(const T& newEntry)
{
if (top == STACK_SIZE - 1) return false; //Stack is FULL
top++;
items[top] = newEntry;
return true;
} // end push
bool pop(T& TopEntry)
{
if (isEmpty()) return false;
TopEntry = items[top];
top--;
return true;
} // end pop
bool peek(T& TopEntry) const
{
if (isEmpty()) return false;
TopEntry = items[top];
return true;
} // end peek
const T* Toarray(int& count)
{
count = top + 1;
return items;
}
//Destructor
~stack()
{
delete[]items;
}
//Copy constructor
stack(const stack<T>& S) :STACK_SIZE(S.STACK_SIZE)
{
items = new T[STACK_SIZE];
for (int i = 0; i <= S.top; i++)
items[i] = S.items[i];
top = S.top;
}
}; // end stack