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. Show graphically representation for the following data.c++ 1 2 3 4 5 -1 1 2 3 4 5 6 7 8 9 10 -2 11 12 -3 1 2 3 4 5
PRATICAL QUESTIONS
Answers
Answer:
mark me brainlest
Explanation:
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. Read the numbers 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 a message and the items remaining in the stack, lest your program with the following data:
Step-by-step solution:
Step 1 of 4
Program to backtrack that is pop 5 elements from stack on reading a negative value
Program Plan:
• First declare the structure for node and stack.
• Declare three functions: one to allocate memory for stack element, one to insert element in to stack, one to remove element from stack.
• Read name of file from user which he want to create
• Create that text file fopen() function.
• Read input from user which he want to insert into file until user not entered ctrl+z.
• After inserting data into file close the file.
• Then again open the file in read mode.
• Read each value from file and simultaneously add that value into stack using push() method..
• If negative value read from file than remove five top element from stack using pop() method.
• At the end when all value reads from the file than print the remaining value in stack.