forked from salman2481/basicQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cStack.cpp
109 lines (97 loc) · 2.9 KB
/
cStack.cpp
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
/*
LAST IN FIRST OUT
The nextNode reference of the last node is always null to avoid GARBAGE ACCESS
*/
#include<iostream>
#include"nodeClass.cpp"
using namespace std;
class cStack {
//The dynamic memory location that is going to serve as the BASE REFERENCE to the whole STACK
protected:
cNode *topRef;
public:
/*
Default Class Construstor
Using Initializer list to set the topReference as NULL for the empty STACK
*/
cStack() : topRef(NULL) {}
/*
Parameterized Class Constructor
Generating a single node stack using pointer Reference to avoid multiple pointer to same node
*/
cStack(cNode *&topPtr) : topRef(topPtr) { topRef->nextNode = NULL; topRef = NULL; }
/*
Check the BASE REFERENCE to determin whether the STACK is empty or not
*/
bool isNotEmpty() const { if (topRef) return true; else return false; }
bool isEmpty() const { if (topRef) return false; else return true; }
/*
Push a node at the end of the STACK using previousLastNode Next Reference to New node
Returning Reference will allow cascadeability use
*/
cStack& push(cNode *&nodeRef) { nodeRef->nextNode = topRef; topRef = nodeRef; nodeRef = NULL; return *this; }
/*
To extract the first node element of the STACK
*/
cNode* pop() { cNode *nodeRef = topRef; topRef = topRef->nextNode; nodeRef->nextNode = NULL; return nodeRef; }
//print function to print the whole stack from top to bottom order
void printStack() const {
if (!topRef)
cout << "\nEMPTY DATA STRUCTURE\n";
else {
cout << "The elements from TOP to BOTTOM are \n";
cNode *nodeRef = topRef;
//print all elements until last node NULL reference is reached
while (nodeRef) {
nodeRef->printData();
cout << "\n";
nodeRef = nodeRef->nextNode;
}
}
/*
Default Class Construstor
Using Initializer list to set the topReference as NULL for the empty STACK
*/
cStack() : topRef(NULL) {}
/*
Parameterized Class Constructor
}
/*
The default copy constructor gives shallow copy so default constructor is redefined
Two tracing pointers "sptr(source pointer) , dptr(destination pointer)" are used for assistance
*/
cStack(const cStack &src) {
this->topRef = src.topRef;
if (src.topRef) {
cNode *sptr, *dptr;
dptr = topRef = new cNode(*src.topRef);
sptr = src.topRef->nextNode;
while (sptr) {
dptr = new cNode(*sptr);
sptr = sptr->nextNode;
dptr = dptr->nextNode;
}
}
}
/*
Defining the assignment operator for the cStack class
*/
cStack& operator = (const cStack &src) {
if (this == &src) { return *this; }
if (true) { cStack temp; temp.topRef = this->topRef; }
if (true) { cStack temp = src; topRef = temp.topRef; temp.topRef = NULL; }
return *this;
}
/*
Default desstructor wouldn't delete the stack allocated on HEAP so the self defined deletes all nodes in STACK
*/
~cStack() {
cNode *nodeRef = topRef;
while (nodeRef) {
nodeRef = nodeRef->nextNode;
delete topRef;
topRef = nodeRef;
}
}
};
#pragma once