-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
129 lines (123 loc) · 2 KB
/
stack.h
File metadata and controls
129 lines (123 loc) · 2 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
#include <iostream>
#include "assert.h"
using namespace std;
template <class t>
struct Stack
{
int maxSize;
int top;
//pointer to the array that holds the stack elements ->
t* list;
public:
// #1 constructor
Stack(int SIZE = 100);
//#2 copy constructor
Stack(const Stack<t>& otherStack);
// #3 operator
const Stack<t> & operator=(const Stack<t>&);
// #4 Destructor
~Stack();
// #5
void initializeStack();
//#6 returns true if empty else false
bool isEmpty() const;
// #7 returns true if full else false
bool isFull();
// #8
void push(const t& item);
// #9
t pop();
// #10 This function returns the top element
int showTop();
};
// #1
// CONSTRUCTOR
template <class t>
Stack<t>::Stack(int length)
{
if (length <= 0)
{
cout << "Error! length must be a positive value." << endl;
}
else
{
maxSize = length;
top = 0;
//t *temp = list;
//list[top-1] = 0;
list = new t[maxSize];
}
}
// #2
// Copy Contructor
template <class t>
Stack<t>::Stack(const Stack<t>& otherStack)
{
delete[] list;
maxSize = otherStack.maxSize;
top = otherStack.top;
}
// #3
// operator
template <class t>
const Stack<t> & Stack<t>::operator= (const Stack<t>& otherStack)
{
if (this != &otherStack) //avoid self-copy
{
delete[] list;
maxSize = otherStack.maxSize;
top = otherStack.top;
return *this;
}
}
// #4
template <class t>
Stack<t>::~Stack()
{
delete[] list;
}
// #5
template <class t>
void Stack<t>::initializeStack()
{
top = 0;
}
// #6
template <class t>
bool Stack<t>::isEmpty()const
{
return(top == 0);
}
// #7
template <class t>
bool Stack<t>::isFull()
{
return(top == maxSize);
}
// #8
template <class t>
void Stack<t>::push(const t & item)
{
if (!isFull())
{
list[top] = item;
top++;
}
else cout << "Error Stack is full" << endl;
}
// #9
template <class t>
t Stack<t>::pop() // need change
{
if (!isEmpty())
top--;
else cout << "Error List is Empty" << endl;
return list[top];
}
// #10
template <class t>
int Stack<t>::showTop()
{
assert(top != 0);
return list[top-1];
}