Question
C++ HW correction Please correct the following code from the textbook: Introduction to Programming with C++ (3rd Edition) by Daniel Liang Chapter 12 Programming Excercise
C++ HW correction
Please correct the following code from the textbook:
Introduction to Programming with C++ (3rd Edition) by Daniel Liang
Chapter 12 Programming Excercise 7
(Function contains) Add the contains(T element) function into the Stack class as an instance function to check whether the element is in the stack. The Stack class was introduced in Listing, GenericStack.h. LISTING GenericStack.h
#ifndef STACK_H #define STACK_H
// Create a template prefix template // Define Stack class class Stack { public: // Function prototypes Stack(); bool empty(); T peek(); T push(T value); T pop(); int getSize(); void contains(T element); private: // Declare the variables T elements[100]; T element; int size; };
// No-argument generic constructor definition template Stack::Stack() { size = 0; }
// Generic function definition template bool Stack::empty() { return (size == 0); }
// Generic function definition template T Stack::peek() { return elements[size - 1]; }
// Generic function definition template T Stack::push(T value) { return elements[size++] = value; }
// Generic function definition template T Stack::pop() { return elements[--size]; }
// Generic function definition template int Stack::getSize() { return size; }
// Generic function definition template void Stack::contains(T element) { // Declare the variable int temp=0,pos=0; // Loop executes until the condition is satisfied for (int i=0; i { if (elements[i] == element) { temp=1; pos=i; } } // Print the result if (temp==1) cout
// Function main int main() { cout
cout>search;
// Call the contains() s1.contains(search); cout
cout
cout>search1;
// Call the contains() s2.contains(search1); cout
cout
cout>search2;
// Call the contains() s3.contains(search2); cout
Sample output: For Integer Elements List of elements in the stack 1 2 3 Enter an element to be searched: 3 Element 3 found at position 2 For Double Elements List of elements in the stack 1.5 2.4 3.3 Enter an element to be searched: 2.4 Element 2.4 found at position 1 For String Elements List of elements in the stack alpha beta gamma Enter an element to be searched: alpha Element alpha found at position 0 Done Press any key to continue . . .
Chapter 12 Programming Excercise 9
Implement a stack class using a vector) In Listing GenericStack is implemented using arrays. Implement it using a vector LISTING GenericStack.h
#ifndef STACK_H
#define STACK_H
//Create a template prefix template //Define Stack class class Stack { public: //Function prototypes bool empty() const; T peek() const; void push(T value); void pop(); T getSize() const; T getData(int value); private: //Declare generic vector vector elements; };
//Generic function definition template bool Stack::empty() const { return elements.empty(); }
//Generic function definition template T Stack::peek() const { return elements.back(); }
//Generic function definition template void Stack::push(T value) { elements.push_back(value); }
//Generic function definition template void Stack::pop() { elements.pop_back(); }
//Generic function definition template T Stack::getSize() const { return elements.size() ;
}
//Generic function definition template T Stack::getData(int value) { return elements.at(value); } #endif
//Function main int main() {
//Create the object Stack iVector;
/*Call the empty() and return true if the stack is empty*/ if(iVector.empty()) { cout
cout
cout
cout
cout
cout
Sample output: The stack is empty Push elements 5 8 3 1 2 The top of the stack element: 2 The number of elements in stack: 5 Pop operation is performed After pop operation the elements in stack 5 8 3 1 The number of elements in stack after pop: 4 Press any key to continue . . .
4 tesplatectypenane T class Stack pablic: 8 StackO: 9 bool epty const; 20 Tpeak) const 1 vid push(T aue) 2 Tpopo 3 int getSize) const 24 s private 6 T elesentsti00] 17 Int size 19 20 tesplatectypenase T 21 StackT:StackO 24 25 tesplatectypenane 27 bool Stackel:cemptyD const 28 a0 1 2 templatectypename T 3 T StackeTs:peskO const 34 5 return eleses[size 37 38 teplatectypenane T 39 void Stack1:push(Tva1ue 40 eesentsIsize 1-vaTue 43 44 teeplatestypenane T 45 T Stack-popO 46 7 return elenestst--s1ze] 48 49 D tesplatectypenane T 52 54 t endit return sizeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started