Starting Out with C++: Early Objects (9th Edition)
9th Edition
ISBN: 9780134400242
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 18, Problem 13RQE
Program Plan Intro
Stack:
A stack is type of container. It performs “Last In First Out”.
- In stack, the item which is inserted at last will be retrieved first.
- A stack can perform two operations. They are:
- Push – Inserting an element inside a stack.
- When the first element is pushed into the stack, the element will be at the “top” the stack. When the second element is added, the first element is pushed down and the second element will be at the top position, like this it goes on until the element which pushed at last will be at the top of the stack.
- Pop – Deleting an element from the stack.
- The element which is inserted at last will be deleted first.
- Push – Inserting an element inside a stack.
- The elements can be inserted and retrieved at any one end of the stack.
Example: Cookies in a container.
The baker arranges the cookies one by one in a container in which the lastly inserted cookie is taken out first (pop operation) from the container.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Suppose the following operations were performed on an empty stack:pus h(8);pus h(?);pop();push (19);pus h(21);pop();Insert numbers in the following diagram to show what will be stored in the staticstack after the operations have executed
TRUE or FALSE? Please answer the following question and state the reason why: Thank you!
In a Dynamic Stack, the pointer top stays at the head after a push operation.
During a Pop operation in Static Stack, the elements are being moved one step up.
In a dynamic implementation of stack, the pointer top has an initial value of null.
STL empty function will yield a value of true if the stack has elements.
TRUE or FALSE? Answer the following question and state the reason why:
In a dynamic stack, the node that was popped is deleted.
In a dynamic stack, the pointer top stays at the head after push operation.
STL function top returns a reference to element at the top of the stack.
STL empty function will yield a value of true if the stack has elements.
Chapter 18 Solutions
Starting Out with C++: Early Objects (9th Edition)
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
- 4.3 Refer to the code fragment below and convert the following Queue structure into a Stack structure. You should create a reference called S1 for the stack structure. Code: Queue Q1 = new LinkedList (); Q1.add("Sandra"); Q1.add (15); Q1.add (200); Q1.add ('#'); Q1. remove (); System.out.println(Q1); System.out.println(Q1.peek()); Q1.add("Mary"); System.out.println(Q1);arrow_forward1. a) Using the stack diagram, convert the following infix expression to a postfix expression. A* (B – C + D) – E / F b) Referring to question 1 (a), give the value of postfix expression by using the stack concept if provided: A=2, B=3, C=8, D=10, E=10 and F=2arrow_forwardTRUE or FALSE? Answer the following question and state the reason why: A circular linked list has 2 node pointers. cout<<list.back()<<endl; = The back member function returns a reference to the last element in the list. In a Dynamic Stack, the pointer top stays at the head after a push operation. During a Pop operation in Static Stack, the elements are being moved one step up. In a dynamic implementation of stack, the pointer top has an initial value of null.arrow_forward
- TRUE or FALSE? Answer the following question and state the reason why: cout<<list.back()<<endl; = The back member function returns a reference to the last element in the list. In a Dynamic Stack, the pointer top stays at the head after a push operation. During a Pop operation in Static Stack, the elements are being moved one step up. In a dynamic implementation of stack, the pointer top has an initial value of null.arrow_forwardCode in C One of the applications of a stack is to backtrack - that is, to retrace its steps. As an example, imagine we want to read a list of items, and each time we read a negative number we must backtrack and print the five numbers that come before the negative number and then discard the negative number. Use a stack to solve this problem. Use a stack and push them into the stack (without printing them) until a negative number is read. At this time, stop reading and pop five items from the stack and print them. If there are fewer than five items in the stack, print an error message and stop the program. After printing the five items, resume reading data and placing them in the stack. When the end of the file is detected, print the message and the items remaining in the stack. Sample Output 1 2 3 4 5 6 7 -1 7 6 5 4 3 9 6 4 3 -2 3 4 6 9 2 -3 Number of items left is less than five! Program is terminating! Project name : Backtrack Filenames: backtrack.h, backtrack.c,…arrow_forwardMachine organization The following operations are performed on a stack: PUSH A, PUSH B, POP, PUSH C, POP,POP,PUSH D, PUSH E, POP, PUSH F. What does the stack contain after each operation?arrow_forward
- Project Overview: This project is for testing the use and understanding of stacks. In this assignment, you will be writing a program that reads in a stream of text and tests for mismatched delimiters. First, you will create a stack class that stores, in each node, a character (char), a line number (int) and a character count (int). This can either be based on a dynamic stack or a static stack from the book, modified according to these requirements. I suggest using a stack of structs that have the char, line number and character count as members, but you can do this separately if you wish.Second, your program should start reading in lines of text from the user. This reading in of lines of text (using getline) should only end when it receives the line “DONE”.While the text is being read, you will use this stack to test for matching “blocks”. That is, the text coming in will have the delimiters to bracket information into blocks, the braces {}, parentheses (), and brackets [ ]. A string…arrow_forwardIn the following code, assume myStack is a stack that holds integers. What would be the contents of the stack after the following operations, indicating what element is currently at the top and the bottom of the stack. myStack.push(1);myStack.push(2);myStack.push(3);myStack.pop();myStack.push(4);myStack.push(5);myStack.pop(); Group of answer choices 4 <- Top21 <- Bottom 5 <- Top31 <- Bottom 4 <- Top32 <- Bottom 5 <- Top21 <- Bottomarrow_forwardQ: Write the code in c++ of Stack By Link List and Perform MultipleTask which are given below. 1. Push value in stack2. Check that Stack is Empty or Not.3. POP values From Stack.4. Search any values from stack.5. Update any value from stack.arrow_forward
- Write a structure to represent a Node in a singly linked-list-based stack similar to the onediscussed in class. Your stack will store integer values. Your program will read a series of integers(until the user enters a non-integer) and store them in a stack, then print them in the reverse order;You will write three functions to assist with this task: push, printStack, and deleteStack.The function push takes a number and a Node and adds the number to the head (top) of thestack by creating a new Node (using malloc) and returning a pointer to this Node. This new nodeis now the top of your stack.The function printStack will need to iterate through your stack and print each one (5 spacesper number, left-aligned), with a new line following the nal number. You will need a pointer thatyou update to the next Node each time you print one. Since you add new items to the top of thestack, the newest elements will be printed rst, giving the reverse-order behavior requested.The function deleteStack…arrow_forwardConsider the following stack, which can be written as [70, 10, 50, 80, 20] i.e. the "top" of the stack is the first element in the list Consider the following code (written as pseudocode): myfunction( stack1 ) stack2 = createStack() while stack1 is NOT empty value = top(stack1) pop(stack1) while stack2 is NOT empty and top(stack2) > value push(stack1, top(stack2)) pop(stack2) push(stack2, value) print(stack2) // Prints the current stack as [element_1, element_2, ..., element_n] return stack2 From the answers below, select the correct output for the first 4 print statements if myFunction is executed with: myFunction( [ 70, 10, 50, 80, 20] )arrow_forward2. a) Using the stack diagram, convert the following infix expression to a postfix expression. A*(B + C* (D - E) ) / F b) Referring to question 2 (a), give the value of postfix expression by using the stack concept if provided: A=4, B=3, C=6, D=10, E=2 and F=4arrow_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