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, The program must continue
to accept and process commands until the user types the end command.
The program maintains a linked list. Each node of the list contains an integer value and a
pointer to the next node of the list. Note that the same integer value may appear several times
in the list. At all times, the values in the list must be in non-decreasing order. Some commands
require your program to modify the list while others involve traversing the list to gather information.
Initially, the list is empty. The commands and their meanings are as follows. (You should bear in
mind that different parts of a command are separated by one or more spaces or tabs.)
(a) Insert Command: The syntax for this command is as follows:
ins value
In the above, ins represents the name of the command and value represents an integer value (which
may be positive, negative or zero). In response to this command, your program should create a
new node containing the specified value and insert the node into the list in such a way that the
values in the list continue to be in non-decreasing order even after the insert operation.
(b) Delete Command: The syntax for this command is as follows:
del value
As in the insert command, value represents an integer value (which may be positive, negative or
zero). In response to this command, your program should delete a node containing the specified
value from the list, if the list contains such a node. Otherwise, the program should print a short
message such as “The given value is not in the list”. The same message should be produced even
when the list is empty. Note that if there are two or more nodes that contain the specified value,
the delete command should only delete one of the nodes.
1
(c) Print List Command: The syntax for this command is as follows:
pri
In response to this command, your program should traverse the list and print the value stored in
each node. (Thus, the number of values printed is equal to the number of nodes in the list.) If the
list is empty, the output should be a short message such as “The list is empty”.
(d) Count Command: The syntax for this command is as follows:
cnt
In response to this command, your program should output the number of nodes in the list. (If the
list is empty, the output must be 0.)
(e) Count Distinct Values Command: The syntax for this command is as follows:
cdv
Recall that values may occur multiple times in the list. In response to the cdv command, your
program should compute and output the number of distinct values in the list. (If the list is empty,
the output must be 0.)
Example: To see the difference between the cnt and cdv commands, consider the following list.
head
1 5 5 5 7 7 /
Figure 1: An example of a linked list
For the above list, the cnt command must output the value 6 (since there are six nodes in the list)
while the cdv command must output the value 3 (since there are only three distinct values in the
list, namely 1, 5 and 7).
(f) Count Occurrences Command: The syntax for this command is as follows:
occ value
As in the insert command, value represents an integer value (which may be positive, negative or
zero). In response to this command, your program should print the number of times the specified
value occurs in the list (i.e., the number of nodes that contain the specified value). If the value
does not appear in the list (this also covers the case of an empty list), the answer produced must
be 0. (For example, in the list shown in Figure 1, the value 5 appears three times while the value
−7 appears zero times.)
2
(g) Count Range Command: The syntax for this command is as follows:
ran value1 value2
Here, value1 and value2 represent integer values (which may be positive, negative or zero). Your
program should first check and make sure that the first value is less than or equal to the second
value. If this condition is not satisfied, your program should simply print a message of the form
“Invalid range” and prompt the user for the next command. If the condition is satisfied, then
your program should compute and print the number of nodes in the list which contain values in
the range [value1 .. value2]. Note that the range includes both the end points. If none of the
the values in the range appears in the list (this also covers the case of an empty list), the answer
produced must be 0. (For example, in the list shown in Figure 1, there are six nodes with values
in the range [−1 .. 10], five nodes with values in the range [5 .. 7] and zero nodes with values in the
range [−3 .. 0].)
(h) End Command: The syntax for this command is as follows:
end
In response to this command, your program should stop.
Note: In writing this program, you may assume the following.
(a) The command given by the user will be one of ins, del, pri, cnt, cdv, occ or ran. (The
command names are case sensitive.)
(b) Each command will contain the appropriate number of integer values (if needed).
Thus, the only error that your program needs to detect is in the ran command described above.
Your program should continue to prompt the user and process commands until the user types the
end command.
***********************************************
Code:
Program Outline: An outline for your program is as follows.
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.
(c) Prompt the user for the next command.
(d) Read the next command.
}
3
Suggestions:
1. Write a separate function to implement each command (except end).
2. Use the "%s" format to read the command as a string into a character 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.)
3. Use the strcmp function in the string library (<string.h>) to identify which command is
specified. (