STARTING OUT WITH C++ MPL
9th Edition
ISBN: 9780136673989
Author: GADDIS
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 18, Problem 2PC
Program Plan Intro
Dynamic Stack Template
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 ()”.
DynStack.h:
- Include required header files.
- Create a template.
- Declare a class named “DynStack”. 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”
- 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 “private” 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 most node and make temp as the top node.
- Give function definition for “isEmpty ()”.
- Assign Boolean value to the variable
- Check if the top node is null
- Assign true to “status”.
- Return the status
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Stack Implementation in C++make code for an application that uses the StackX class to create a stack.includes a brief main() code to test this class.
C++ code
Stack Implementation using Variable-sized Dynamic Arrays2. Stack Implementation using Linked Lists3. The applications of Stacks1. Implement a template-based stack using a variable-sized dynamic array. When the array getsfull, double its size before insertion of new elements. When array contains data lesser than33% of its size, reduce its size to half. The required member methods are:T* arr: array containing data elements of stackint capacity: stores the capacity of stack.int getCout(): returns total elements stored in the stack.bool isEmpty(): returns true if the stack is empty else false.bool isFull(): returns true if the stack is full.bool top(): returns, but does not delete, the topmost element from the stack via theparameter passed by reference, and returns true via the return statement. If there is noelement, it returns false via the return statement.bool pop(): deletes the top most element from the stack and returns true via the returnstatement. If there is no element,…
In c++
Also add comments explaining each line
Chapter 18 Solutions
STARTING OUT WITH C++ MPL
Ch. 18.3 - Describe what LIFO means.Ch. 18.3 - What is the difference between static and dynamic...Ch. 18.3 - What are the two primary stack operations?...Ch. 18.3 - What STL types does the STL stack container adapt?Ch. 18 - Prob. 1RQECh. 18 - Prob. 2RQECh. 18 - What is the difference between a static stack and...Ch. 18 - Prob. 4RQECh. 18 - The STL stack is considered a container adapter....Ch. 18 - What types may the STL stack be based on? By...
Ch. 18 - Prob. 7RQECh. 18 - Prob. 8RQECh. 18 - Prob. 9RQECh. 18 - Prob. 10RQECh. 18 - Prob. 11RQECh. 18 - Prob. 12RQECh. 18 - Prob. 13RQECh. 18 - Prob. 14RQECh. 18 - Prob. 15RQECh. 18 - Prob. 16RQECh. 18 - Prob. 17RQECh. 18 - Prob. 18RQECh. 18 - Prob. 1PCCh. 18 - Prob. 2PCCh. 18 - Prob. 3PCCh. 18 - Prob. 4PCCh. 18 - Prob. 5PCCh. 18 - Prob. 6PCCh. 18 - Prob. 7PCCh. 18 - Prob. 8PCCh. 18 - Prob. 9PCCh. 18 - Prob. 10PCCh. 18 - Prob. 11PCCh. 18 - Prob. 12PCCh. 18 - Prob. 13PCCh. 18 - Prob. 14PCCh. 18 - Prob. 15PC
Knowledge Booster
Similar questions
- Computer science JAVA programming languarrow_forward#include <stdio.h>#include <stdlib.h>#include <string.h> typedef struct LINKED_STACK_NODE_s *LINKED_STACK_NODE; typedef struct LINKED_STACK_NODE_s{LINKED_STACK_NODE next;void *data;} LINKED_STACK_NODE_t[1]; typedef struct LINKED_STACK_s{LINKED_STACK_NODE head;int count;} LINKED_STACK_t[1], *LINKED_STACK; typedef struct{int R;int C;} POS_t[1], *POS; LINKED_STACK stack_init();void stack_free(LINKED_STACK stack);void stack_push(LINKED_STACK stack, void *data);void *stack_pop(LINKED_STACK stack);void *stack_top(LINKED_STACK stack);int is_empty(LINKED_STACK stack); int is_empty(LINKED_STACK stack){return stack->head == NULL;} LINKED_STACK stack_init(){LINKED_STACK stack = (LINKED_STACK)malloc(sizeof(LINKED_STACK_t));if (stack == NULL){printf("\nproblem with initializing stack\n\n");return NULL;}stack->head = NULL;stack->count = 0;return stack;} void stack_free(LINKED_STACK stack){while (is_empty(stack) == 0){stack_pop(stack);}free(stack);}void…arrow_forward#include <stdio.h>#include <stdlib.h>#include <string.h> typedef struct LINKED_STACK_NODE_s *LINKED_STACK_NODE; typedef struct LINKED_STACK_NODE_s{LINKED_STACK_NODE next;void *data;} LINKED_STACK_NODE_t[1]; typedef struct LINKED_STACK_s{LINKED_STACK_NODE head;int count;} LINKED_STACK_t[1], *LINKED_STACK; typedef struct{int R;int C;} POS_t[1], *POS; LINKED_STACK stack_init();void stack_free(LINKED_STACK stack);void stack_push(LINKED_STACK stack, void *data);void *stack_pop(LINKED_STACK stack);void *stack_top(LINKED_STACK stack);int is_empty(LINKED_STACK stack); int is_empty(LINKED_STACK stack){return stack->head == NULL;} LINKED_STACK stack_init(){LINKED_STACK stack = (LINKED_STACK)malloc(sizeof(LINKED_STACK_t));if (stack == NULL){printf("\nproblem with initializing stack\n\n");return NULL;}stack->head = NULL;stack->count = 0;return stack;} void stack_free(LINKED_STACK stack){while (is_empty(stack) == 0){stack_pop(stack);}free(stack);}void…arrow_forward
- Give me the answer in one Visual Studio file, not Visual Studio Code - Give me full code near and simple Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in your linked list push_front(value) Insert the value at the front of the linked list pop_front() Remove the node at the front of the linked list If empty, this is a no operation operator << Display the contents of the linked list just like you would print a character string operator [] Treat like an array Return the value stored in that element of the linked list If element doesn’t exist, return…arrow_forwardBriefly describe the stack parameter.arrow_forwardRead this: Complete the code in Visual Studio using C++ Programming Language with 1 file or clear answer!! - Give me the answer in Visual Studio so I can copy*** - Separate the files such as .cpp, .h, or .main if any! Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in your linked list push_front(value) Insert the value at the front of the linked list pop_front() Remove the node at the front of the linked list If empty, this is a no operation operator << Display the contents of the linked list just like you would print a character…arrow_forward
- Language: C++ Note: Write both parts separately. Part a: Complete the above Priority Queue class. Write a driver to test it. Part b: Modify the above class to handle ‘N’ different priority levelsarrow_forwardSolve in C Program Implement a program using stack and queue operationsarrow_forwardC++ Code for a QueueThe program should features a Queue class with insert(), remove(), peek(),isFull(), isEmpty(), and size() member functions.The main() program creates a queue of five cells, inserts four items, removes threeitems, and inserts four more. The sixth insertion invokes the wraparound feature. All the items are then removed and displayed. The output looks like this:40 50 60 70 80arrow_forward
- C++ ProgrammingActivity: Deque 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 "deque.h" #include "linkedlist.h" #include <iostream> using namespace std; class DLLDeque : public Deque { DoublyLinkedList* list; public: DLLDeque() { list = new DoublyLinkedList(); } void addFirst(int e) { list->addAt(e,1); } void addLast(int e) { list->addAt(e,size()+1); } int removeFirst() { return list->removeAt(1); } int removeLast() { return list->removeAt(size()); } int size(){ return list->size(); } bool isEmpty() { return list->isEmpty(); } // OPTIONAL: a helper method to help you debug void print() {…arrow_forwardExplain the process of Linking with Static Libraries ?arrow_forwardC++ Consider the following function as a property of a LinkedBag that contains a Doubly Linked List. Assume a Node has pointers prev and next, which can be read and changed with the standard get and set methods. Assume that the doubly linked list is: 1 <--> 2 <--> 3 <--> 4 <--> 5 <-->6 If you are uncertain what the above diagram depicts, it is a doubly linked list such that: The head of this doubly linked list is the node that contains the value 1. The tail of this doubly linked list is the node that contains the value 6. The 3rd node in this list contains the value 3. The contents of this list are the values 1 through 6 in sequential order. The following questions are regarding the linked list after after the test_function is run. A. The head of the list after the test_function is run contains the value: B. The tail of the list after the test_function is run contains the value: C. The 3rd node in the list after the test_function is run…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning