Question: You are required to write an interactive C program that prompts the user for commands, accepts commands from the keyboard (stdin) and executes those commands.
You are required to write an interactive C program that prompts the user for commands, accepts commands from the keyboard (stdin) and executes those commands. When a command requires output, it must be written to stdout. The program must continue to accept and process commands until the user types the end command.
The program deals with linked lists. Each node of such a list contains a string of length at most 255, a positive integer (i.e., an integer value 1) and a pointer to the next node of the list. For any node, the string and the integer stored in that node will be referred to as the text and the index for that node respectively. Initially, the list is empty. At all times, the existing list must satisfy the following requirements:
1.The index is a number of the node in the list, i.e. the first node has index 1, and when the list is scanned from the beginning to the end, the value of indexes is increasing by 1.
2.The texts appearing in the list are all distinct; that is, no two nodes have the same text.
The commands and their interpretations are as follows. (You should bear in mind that different parts of a command are separated by one or more spaces.)
A.Command Insert After: The syntax for this command is as follows:
ina num str
Here, ina represents the name of the command, num represents a positive integer number, and str represents a text. The interpretation of this command is as follows.
A new node with the text specified in the command must be inserted in the list after a node whose index is equal to the number specified in the command, indexes of the list should be changed to keep increasing order, and the following message must be printed Ok.
If the list contains a node whose text is identical to the text specified in the command, then no new node must be created and the following message must be printed Such text exists already.
If the list does not contain a node whose index is equal to the number specified in the command, then a new node must be inserted at the end of the list and the following message must be printed Text inserted at the end.
B.Command Insert Before: The syntax for this command is as follows:
inb num str
Here, inb represents the name of the command, num represents a positive integer number, and str represents a text. The interpretation of this command is as follows.
A new node with the text specified in the command must be inserted the list before a node whose index is equal to the number specified in the command, indexes of the list should be changed to keep increasing order, and the following message must be printed Ok.
If the list contains a node whose text is identical to the text specified in the command, then no new node must be created and the following message must be printed Such text exists already.
If the list does not contain a node whose index is equal to the number specified in the command, then a new node must be inserted at the beginning of the list and the following message must be printed Text inserted at the beginning.
C.Command Delete: The syntax for this command is as follows:
del num
Here, del represents the name of the command and num represents a positive integer number. The interpretation of this command is as follows.
If the list contains a node whose index is equal to the number specified in the command, then the node must be removed from the list, indexes of the list should be changed to keep increasing order, and the following message must be printed Deleted.
If the list does not contain a node whose index is equal to the number specified in the command, then the program must leave the list unchanged and the following message must be printed No such index.
D.Command Replace: The syntax for this command is as follows:
rep num str
Here, num represents a positive integer number, and str represents a text. The interpretation of this command is as follows.
If the list contains a node whose index is equal to the number specified in the command, then the node text must be replaced with the text specified in the command, and the following message must be printed Replaced.
If the list does not contain a node whose index is equal to the number specified in the command, then the program must leave the list unchanged, and the following message must be printed No such index.
E.Print List Command: The syntax for this command is as follows:
prn
Here, prn represents the name of the command. If the list is empty, your program should print the message The list is empty. Otherwise, your program should traverse the list (from left to right) and print each index and the corresponding text on a line by itself. (Thus, when the list is non-empty, the number of lines printed is the number of nodes in the list.)
A.End Command: The syntax for this command is as follows:
end
In response to this command, your program must stop.
Assumptions: In writing this program, you may assume the following.
The command given by the user will be one of ina, inb, del, rep, prn, or end. (The command names are case sensitive.)
Each command will contain all and only the necessary arguments. (Thus, commands wont have missing or extraneous arguments.) Further, when a command has one or more arguments, the command name and the successive arguments will be separated by one or more spaces.
Each string specified in a command wont include any whitespace characters.
Thus, there is no need to deal with any erroneous commands; if such command is entered it should be ignored. Your program should continue to prompt the user and process commands until the user types the end command.
Program Outline:
1.Prompt the user for a command.
2.Read the command.
3.While command is not "":
a.Read the value(s) for the command, if necessary.
b.Process the command. (If or entered when the list is empty, the first node will be created.)
c.Prompt the user for the next command.
d.Read the next command
Structural Requirements:
In addition to main, you must have a separate function to implement each of the commands described above. (You may have other functions in addition to these.)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
