-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.h
111 lines (98 loc) · 2.19 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
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
#pragma once
#include <iostream>
// This header file contains the Stack Class implementation for the
// self growable and self shrinking data structure
// with shadow copying
class Stack{
private:
double threshold;
int size;
int top;
int *stack;
public:
Stack();
Stack(int s);
~Stack();
void push(int n);
int pop();
int isEmpty();
int isFull();
void setThreshold(double t);
void displayItems();
};
Stack::Stack(){
threshold = 0.75;
size = 5;
top = -1;
stack = new int[size];
}
Stack::Stack(int s){
threshold = 0.75;
size = s;
top = -1;
stack = new int[size];
}
Stack::~Stack(){
delete[]stack;
}
int Stack::isEmpty(){
if(top==-1)
return 1;
else
return 0;
}
void Stack::setThreshold(double t){
threshold = t;
}
int Stack::isFull(){
if(top==(size-1))
return 1;
else
return 0;
}
void Stack::push(int n){
using namespace std;
if(top >= threshold*(size-1)){
size += size;
int *stackNew = new int[size];
for (int i = 0; i <= top; i++)
{
stackNew[i] = stack[i];
}
delete[]stack;
stack = stackNew;
}
++top;
stack[top]=n;
}
int Stack::pop(){
using namespace std;
if(isEmpty()){
cout << "Empty Stack.. Returning 0" << endl;
return 0;
}
int temp = stack[top--];
if(top < (1-threshold)*size){
size = (size / 2) + 1;
int *stackNew = new int[size];
for (int i = 0; i <= top; i++)
{
stackNew[i] = stack[i];
}
delete[]stack;
stack = stackNew;
}
return temp;
}
void Stack::displayItems(){
using namespace std;
cout << "Stack \n------------------" << endl;
cout << "Size: " << size << endl;
cout << "No of Elements: " << top+1 << endl;
cout << "Stack Elements \n------------------" << endl;
for (int i = top; i >= 0; i--)
{
cout << stack[i] << " ";
}
cout << "\n------------------\n\n" << endl;
}