Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following must be written in C. Please comment each function You are required to write an interactive C program that prompts the user for

The following must be written in C. Please comment each function

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) 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.

(b) 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.

(c) 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.

(d) 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.

(e) 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.

(f) 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.

(a) 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.

(b) 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.

(a) 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.

(b) 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.)

F. 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.

(a) The command given by the user will be one of ina, inb, del, rep, prn, or end. (The command names are case sensitive.)

(b) 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.

(c) 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 "end":

a.Read the value(s) for the command, if necessary.

b.Process the command. (If ina or iinb 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.)

Suggestions:

(a)Use the "%s" format to read the command as a string into a char array of size 4. (Since each command is exactly three characters long and each string must be properly terminated using the '\0' character, the size of the character array must be 4.)

(b)Use the "%d" format to read the integer number specified as argument to the commands

(c)Use the "%s" format to read the string specified as an argument to the commands

(d)Use the strcmp function in the string library () to identify which command is specified..

(e)Use fflush(stdout) after each call to printf.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

8. Describe the steps in the development planning process.

Answered: 1 week ago