Code 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, main.c
Code 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
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, main.c
Trending now
This is a popular solution!
Step by step
Solved in 2 steps