Starting Out with C++ from Control Structures to Objects (8th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
Question
Book Icon
Chapter 18, Problem 5PC
Program Plan Intro

Error Testing

Dynamic Stack:

Program Plan:

Main.cpp:

  • Include required header files.
  • Inside the “main ()” function,
    • Create an object named “dstack” for stack.
    • Declare a variable named “popElem”.
    • Push 3 elements inside the stack using the function “push ()”.
    • Pop 3 elements from the stack using the function “pop ()”.

DynIntStack.h:

  • Include required header files.
  • Create a template.
  • Declare a class named “DynIntStack”. Inside the class
    • Inside the “private” 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”
    • 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 the class template.
  • Define 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.
  • Define 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 most node and make temp as the top node.
  • Define function definition for “isEmpty ()”.
    • Assign Boolean value to the variable
    • Check if the top node is null
      • Assign true to “status”.
    • Return the status

Blurred answer
Students have asked these similar questions
Stack: Stacks are a type of container with LIFO (Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. Your Stack should not be of the fixed sized. It should be able to grow itself. So using the class made in task 1, make a class named as Stack, having following additional functionalities: bool empty() : Returns whether the Stack is empty or not. Time Complexity should be: O(1) bool full() : Returns whether the Stack is full or not. Time Complexity should be: O(1)int size() : Returns the current size of the Stack. Time Complexity should be: O(1)Type top () : Returns the last element of the Stack. Time Complexity should be: O(1) void push(Type) : Adds the element of type Type at the top of the stack. Time Complexity    should be: O(1) Type pop() : Deletes the top most element of the stack and returns it. Time Complexity    should be: O(1) Write non-parameterized constructor for the above class. Write Copy…
In C++ Please
for c++ thank you Error Testing The DynIntStack and Dyn IntQueue classes shown in this chapter are abstract data types using a dynamic stack and dynamic queue, respectively. The classes do not cur- rently test for memory allocation errors. Modify the classes so they determine whether new nodes cannot be created by handling the bad_alloc exception. here is the extention.h file please use it #ifndef DYNINTQUEUE_H #define DYNINTQUEUE_H   class DynIntQueue { private:    // Structure for the queue nodes    struct QueueNode    {       int value;       // Value in a node       QueueNode *next; // Pointer to the next node    };      QueueNode *front;  // The front of the queue    QueueNode *rear;   // The rear of the queue    int numItems;      // Number of items in the queue public:    // Constructor    DynIntQueue();      // Destructor    ~DynIntQueue();      // Queue operations    void enqueue(int);    void dequeue(int &);    bool isEmpty() const;    bool isFull() const;    void…

Chapter 18 Solutions

Starting Out with C++ from Control Structures to Objects (8th Edition)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning