Starting Out with C++ from Control Structures to Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th Edition)
9th Edition
ISBN: 9780134544847
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 19, Problem 8PC
Program Plan Intro
Dynamic MathStack Template
Program Plan:
MathStack.h:
- Include required header files
- Declare a class named “MathStack” which inherits “DynStack” Inside the class,
- Inside “public” access specifier,
- Declare functions “add ()”, “sub ()”, “mult ()”, “div ()”, “addAll ()”, and “multAll ()”.
- Inside “public” access specifier,
- Declare class template.
- Give function definition to add elements “add ()”.
- Declare required template variables “num_Value”, and “sum_Value”.
- Call the function “pop ()”
- Add the elements.
- Push the value into the stack using the function “push ()”.
- Declare class template.
- Give function definition to subtract elements “sub ()”.
- Declare required template variables “num_Value”, and “diff_Value”.
- Call the function “pop ()”
- Subtract the elements.
- Push the value into the stack using the function “push ()”.
- Declare class template.
- Give function definition to multiply elements “mult ()”.
- Declare required template variables “num_Value”, and “prod_Value”.
- Call the function “pop ()”
- Multiply the elements.
- Push the value into the stack using the function “push ()”.
- Declare class template.
- Give function definition to divide elements “div ()”.
- Declare required template variables “num_Value”, and “quo_Value”.
- Call the function “pop ()”
- Divide the elements.
- Push the value into the stack using the function “push ()”.
- Declare class template.
- Give function definition to add all the elements “addAll ()”.
- Declare required template variables “num_Value”, and “sum_Value”.
- Call the function “pop ()”
- Add all the elements.
- Push the value into the stack using the function “push ()”.
- Declare class template.
- Give function definition to multiply all the elements “multAll ()”.
- Declare required template variables “num_Value”, and “prod_Value”.
- Call the function “pop ()”
- Multiply all the elements.
- Push the value into the stack using the function “push ()”.
DynStack.h:
- Include required header files.
- Create a template.
- Declare a class named “DynStack”. Inside the class
- Inside the “protected” access specifier,
- Give structure declaration for the stack
- Create an object for the template
- Create a stack pointer name “next”.
- Create a stack pointer name “top”
- Declare a variable named “stackSize”.
- Give structure declaration for the stack
- Inside the “public” access specifier,
- Give a declaration for a constructor.
- Assign null to the top node.
- Give function declaration for “push ()”, “pop ()”,and “isEmpty ()”.
- Give a declaration for a constructor.
- Inside the “protected” access specifier,
- Give the class template.
- Give function definition for “push ()”.
- Assign null to the new node.
- Dynamically allocate memory for new node
- Assign “num” to the value of new node.
- Check if the stack is empty using the function “isEmpty ()”
- If the condition is true then assign new node as the top and make the next node as null.
- If the condition is not true then, assign top node to the next of new node and assign new node as the top.
- Give the class template.
- Give function definition for “pop ()”.
- Assign null to the temp node.
- Check if the stack is empty using the function “is_Empty ()”
- If the condition is true then print “The stack is empty”.
- If the condition is not true then,
- Assign top value to the variable “num”.
- Link top of next node to temp node.
- Delete the top node and make temp as the top node.
- Give the class template.
- Give function definition for “isEmpty ()”.
- Assign false to a Boolean variable
- Check if the top node is null
- Assign true to “status”.
- Return the status
Main.cpp:
- Include required header files.
- Inside “main ()” function,
- Declare constant variables “STACKSIZE”, “ADDSIZE”, and “MULTSIZE”.
- Create three stacks “stack”, “addAllStack”, and “multAllStack”.
- Declare two variables “popVar” and “ipopVar”.
- Push two elements to perform add operation.
- Call the function “add ()”.
- Display the element.
- Push two elements to perform multiplication operation.
- Call the function “mult ()”.
- Display the element.
- Push two elements to perform division operation.
- Call the function “div ()”.
- Display the element.
- Push two elements to perform subtraction operation.
- Call the function “sub ()”.
- Display the element.
- Push four elements to perform addAll operation.
- Call the function “addAll ()”.
- Display the element.
- Push six elements to perform multAll operation.
- Call the function “multAll ()”.
- Display the element.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
C++ Question
You need to write a class called LinkedList that implements the following List operations:
public void add(int index, Object item);
// adds an item to the list at the given index, that index may be at start, end or after or before the
// specific element
2.public void remove(int index);
// removes the item from the list that has the given index
3.public void remove(Object item);
// finds the item from list and removes that item from the list
4.public List duplicate();
// creates a duplicate of the list
// postcondition: returns a copy of the linked list
5.public List duplicateReversed();
// creates a duplicate of the list with the nodes in reverse order
// postcondition: returns a copy of the linked list with the nodes in
6.public List ReverseDisplay();
//print list in reverse order
7.public Delete_Smallest();
// Delete smallest element from linked list
8.public List Delete_duplicate();
// Delete duplicate elements from a given linked list.Retain the…
C++ Question
You need to write a class called LinkedList that implements the following List operations:
public void add(int index, Object item);
// adds an item to the list at the given index, that index may be at start, end or after or before the
// specific element
2.public void remove(int index);
// removes the item from the list that has the given index
3.public void remove(Object item);
// finds the item from list and removes that item from the list
4.public List duplicate();
// creates a duplicate of the list
// postcondition: returns a copy of the linked list
5.public List duplicateReversed();
// creates a duplicate of the list with the nodes in reverse order
// postcondition: returns a copy of the linked list with the nodes in
6.public List ReverseDisplay();
//print list in reverse order
7.public Delete_Smallest();
// Delete smallest element from linked list
8.public List Delete_duplicate();
// Delete duplicate elements from a given linked list.Retain the…
data structures in java
Chapter 19 Solutions
Starting Out with C++ from Control Structures to Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th Edition)
Ch. 19.1 - Describe what LIFO means.Ch. 19.1 - What is the difference between static and dynamic...Ch. 19.1 - What are the two primary stack operations?...Ch. 19.1 - What STL types does the STL stack container adapt?Ch. 19 - Prob. 1RQECh. 19 - Prob. 2RQECh. 19 - What is the difference between a static stack and...Ch. 19 - Prob. 4RQECh. 19 - Prob. 5RQECh. 19 - The STL stack is considered a container adapter....
Ch. 19 - What types may the STL stack be based on? By...Ch. 19 - Prob. 8RQECh. 19 - Prob. 9RQECh. 19 - Prob. 10RQECh. 19 - Prob. 11RQECh. 19 - Prob. 12RQECh. 19 - Prob. 13RQECh. 19 - Prob. 14RQECh. 19 - Prob. 15RQECh. 19 - Prob. 16RQECh. 19 - The STL stack container is an adapter for the...Ch. 19 - Prob. 18RQECh. 19 - Prob. 19RQECh. 19 - Prob. 20RQECh. 19 - Prob. 21RQECh. 19 - Prob. 22RQECh. 19 - Prob. 23RQECh. 19 - Prob. 24RQECh. 19 - Prob. 25RQECh. 19 - Prob. 26RQECh. 19 - Write two different code segments that may be used...Ch. 19 - Prob. 28RQECh. 19 - Prob. 29RQECh. 19 - Prob. 30RQECh. 19 - Prob. 31RQECh. 19 - Prob. 32RQECh. 19 - Prob. 1PCCh. 19 - Prob. 2PCCh. 19 - Prob. 3PCCh. 19 - Prob. 4PCCh. 19 - Prob. 5PCCh. 19 - Dynamic String Stack Design a class that stores...Ch. 19 - Prob. 7PCCh. 19 - Prob. 8PCCh. 19 - Prob. 9PCCh. 19 - Prob. 10PCCh. 19 - Prob. 11PCCh. 19 - Inventory Bin Stack Design an inventory class that...Ch. 19 - Prob. 13PCCh. 19 - Prob. 14PCCh. 19 - Prob. 15PC
Knowledge Booster
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.Similar questions
- C++ ProgrammingActivity: Queue Linked List Explain the flow of the code not necessarily every line, as long as you explain what the important parts of the code do. The code is already correct, just explain the flow. #include "queue.h" #include "linkedlist.h" class SLLQueue : public Queue { LinkedList* list; public: SLLQueue() { list = new LinkedList(); } void enqueue(int e) { list->addTail(e); return; } int dequeue() { int elem; elem = list->removeHead(); return elem; } int first() { int elem; elem = list->get(1); return elem;; } int size() { return list->size(); } bool isEmpty() { return list->isEmpty(); } int collect(int max) { int sum = 0; while(first() != 0) { if(sum + first() <= max) { sum += first(); dequeue(); } else {…arrow_forwardComputer Science //iterator() creates a new Iterator over this list. It will//initially be referring to the first value in the list, unless the//list is empty, in which case it will be considered both "past start"//and "past end". template <typename ValueType>typename DoublyLinkedList<ValueType>::Iterator DoublyLinkedList<ValueType>::iterator(){//return iterator(head);} //constIterator() creates a new ConstIterator over this list. It will//initially be referring to the first value in the list, unless the//list is empty, in which case it will be considered both "past start"//and "past end". template <typename ValueType>typename DoublyLinkedList<ValueType>::ConstIterator DoublyLinkedList<ValueType>::constIterator() const{//return constIterator(head);} //Initializes a newly-constructed IteratorBase to operate on//the given list. It will initially be referring to the first//value in the list, unless the list is empty, in which case//it will be…arrow_forwardJava/Data Structures: The public ArrayList() constructor in the Java Class Library ArrayList creates an empty list with an initial capacity of ____. Multiple choice. A) 0 B) 1 C) 10 D) 100arrow_forward
- JAVA programming language DescriptionYour job is to write your own array list (growable array) that stores elements that are objects of a parentclass or any of its child classes but does not allow any other type of objects to be stored. You will createa general data class and two specific data classes that inherit from that class. You will then write thearray list to hold any objects of the general data class. Finally, you will test your array list using JUnittests to prove that it does what it should.DetailsYour array list must be named DataList. Start with an array with room for 10 elements. Keep all elementscontiguous (no blank places) at the low end of the indices. When the array is full, grow it by doubling thesize. (Do not use any built-in methods to copy the array elements. Write your own code to do this.) Youdo not have to shrink the array when elements are removed. The array list must have the followingpublic methods (listed in UML style where GenClass is your general data…arrow_forwardC++ program LinkList: namespace { template<typename T> class List { public: virtual ~List(){}; virtual bool addFirst(T) = 0; virtual bool addLast(T) = 0; virtual T& get(int) = 0; virtual bool insert(int, T) = 0; virtual T remove(int) = 0; virtual T removeFirst() = 0; virtual T removeLast() = 0; virtual void set(int, T) = 0; virtual int size() const = 0; }; } #endif //PROG1_LIST_Harrow_forward6. Suppose that we have defined a singly linked list class that contains a list of unique integers in ascending order. Create a method that merges the integers into a new list. Note the additional requirements listed below. Notes: ● . Neither this list nor other list should change. The input lists will contain id's in sorted order. However, they may contain duplicate values. For example, other list might contain id's . You should not create duplicate id's in the list. Important: this list may contain duplicate id's, and other list may also contain duplicate id's. You must ensure that the resulting list does not contain duplicates, even if the input lists do contain duplicates.arrow_forward
- C++ ProgrammingActivity: Linked List Stack and BracketsExplain the flow of the code not necessarily every line, as long as you explain what the important parts of the code do. The code is already correct, just explain the flow #include "stack.h" #include "linkedlist.h" // SLLStack means Singly Linked List (SLL) Stack class SLLStack : public Stack { LinkedList* list; public: SLLStack() { list = new LinkedList(); } void push(char e) { list->add(e); return; } char pop() { char elem; elem = list->removeTail(); return elem; } char top() { char elem; elem = list->get(size()); return elem; } int size() { return list->size(); } bool isEmpty() { return list->isEmpty(); } };arrow_forwardLab 3 Directions (linked lists) Program #1 1. Show PolynomialADT interface 2. Create the PolyNodeClass with the following methods: default constructor, overloaded constructor, copy constructor, setCoefficient, setExponent, setNext, getCoefficient, get Exponent, getNext 3. Create the PolynomialDataStrucClass with the following methods: default constructor, overloaded constructor, copy constructor, isEmpty, setFirstNode, getFirstNode, addPolyNodeFirst (PolyNode is created and set to beginning of polynomial), addPolyNodeLast, addPolyNode (PolyNode is set to the end of polynomial), addPolynomials, toString 4. Create the Polynomial DemoClass: instantiate and initialize Polynomial DataStrucClass objects p1, p2, p3, p4 1 - Add terms to the polynomials (pass 2 arguments to the method: coefficient and exponent- for example: p1.addPolyNodeLast(4, 3);) Print out p1, p2 and sum of the polynomials AND p3, p4, and sum of the polynomials Use: p1= 4x^3 + 3x^2 - 5; p2 = 3x^5 + 4x^4 + x^3 - 4x^2 + 4x^1…arrow_forwardclass Node: def __init__(self, e, n): self.element = e self.next = n class LinkedList: def __init__(self, a): # Design the constructor based on data type of a. If 'a' is built in python list then # Creates a linked list using the values from the given array. head will refer # to the Node that contains the element from a[0] # Else Sets the value of head. head will refer # to the given LinkedList # Hint: Use the type() function to determine the data type of a self.head = None # To Do # Count the number of nodes in the list def countNode(self): # To Do # Print elements in the list def printList(self): # To Do # returns the reference of the Node at the given index. For invalid index return None. def nodeAt(self, idx): # To Doarrow_forward
- Course: Data Structure and Algorithms Language: C++ Question is well explained Question #2Implement a class for Circular Doubly Linked List (with a dummy header node) which stores integers in unsorted order. Your class definitions should look like as shown below: class CDLinkedList;class DNode {friend class CDLinkedList;private int data;private DNode next;private DNode prev;};class CDLinkedList {private:DNode head; // Dummy header nodepublic CDLinkedList(); // Default constructorpublic bool insert (int val); public bool removeSecondLastValue (); public void findMiddleValue(); public void display(); };arrow_forwardC++arrow_forwardC++ ProgrammingActivity: Queue Linked List Explain the flow of the code not necessarily every line, as long as you explain what the important parts of the code do. The code is already correct, just explain the flow. SEE ATTACHED PHOTO FOR THE PROBLEM #include "queue.h" #include "linkedlist.h" class SLLQueue : public Queue { LinkedList* list; public: SLLQueue() { list = new LinkedList(); } void enqueue(int e) { list->addTail(e); return; } int dequeue() { int elem; elem = list->removeHead(); return elem; } int first() { int elem; elem = list->get(1); return elem;; } int size() { return list->size(); } bool isEmpty() { return list->isEmpty(); } int collect(int max) { int sum = 0; while(first() != 0) { if(sum + first() <= max) { sum += first();…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education