-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackt.cpp
More file actions
73 lines (59 loc) · 1.53 KB
/
Stackt.cpp
File metadata and controls
73 lines (59 loc) · 1.53 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
// File: Stackt.cpp
// Stack template class implementation
#include <iostream>
using namespace std;
// Constructor with argument, size is nelements, default is 128
template <class Type>
Stackt<Type>::Stackt(int nelements)
{ MaxSize = nelements; stack = new Type[MaxSize]; top = -1; }
// Copy Constructor
template <class Type>
Stackt <Type>::Stackt(const Stackt<Type> &original)
:MaxSize(original.MaxSize), top(original.top)
{
stack = new Type[MaxSize];
for (int i = 0; i <= top; i++) stack[i] = original.stack[i];
}
// Destructor
template <class Type>
Stackt<Type>::~Stackt()
{ delete [] stack;}
// Push
template <class Type>
void Stackt<Type>::push(Type v)
{
if(stackIsFull()) cout << "Stack Overflow" << endl;
else
{
stack[++top] = v;
size ++;
}
}
// Pop
template <class Type>
void Stackt<Type>::pop(Type &v)
{
if(stackIsEmpty()) cout << "Stack Underflow" << endl;
else v = stack[top--];
}
// Retrieve stack top without removing it
template <class Type>
void Stackt<Type>::stackTop(Type &v) const
{
if(stackIsEmpty()) cout << "Stack Underflow";
else v = stack[top];
}
// Test for Empty stack
template <class Type>
bool Stackt<Type>::stackIsEmpty() const
{ return (top < 0); }
// Test for Full stack
template <class Type>
bool Stackt<Type>::stackIsFull() const
{ return (top >= (MaxSize-1)); }
// template <class Type>
// int Stackt<Type>::gettop()const
// {return top+1 ;}
template <class Type>
int Stackt<Type>::getsize()const
{return size ;}