Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CODE GIVEN: #include using namespace std; //------------------------ class node -------------------------- class node { friend class stack; private: int key = 0; node * next =
CODE GIVEN:
#includeusing namespace std; //------------------------ class node -------------------------- class node { friend class stack; private: int key = 0; node * next = NULL; }; //----------------------- class stack --------------------------- class stack { public: void print(); void push(int newkey); int pop(); private: node * top = NULL; }; //----------------------- stack::print --------------------------- void stack::print() { cout "; cin >> opt; switch (opt) { case 1: cout > key; s.push(key); s.print(); break; case 2: key = s.pop(); cout CS 215- Fall 2017 Lab 11 Learning Objectives: implementation of a dynamic Stack class using Dynamic allocation and deallocation of nodes Push: inserting a new node on the top of a stack - -Pop: removing the top node of a stack -Print: printing the current contents of a stack General Description: A Dynamic Stack is a dynamic data structure that implements a "Last In- First Out" order of adding and removing data nodes. It is defined as: Node- consists of a key (plus other data when needed) and a next pointer (same as a Linked List) Stack-consists of a top pointer that points to the top (first) node in the stack, or NULL when the stack is empty. Each node points to the next node in the stack, with the "bottom" (last) node having NULL for its next pointer (same as a Linked List). The print0) method is identical to the print() for a linked list, except the pointer is called top instead of head. The push0 method is given a key for a new node, allocates and populates the new node, and places it on top of the stack, making the new node the new top. The pop) method removes the top node, deallocates it, and returns its key. Exception: when the stack is empty (top is NULL), it returns -1 indicating "stack is empty, there is nothing to return" Specifications Start a new project, add a source file (.cpp), and copy/paste the starting code provided on the course website All of the project is complete, except for the coding of the print(), pop() and push() methods. Complete the push() and pop() by writing one line of code for each comment as instructed in the comments. Look at the code for print) for Linked Lists in the course notes to complete the print() method. It should print "Stack Print: Empty" when the stack is empty, or "Stack Print:"followed by the keys of all nodes in the stack, top-to-bottom printed left-to-right. See diagrams on page 2 See sample execution on page 3
Step 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