Write a C++ program that initially presents a menu of choices for the user. The
ID: 3866125 • Letter: W
Question
Write a C++ program that initially presents a menu of choices for the user. The menu should consist of the following choices:
a.Create linked list of names
b.Insert a new structure into the linked list
c.Modify an existing structure in the linked list
d.Delete an existing structure in the linked list
e. display structure in descending order
f. display structure in ascending order
g.search linked list for a particular structure
h.Exit from the program
Based on the user's selection, the program should execute a function to satisfy the request. Please can you solve this with singly linked structs?
i also need to generate a counter for the number of elements
can you also show steps on how you got your answers
Explanation / Answer
#include <iostream>
#include <stdio.h>
#include <cstdlib>
using namespace std;
struct lists
{
int info;
struct lists *next;
} *start;
class singly
{
public:
lists* create(int);
void insert();
void del();
void asc_sort();
void desc_sort();
void search();
void display();
singly()
{
start = NULL;
}
};
int main()
{
int ch, node, numb, i;
singly sly;
start = NULL;
while(1)
{
cout<<" Menu";
cout<<" 1. Insert node";
cout<<" 2. Delete node";
cout<<" 3. Search node";
cout<<" 4. Ascending Order";
cout<<" 5. Descending Order";
cout<<" 6. Display node";
cout<<" 7. Exit";
cout<<" Enter User choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<" Inserting elements";
sly.insert();
break;
case 2: cout<<" Deleting element";
sly.del();
break;
case 3: cout<<" Searching element";
sly.search();
break;
case 4: cout<<" Sorting - Ascending Order";
sly.asc_sort();
break;
case 5: cout<<" Sorting - Descending Order";
sly.desc_sort();
break;
case 6: cout<<" Display element";
sly.display();
break;
case 7: cout<<" Program Ends";
exit(1);
break;
default: cout<<" Enter U'r Choice correctly: ";
cout<<endl;
break;
}
}
return 0;
}
lists *singly::create(int numb)
{
struct lists *temp, *s;
temp = new(struct lists);
if(temp == NULL)
{
cout<<" Not Allocated";
return 0;
}
else
{
temp->info = numb;
temp->next = NULL;
return temp;
}
}
void singly::insert()
{
int numb;
cout<<" Enter an Element to be inserted: ";
cin>>numb;
struct lists *temp, *l;
temp = create(numb);
if(start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
l = start;
start = temp;
start->next = l;
}
cout<<" Element inserted" <<endl;
}
void singly::del()
{
int counter = 0, i, pos;
if(start == NULL)
{
cout<<" List is Empty";
return;
}
else
{
cout<<" Enter the Element Position to be deleted: ";
cin>>pos;
struct lists *s, *ptr;
s = start;
if(pos == 1)
{
start = s->next;
}
else
{
while(s != NULL)
{
s = s-> next;
counter++;
}
}
if(pos > 0 && pos <= counter)
{
s = start;
for(i=0; i<pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<" Out of range";
}
free(s);
cout<<" Element deleted";
}
}
void singly::search()
{
int numb, pos=0;
bool b = false;
if(start == NULL)
{
cout<<" List is Empty";
return;
}
cout<<" Enter the element to be searched: ";
cin>>numb;
struct lists *s;
s = start;
while(s != NULL)
{
pos++;
if(s->info == numb)
{
b = true;
cout<<" Element " <<numb <<"is found at position " <<pos;
cout<<endl;
}
s = s-> next;
}
if(!b)
cout<<" Element " <<numb <<"is not found";
cout<<endl;
}
void singly::asc_sort()
{
struct lists *ptr, *s;
int numb;
if(start == NULL)
{
cout<<" List is empty";
return;
}
ptr = start;
while(ptr != NULL)
{
for(s = ptr->next; s!= NULL; s=s->next)
{
if(ptr->info > s->info)
{
numb = ptr -> info;
ptr->info = s->info;
s->info = numb;
}
}
ptr = ptr->next;
}
}
void singly::desc_sort()
{
struct lists *ptr, *s;
int numb;
if(start == NULL)
{
cout<<" List is empty";
return;
}
ptr = start;
while(ptr != NULL)
{
for(s = ptr->next; s!= NULL; s=s->next)
{
if(ptr->info < s->info)
{
numb = ptr -> info;
ptr->info = s->info;
s->info = numb;
}
}
ptr = ptr->next;
}
}
void singly::display()
{
struct lists *temp;
if(start == NULL)
{
cout<<" List is empty";
return;
}
temp = start;
cout<<" List of Elements: ";
while(temp != NULL)
{
cout<<temp->info <<"->";
temp = temp->next;
}
}
OUTPUT
Menu
1. Insert node
2. Delete node
3. Search node
4. Ascending Order
5. Descending Order
6. Display node
7. Exit
Enter User choice: 1
Inserting elements
Enter an Element to be inserted: 4
Element inserted
Menu
1. Insert node
2. Delete node
3. Search node
4. Ascending Order
5. Descending Order
6. Display node
7. Exit
Enter User choice: 1
Inserting elements
Enter an Element to be inserted: 5
Element inserted
Menu
1. Insert node
2. Delete node
3. Search node
4. Ascending Order
5. Descending Order
6. Display node
7. Exit
Enter User choice: 1
Inserting elements
Enter an Element to be inserted: 1
Element inserted
Menu
1. Insert node
2. Delete node
3. Search node
4. Ascending Order
5. Descending Order
6. Display node
7. Exit
Enter User choice: 5
Sorting - Descending Order
Menu
1. Insert node
2. Delete node
3. Search node
4. Ascending Order
5. Descending Order
6. Display node
7. Exit
Enter User choice: 6
Display element
List of Elements: 5->4->1->
Menu
1. Insert node
2. Delete node
3. Search node
4. Ascending Order
5. Descending Order
6. Display node
7. Exit
Enter User choice: 7
Program Ends
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.