Please write the code in C++, and give a screenshot of the program. Develop a pr
ID: 3857751 • Letter: P
Question
Please write the code in C++, and give a screenshot of the program.
Develop a program that will maintain an ordered linked list of positive whole numbers. Your program will provide for the following options:
a. Add a number
b. Delete a number
c. Search for a number
d. Display the whole list of numbers
At all times, the program will keep the list ordered (the smallest number first and the largest number last).
At the start of the program, the list will be empty. The user will add new numbers by choosing the "add" option and will supply the number to be included in the list. The program will add the number to the list at the appropriate position in the linked list so that the list will remain ordered.
If the number being added is already in the list, the program will display a message: "Duplicate number. Not added to the list". This will ensure that the list does not contain any duplicate numbers.
For removing a number from the list, the user will choose the "delete" option and supply the number to be removed. If the number is in the list, it will be eliminated from the linked list. Otherwise, a message will be displayed: "Number is not in the list".
For looking up a number in the list, the user will choose the "search" option and will provide the number to be found. If the number is in the list, the program will display : "the number is in the list". Otherwise, it will display: “the number is not in the list".
For printing, the complete list of numbers, the user will select the "display" option. This option will display the list of all the numbers in the list. It should be an ordered list of unique numbers.
Instructions:
Use the following structure for storing each number.
struct entry
{
int number;
entry * next;
}
Explanation / Answer
#include<iostream>
using namespace std;
struct entry
{
int number;
entry * next;
};
entry* head = NULL;
int addNumber(int item) { //simple insertion
entry *tmp;
if (head == NULL) { //first entry
head = new entry;
head->number = item;
head->next = NULL;
return 1;
}
else if (item < head->number) {
tmp = new entry;
tmp->number = item;
tmp->next = head;
head = tmp;
return 1;
}
else if (item == head->number ) //duplicate
return 0;
tmp = head;
while (tmp->next != NULL) { //find correct locaton
if (tmp->next->number == item) //duplicate
return 0;
else if (tmp->next->number > item) //location found
break;
tmp = tmp->next;
}
entry * newEntry = new entry;
newEntry->number = item;
newEntry->next = tmp->next;
tmp->next = newEntry;
return 1;
}
int delete_item(int item) {
entry * victim;
if (head == NULL) //empty list
return 0;
else if (head->number == item) { //head is victim
victim = head;
head = head->next;
delete victim;
return 1;
}
else if (head->number > item) //search item is less than head so cannot be present
return 0;
entry *tmp;
tmp = head;
while (tmp->next != NULL) {
if (tmp->next->number == item) //victm found
break;
else if (tmp->next->number > item) //cannt be present after this node
return 0;
tmp = tmp->next;
}
if (tmp->next == NULL)
return 0;
victim = tmp->next;
tmp->next = tmp->next->next;
delete victim;
return 1;
}
int search_item(int item) { //simple linear search
entry *current = head;
int index = 0;
while (current != NULL) {
if (current->number > item) //cannot be present after this point
return -1;
else if (current->number == item)
return index;
current = current->next;
index++;
}
return -1;
}
void print_list() {
entry *current = head;
while (current != NULL) {
cout << current->number << endl;
current = current->next;
}
}
int main() {
int choice, number;
do {
cout << "1. Insert" << endl
<< "2. Delete" << endl
<< "3. Search" << endl
<< "4. print" << endl
<< "5. exit" <<endl
<< "Enter choice: ";
cin >> choice;
switch(choice) {
case 1:
cout << "Enter a number: ";
cin >> number;
if (!addNumber(number))
cout << "Duplicate, cannot insert.";
break;
case 2:
cout << "Enter a number: ";
cin >> number;
if (!delete_item(number))
cout << "Not found, cannot delete.";
break;
case 3:
cout << "Enter a number: ";
cin >> number;
if (!search_item(number) == -1)
cout << "Cannot find the number in list.";
else
cout << "Found in the list";
break;
case 4:
print_list();
break;
}
cout << endl;
}while (choice != 5);
}
let me know if you need any changes.... Shall be happy to help you!!