Description 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 1234567-1 76543 9643-2 34692 -3 Number of items left is less than five! Program is terminating!

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 1TF
icon
Related questions
Question

code this in C

Data Structures and Algorithms 1
Course Title
Topics Covered:
Stack
Objectives:
Implement a program using the stack operations.
Description
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
1234567-1
76543
9643-2
34692
-3
Number of items left is less than five! Program is terminating!
Remarks
Project name: Backtrack
Filenames: backtrack.h, backtrack.c, main.c
Transcribed Image Text:Data Structures and Algorithms 1 Course Title Topics Covered: Stack Objectives: Implement a program using the stack operations. Description 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 1234567-1 76543 9643-2 34692 -3 Number of items left is less than five! Program is terminating! Remarks Project name: Backtrack Filenames: backtrack.h, backtrack.c, main.c
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning