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

Write a C++ program that initially presents a menu of choices for the user. The

ID: 3866082 • 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 an initial linked list of names and phone numbers

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 using only structs?

Explanation / Answer

Given below is the code for the question. Please don't forget to rate the answer if it helped. Thank you.

phonelist.h

#ifndef phonelist_h
#define phonelist_h
#include <iostream>
using namespace std;
//doubly linked list in sorted order on name
class PhoneList
{
typedef struct Node
{
string name;
string phone;
Node* prev;
Node* next;
  
Node(string n, string p)
{
name = n;
phone = p;
prev = NULL;
next = NULL;
}
  
void setNext(Node* n)
{
next = n;
if(n != NULL)
n->prev = this;
}
  
void setPrev(Node* n)
{
prev = n;
if(n != NULL)
n->next = this;
}
}Node;
  
  
Node* head;
Node* tail;
  
public:
PhoneList()
{
head = tail = NULL;
}
  
void insert(string name, string phone)
{
Node *n = new Node(name, phone);
if(head == NULL)
head = tail = n;
else
{
Node *curr = head;
while(curr != NULL)
{
if(name < curr->name)
break;
curr = curr->next;
}
  
if(curr == NULL)//reached end of list
{
tail->setNext(n);
tail = n;
}
else
{
n->setPrev(curr->prev);
n->setNext(curr);
if(n->next == head) //insertion before head
head = n;
}
}
}
  
Node* findNode(string name) const
{
Node* curr = head;
while(curr != NULL)
{
if(curr->name == name)
return curr;
curr = curr->next;
}
return NULL;
}
  
bool modify(string name, string phone)
{
Node* curr = findNode(name);
if(curr == NULL)
return false;
else
{
curr->phone = phone;
return true;
}
}
  
bool remove(string name)
{
Node* curr = findNode(name);
if(curr == NULL)
return false;
else
{
if(curr->prev != NULL) //not removng head node
{
curr->prev->setNext(curr->next);
if(curr == tail) //deleting tail node
tail = curr->prev;
  
}
else
{
head = head->next;
head->prev = NULL;
if(head == NULL)
tail = NULL;
}
  
delete curr;
return true;
}
}
  
void displayAscending()
{
Node* curr = head;
cout << "-----------------" << endl;
while(curr != NULL)
{
cout << curr->name << " " << curr->phone << endl;
curr = curr->next;
}
cout << "-----------------" << endl << endl;

}
  
void displayDescending()
{
Node* curr = tail;
cout << "-----------------" << endl;

while(curr != NULL)
{
cout << curr->name << " " << curr->phone << endl;
curr = curr->prev;
}
cout << "-----------------" << endl << endl;

}
  
string search(string name) const
{
Node *n = findNode(name);
if(n == NULL)
return "";
else
return n->phone;
}
};
#endif /* phonelist_h */

main.cpp

#include "phonelist.h"
#include <iostream>
using namespace std;

PhoneList createInitialList();
void insert(PhoneList &list);
void modify(PhoneList &list);
void remove(PhoneList &list);
void search(const PhoneList &list);

int main()
{
char choice = 0;
PhoneList list;
  
while(choice != 'h' && choice != 'H')
{
cout << "a. Create an initial list" << endl;
cout << "b. Insert " << endl;
cout << "c. Modify" << endl;
cout << "d. Delete" << endl;
cout << "e. Display Descending" << endl;
cout << "f. Display Ascending" << endl;
cout << "g. Search" << endl;
cout << "h. Exit" << endl;
  
cout << "Enter your choice: " ;
cin >> choice;
  
switch(choice)
{
case 'a':
case 'A':
list = createInitialList();
break;
case 'b':
case 'B':
insert(list);
break;
case 'c':
case 'C':
modify(list);
break;
case 'd':
case 'D':
remove(list);
break;
case 'e':
case 'E':
list.displayDescending();
break;
case 'f':
case 'F':
list.displayAscending();
break;
case 'g':
case 'G':
search(list);
break;
case 'h':
case 'H':
break;
default:
cout << "Invalid choice" << endl;
  
}
}
return 0;
}

PhoneList createInitialList()
{
PhoneList list;
list.insert("John", "1111111");
list.insert("Alice", "2222222");
list.insert("Peter", "3333333");
return list;
  
  
}
void insert(PhoneList &list)
{
string name, phone;
cout << "Inserting a new name and phone..." << endl;
cout << "Enter name: ";
cin >> name;
cout << "Enter phone: ";
cin >> phone;
list.insert(name, phone);
cout << "Successfully inserted." << endl << endl;
}

void modify(PhoneList &list)
{
string name, phone;
cout << "Modifying an existing name..." << endl;
cout << "Enter name: ";
cin >> name;
cout << "Enter new phone: ";
cin >> phone;
if(list.modify(name, phone))
cout << "Successfully modified." << endl;
else
cout << "Not found " << name << endl;
  
cout << endl;
  

}

void remove(PhoneList &list)
{
string name, phone;
cout << "Deleting an existing name..." << endl;
cout << "Enter name: ";
cin >> name;
if(list.remove(name))
cout << "Successfully deleted." << endl;
else
cout << "Not found " << name << endl;
  
cout << endl;

}
void search(const PhoneList &list)
{
string name, phone;
cout << "Searching an existing name ..." << endl;
cout << "Enter name: ";
cin >> name;
phone = list.search(name);
if(phone != "")
cout << "Found " << name << ". Phone = " << phone << endl;
else
cout << "Not found " << name << endl;
cout << endl;

}

output

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: a
a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: f
-----------------
Alice   2222222
John   1111111
Peter   3333333
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: e
-----------------
Peter   3333333
John   1111111
Alice   2222222
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: b
Inserting a new name and phone...
Enter name: Alen
Enter phone: 665443
Successfully inserted.

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: f
-----------------
Alen   665443
Alice   2222222
John   1111111
Peter   3333333
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: e
-----------------
Peter   3333333
John   1111111
Alice   2222222
Alen   665443
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: b
Inserting a new name and phone...
Enter name: Richard
Enter phone: 99999
Successfully inserted.

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: f
-----------------
Alen   665443
Alice   2222222
John   1111111
Peter   3333333
Richard   99999
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: e
-----------------
Richard   99999
Peter   3333333
John   1111111
Alice   2222222
Alen   665443
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: d
Deleting an existing name...
Enter name: John
Successfully deleted.

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: g
Searching an existing name ...
Enter name: John
Not found John

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: g
Searching an existing name ...
Enter name: Richard
Found Richard. Phone = 99999

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: f
-----------------
Alen   665443
Alice   2222222
Peter   3333333
Richard   99999
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: e
-----------------
Richard   99999
Peter   3333333
Alice   2222222
Alen   665443
-----------------

a. Create an initial list
b. Insert
c. Modify
d. Delete
e. Display Descending
f. Display Ascending
g. Search
h. Exit
Enter your choice: h

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote