Question
One of the application of stack is to backtrack. As an example , imagine we want to read a list of items and each time
One of the application of stack is to backtrack. 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 discards the negative number. Use stack to solve the problem using python. Read the numbers and push them into 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 items in the stack, print an error message and stop the program. After printing the five items resume reading data and placing them into stack. When the end of the file is detected print a message and the items remaining in the stack. Test your program with the following data: 1 2 3 4 5 -1 10 20 30 -2 40 50 60 70 80(CO3)
Maximum number of characters (including HTML tags added by text editor): 32,000
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