Page 1 of 1

i have attached all the pages in correct format . all the 3 pages are for a single same question.

Posted: Fri May 20, 2022 11:44 am
by answerhappygod
i have attached all the pages in correct format . all the 3 pages are for a single same question.
I Have Attached All The Pages In Correct Format All The 3 Pages Are For A Single Same Question 1
I Have Attached All The Pages In Correct Format All The 3 Pages Are For A Single Same Question 1 (52.4 KiB) Viewed 55 times
I Have Attached All The Pages In Correct Format All The 3 Pages Are For A Single Same Question 2
I Have Attached All The Pages In Correct Format All The 3 Pages Are For A Single Same Question 2 (38.25 KiB) Viewed 55 times
I Have Attached All The Pages In Correct Format All The 3 Pages Are For A Single Same Question 3
I Have Attached All The Pages In Correct Format All The 3 Pages Are For A Single Same Question 3 (42.51 KiB) Viewed 55 times
Problem: Make a dynamically allocated stack data structure, with a variable number of nodes. Each node should be dynamically allocated memory before pushing into the stack. It should support the following operations: 1. Push: Insert the given element at the top of the stack. You must dynamically allocate the memory for the node before pushing it. The user will input 'P' followed by the integer to be pushed. 2. Pop: Pop (delete) the top element from the stack and return it. You must delink the node, and free up the allocated memory as well. The user will enter 'p' to request a pop. If the stack is empty print "stack underflow". 3. (Bonus) Balanced parentheses: The user will enter 'b', followed by a string consisting of and/or "y. You must apply the concept of stacks and find out if the entered string has balanced parentheses or not. Print "yes" if it is balanced, "no" otherwise Balanced parentheses have the same number of opening and closing 'parentheses. Note that this operation must not disturb the previous stack, but must be solved using a new instance (object) of the structure/class created. The user will enter "x to exit the program. You must free the head before the program ends. Sample Test Case: Input: P5 P3 P 7 р р р P6 р p X Output: Och 3 5 6 stack underflow (Explanation on next page)

Explanation: The user pushes 5, 3 and 7, after which the stack looks like: 7 3 5 Then the user pops three times, emptying the stack and the program prints 7, 3 and 5 in the same order. Again, the user pushes 6, after which the stack has a single element, 6, followed by a pop which pops off the 6. printing it to the console. At last, even though the stack is empty, the user again requests a pop, to which the program prints "stack underflow" since it is empty and there are no elements to pop. The user enters 'X' to exit. Sample Test Case (with bonus part): Input: P 5 P 3 P7 P6 р b (0 р р b (00(0) р р х Output 6 no 7 3 yes 5 stack underflow (Explanation on next page) aax

Explanation: In the first four lines of input, the user pushes elements 5, 3, 7 and 6 onto the stack, Such that it look like: 6 Now a pop is requested, and popping the topmost element (printing 6), the stack now looks like: 7 Now the balanced parentheses option is picked, and the string "O" is entered. Here, the number of opening parentheses is one more than the number of closing parentheses, meaning it is unbalanced. Hence, the program outputs "no". The user requests pop twice, hence 7, then 3 is printed, and the stack now contains a single element, 5. Balanced parentheses function is requested and the string "O))" is entered. Since the number of opening parentheses is the same as the number of closing parentheses, it is balanced, and the program prints "yes". At last, the user requests two pops, to which the program prints 5 for the first and "stack underflow for the second the stack is emptied after 5 is popped. The user enters 'X' to exit.