Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a program that will maintain an ordered linked list of positive whole nu

ID: 3656723 • Letter: D

Question

Develop a program that will maintain an ordered linked list of positive whole numbers. Your program will provide for the following options:

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:

Explanation / Answer

/*
* C++ Program to Implement Sorted Singly Linked List
*/

#include <stdio.h>
#include <conio.h>
#include <iostream>

using namespace std;

int c = 0;

struct entry
{
entry *next;
int number;
}
*head = NULL, *p = NULL, *q = NULL, *np = NULL;

void create(int x)
{
np = new entry;
np->number = x;
np->next = NULL;
if (c == 0)
{
head = np;
p = head;
p->next = NULL;
c++;
}
else
{
p = head;
q = p;
if (p->number > np->number)
{
np->next = p;
head = np;
}
else if (p->number < np->number)
{
while (p != NULL && q->number < np->number)
{
q = p;
p = p->next;
if (p == NULL)
{
q->next = np;
np->next = NULL;
}
else if (np->number < p->number)
{
q->next = np;
np->next = p;
break;
}
}
}
}
}

void traverse()
{
entry *t = head;
cout<<" linear display of entrys currently present in linked list.... ";
while (t != NULL)
{
cout<<t->number<<" ";
t = t->next;
}
}

int search(int s)
{
entry *t = head;
while (t != NULL)
{
if((t->number)==s) {
return 1;
}
t = t->next;
}
return 0;
}

int delete(int s)
{
entry *t = head;
int f=0;
   if (t != NULL){
   if(t->number==s) {
           head=t->next;
           return 1;
       }
   }
while (t != NULL)
{
p=t
if((t->number)==s) {
p->next=t->next;
return 1;
}
t = t->next;
}
return 0;
}
int main()
{
int i = 0, n=0, x;
while (n<>4){
cout<<"Please select below options: ";
cout<<"1. Add a number ";
cout<<"2. Delete a number ";
cout<<"3. Search for a number ";
cout<<"4. Display the whole list of numbers ";
cout<<"5. Quit: ";
cin>>n;
if (n==1){
cout<<"Enter the number : ";
cin>>x;
if(search(x))
               cout<<"Duplicate number. Not added to the list ";
else
               create(x);
}
if (n==2){
cout<<"Enter the number : ";
cin>>x;
if (delete(x))
               cout<<"the number deleted. ";
else
               cout<<"the number not found in the list ";
}
if (n==3){
cout<<"Enter the number : ";
cin>>x;
if (search(x))
               cout<<"the number is in the list ";
else
               cout<<"the number is not in the list ";
}
if (n==4){
traverse();
}
if (n==5){
break;
}
}
cout<<"Exited.";
}