C++ Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class linkedStackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various functions and operators of the class linkedStackType.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

C++

Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class linkedStackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various functions and operators of the class linkedStackType.

Instructions
Instructions
Two stacks of the same type are the same if they have the
same number of elements and their elements at the
corresponding positions are the same.
Overload the relational operator == for the class
linkedStackType that returns true if two stacks of the
same type are the same; it returns false otherwise.
Also, write the definition of the function template to overload
this operator.
Write a program to test the various functions and operators
of the class linkedStackType.
Grading
When you have completed your program, click the Submit
button to record your score.
linkedStack.h
1
2 //Header File: linkedStack.h
3
4 #ifndef H_StackType
5 #define H_StackType
6
7 #include <iostream>
8 #include <cassert>
9
10 #include "stackADT.h"
11
12 using namespace std;
13
14 //Definition of the node
15 template <class Type>
16 struct node Type
17 {
18
19
20 };
21
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
22 template <class Type>
23 class linkedStackType: public stackADT<Type>
24 {
25 public:
26
27
28
29
30
31
69 70 71 72
91 }
92
Type info;
node Type<Type> *link;
const linkedStackType<Type>& operator=
//
//Overload the assignment operator.
bool isEmptyStack() const;
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty;
otherwise returns false.
139
140
main.cpp
//
bool is FullStack() const;
//Function to determine whether the stack is full.
//Postcondition: Returns false.
void initializeStack();
//Function to initialize the stack to an empty state.
//Postcondition: The stack elements are removed;
stackTop = nullptr;
//
169
void push(const Type& newItem);
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
is added to the top of the stack.
//
//
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Type top() const;
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
terminates; otherwise, the top
element of the stack is returned.
//
73
74
75
76
77 private:
78
79
80
81
82
83
84 };
85
void pop();
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
element is removed from the stack.
linkedStackType();
//Default constructor
//Postcondition: stackTop = nullptr;
-linkedStackType();
//Destructor
//Postcondition:
linkedStackType (const linkedStackType<Type>& otherStack);
//Copy constructor
//
All the elements of the stack are
removed from the stack.
bool operator==(const linkedStackType<Type>& otherStack) const;
//
86
87 template <class Type>
88 linkedStack Type<Type> ::linkedStackType()
89 {
90
nodeType<Type> *stackTop; //pointer to the stack
void copyStack (const linkedStackType<Type>& otherStack);
//Function to make a copy of otherStack.
//Default constructor
{
//Postcondition: A copy of otherStack is created and
assigned to this stack.
stackTop = nullptr;
93 template <class Type>
94 bool linkedStackType<Type>::isEmptyStack() const
95 {
96
97}//end isEmptyStack
stackADT.h
(const linkedStackType<Type>&);
return false;
103}//end isFullStack
return (stack Top == nullptr);
98
99 template <class Type>
100 bool linkedStackType<Type> :: isFullStack() const
101 {
102
104
105 template <class Type>
106 void linked StackType<Type>::initializeStack()
107 {
108 nodeType<Type> *temp; //pointer to delete the node
109
110
111
112
113
114
115
116
117
118 }
119} //end initializeStack
120
while (stackTop != nullptr) //while there are elements in
//the stack
132}//end push
temp stackTop;
delete temp;
121 template <class Type>
122 void linkedStackType<Type>::push(const Type& newElement)
123 {
124
125
126
127
128
129
130
131
//set temp to point to the
//current node
stackTop = stackTop->link; //advance stackTop to the
133
134
135 template <class Type>
136 Type linked StackType<Type>::top() const
137 {
138
141}//end top
+
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the node
newNode->info = newElement; //store newElement in the node
newNode->link = stackTop; //insert newNode before stackTop
stackTop newNode;
//set stackTop to point to the
//top node
}
else
return stackTop->info;
142
143 template <class Type>
144 void linkedStackType<Type> : :pop()
145 {
146
147
148
149
150
151
152
153
154
155
156
157
158 }//end pop
assert (stack Top != nullptr); //if stack is empty,
//terminate the program
//return the top element
//next node
//deallocate memory occupied by temp
else
{
nodeType<Type> *temp; //pointer to deallocate memory
if (stackTop != nullptr)
{
temp = stackTop; //set temp to point to the top node
stackTop stackTop->link; //advance stackTop to the
//next node
delete temp; //delete the top node
159
160 template <class Type>
161 void linkedStackType<Type>::copyStack
162
163 {
164 nodeType<Type> *newNode, *current, *last;
165
166
167
168
cout << "Cannot remove from an empty stack." << endl;
if (stackTop != nullptr) //if stack is nonempty, make it empty
initializeStack();
if (otherStack.stackTop == nullptr)
stackTop = nullptr;
(const linkedStackType<Type>& otherStack)
current = otherStack.stackTop; //set current to point
//to the stack to be copied.
//copy the stackTop element of the stack
stackTop = new node Type<Type>; //create the node
last stack Top;
current current->link;
stackTop->info = current->info; //copy the info
stackTop->link = nullptr; //set the link field of the
}//end while
//copy the remaining stack
while (current != nullptr)
{
}//end else
197}//end copyStack
//node to nullptr
//set last to point to the node
//set current to point to
//the next node
newNode = new nodeType<Type> ;
newNode->info = current->info;
newNode->link = nullptr;
last->link = newNode;
last = newNode;
current current->link;
198
199
//copy constructor
200 template <class Type>
201 linkedStackType<Type> ::linkedStackType(
202
203 {
const linkedStackType<Type>& otherStack)
linkedStack.h
1 #include <iostream>
2 #include "linkedStack.h"
3
4 using namespace std;
5
6 int main() {
7
8
9}
10
main.cpp
// Write your main here
return 0;
204 stackTop = nullptr;
205 copyStack(otherStack);
206 }//end copy constructor
207
208 //destructor
209 template <class Type>
210 linkedStack Type<Type> ::~linkedStackType()
211 {
initializeStack();
212
213 }//end destructor
214
stackADT.h
215 //overloading the assignment operator
216 template <class Type>
217 const linkedStackType<Type>& linkedStackType<Type>::operator=
218
(const linkedStackType<Type>& otherStack)
219 {
220 if (this ! &otherStack) //avoid self-copy
221
copyStack (otherStack);
222
223
return *this;
224 }//end operator=
225
226 #endif
227
+
linkedStack.h
1
2 //Header file: stackADT.h
3
4 #ifndef H_StackADT
5 #define H_StackADT
6
7 template <class Type>
8 class stackADT
9 {
10 public:
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
34
35
36
37
38
39
40
41
42
43 };
virtual void initializeStack() = 0;
//Method to initialize the stack to an empty state.
//Postcondition: Stack is empty.
//
virtual bool isEmptyStack() const = 0;
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty,
otherwise returns false.
main.cpp
//
virtual bool is FullStack() const = 0;
//Function to determine whether the stack is full.
//Postcondition: Returns true if the stack is full,
otherwise returns false.
stackADT.h
//
+
virtual void push(const Type& newItem) = 0;
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
is added to the top of the stack.
//
//
44
45 #endif
46
virtual Type top() const = 0;
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
//Postcondition: If the stack is empty, the program
terminates; otherwise, the top element
of the stack is returned.
//
virtual void pop() = 0;
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
element is removed from the stack.
Transcribed Image Text:Instructions Instructions Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class linkedStackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various functions and operators of the class linkedStackType. Grading When you have completed your program, click the Submit button to record your score. linkedStack.h 1 2 //Header File: linkedStack.h 3 4 #ifndef H_StackType 5 #define H_StackType 6 7 #include <iostream> 8 #include <cassert> 9 10 #include "stackADT.h" 11 12 using namespace std; 13 14 //Definition of the node 15 template <class Type> 16 struct node Type 17 { 18 19 20 }; 21 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 22 template <class Type> 23 class linkedStackType: public stackADT<Type> 24 { 25 public: 26 27 28 29 30 31 69 70 71 72 91 } 92 Type info; node Type<Type> *link; const linkedStackType<Type>& operator= // //Overload the assignment operator. bool isEmptyStack() const; //Function to determine whether the stack is empty. //Postcondition: Returns true if the stack is empty; otherwise returns false. 139 140 main.cpp // bool is FullStack() const; //Function to determine whether the stack is full. //Postcondition: Returns false. void initializeStack(); //Function to initialize the stack to an empty state. //Postcondition: The stack elements are removed; stackTop = nullptr; // 169 void push(const Type& newItem); //Function to add newItem to the stack. //Precondition: The stack exists and is not full. //Postcondition: The stack is changed and newItem is added to the top of the stack. // // 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 Type top() const; //Function to return the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: If the stack is empty, the program terminates; otherwise, the top element of the stack is returned. // 73 74 75 76 77 private: 78 79 80 81 82 83 84 }; 85 void pop(); //Function to remove the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: The stack is changed and the top element is removed from the stack. linkedStackType(); //Default constructor //Postcondition: stackTop = nullptr; -linkedStackType(); //Destructor //Postcondition: linkedStackType (const linkedStackType<Type>& otherStack); //Copy constructor // All the elements of the stack are removed from the stack. bool operator==(const linkedStackType<Type>& otherStack) const; // 86 87 template <class Type> 88 linkedStack Type<Type> ::linkedStackType() 89 { 90 nodeType<Type> *stackTop; //pointer to the stack void copyStack (const linkedStackType<Type>& otherStack); //Function to make a copy of otherStack. //Default constructor { //Postcondition: A copy of otherStack is created and assigned to this stack. stackTop = nullptr; 93 template <class Type> 94 bool linkedStackType<Type>::isEmptyStack() const 95 { 96 97}//end isEmptyStack stackADT.h (const linkedStackType<Type>&); return false; 103}//end isFullStack return (stack Top == nullptr); 98 99 template <class Type> 100 bool linkedStackType<Type> :: isFullStack() const 101 { 102 104 105 template <class Type> 106 void linked StackType<Type>::initializeStack() 107 { 108 nodeType<Type> *temp; //pointer to delete the node 109 110 111 112 113 114 115 116 117 118 } 119} //end initializeStack 120 while (stackTop != nullptr) //while there are elements in //the stack 132}//end push temp stackTop; delete temp; 121 template <class Type> 122 void linkedStackType<Type>::push(const Type& newElement) 123 { 124 125 126 127 128 129 130 131 //set temp to point to the //current node stackTop = stackTop->link; //advance stackTop to the 133 134 135 template <class Type> 136 Type linked StackType<Type>::top() const 137 { 138 141}//end top + nodeType<Type> *newNode; //pointer to create the new node newNode = new nodeType<Type>; //create the node newNode->info = newElement; //store newElement in the node newNode->link = stackTop; //insert newNode before stackTop stackTop newNode; //set stackTop to point to the //top node } else return stackTop->info; 142 143 template <class Type> 144 void linkedStackType<Type> : :pop() 145 { 146 147 148 149 150 151 152 153 154 155 156 157 158 }//end pop assert (stack Top != nullptr); //if stack is empty, //terminate the program //return the top element //next node //deallocate memory occupied by temp else { nodeType<Type> *temp; //pointer to deallocate memory if (stackTop != nullptr) { temp = stackTop; //set temp to point to the top node stackTop stackTop->link; //advance stackTop to the //next node delete temp; //delete the top node 159 160 template <class Type> 161 void linkedStackType<Type>::copyStack 162 163 { 164 nodeType<Type> *newNode, *current, *last; 165 166 167 168 cout << "Cannot remove from an empty stack." << endl; if (stackTop != nullptr) //if stack is nonempty, make it empty initializeStack(); if (otherStack.stackTop == nullptr) stackTop = nullptr; (const linkedStackType<Type>& otherStack) current = otherStack.stackTop; //set current to point //to the stack to be copied. //copy the stackTop element of the stack stackTop = new node Type<Type>; //create the node last stack Top; current current->link; stackTop->info = current->info; //copy the info stackTop->link = nullptr; //set the link field of the }//end while //copy the remaining stack while (current != nullptr) { }//end else 197}//end copyStack //node to nullptr //set last to point to the node //set current to point to //the next node newNode = new nodeType<Type> ; newNode->info = current->info; newNode->link = nullptr; last->link = newNode; last = newNode; current current->link; 198 199 //copy constructor 200 template <class Type> 201 linkedStackType<Type> ::linkedStackType( 202 203 { const linkedStackType<Type>& otherStack) linkedStack.h 1 #include <iostream> 2 #include "linkedStack.h" 3 4 using namespace std; 5 6 int main() { 7 8 9} 10 main.cpp // Write your main here return 0; 204 stackTop = nullptr; 205 copyStack(otherStack); 206 }//end copy constructor 207 208 //destructor 209 template <class Type> 210 linkedStack Type<Type> ::~linkedStackType() 211 { initializeStack(); 212 213 }//end destructor 214 stackADT.h 215 //overloading the assignment operator 216 template <class Type> 217 const linkedStackType<Type>& linkedStackType<Type>::operator= 218 (const linkedStackType<Type>& otherStack) 219 { 220 if (this ! &otherStack) //avoid self-copy 221 copyStack (otherStack); 222 223 return *this; 224 }//end operator= 225 226 #endif 227 + linkedStack.h 1 2 //Header file: stackADT.h 3 4 #ifndef H_StackADT 5 #define H_StackADT 6 7 template <class Type> 8 class stackADT 9 { 10 public: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 34 35 36 37 38 39 40 41 42 43 }; virtual void initializeStack() = 0; //Method to initialize the stack to an empty state. //Postcondition: Stack is empty. // virtual bool isEmptyStack() const = 0; //Function to determine whether the stack is empty. //Postcondition: Returns true if the stack is empty, otherwise returns false. main.cpp // virtual bool is FullStack() const = 0; //Function to determine whether the stack is full. //Postcondition: Returns true if the stack is full, otherwise returns false. stackADT.h // + virtual void push(const Type& newItem) = 0; //Function to add newItem to the stack. //Precondition: The stack exists and is not full. //Postcondition: The stack is changed and newItem is added to the top of the stack. // // 44 45 #endif 46 virtual Type top() const = 0; //Function to return the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: If the stack is empty, the program //Postcondition: If the stack is empty, the program terminates; otherwise, the top element of the stack is returned. // virtual void pop() = 0; //Function to remove the top element of the stack. //Precondition: The stack exists and is not empty. //Postcondition: The stack is changed and the top element is removed from the stack.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education