Here is the screenshot of the .dat file, because i cannot upload it. Write a C++
ID: 3815756 • Letter: H
Question
Here is the screenshot of the .dat file, because i cannot upload it.
Write a C++ program derived from HW06_LinkedList_ref.cpp (provided) that presents a menu for the user. The program reads records from a file and then performs the operations listed in the menu. The sample data file, "HW06 dat", is provided. The first line of the data file is an integer indicating the number of records in the file. The rest of the file are records. After each operation, the program displays the updated record and the updated total number of records. Pick an option from the following menu (option 1 must be selected first): Create an initial list of names and phone numbers from a file and then display the list. Insert a new record into the linked list (NOT at the front) and then display the list. Append a new record to the end of the linked list and then display the list. Modify an existing record in the linked list and then display the list. Delete an existing record from the list (NOT the first record) and then display the list. Delete the last record from the list and then display the list. (***Up to 5 bonus points***) Insert a record at the front of the list and then display the list. (***Up to 5 bonus points***) Delete the first record from the front of the list and then display the list. Exit from the program Sam Acme (555)898-2392 Edith Dolan (555)682-3104 Dohan Lanfranc (555)718-4581 Patrick Smith (555)123-4564 Doe Hagar (555)567-8953 June Hagar (555)258-8965 Jacob Fink (555)456-8521Explanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
char *name;
char *phone_number;
struct node *next;
}*start;
/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(char*, char*);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void delete_begin();
void delete_last();
void update();
void display();
single_llist()
{
start = NULL;
}
};
/*
* Main :contains menu
*/
main()
{
int choice, nodes, element, position, i;
single_llist sl;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1. Create Initial list"<<endl;
cout<<"2. Insert node at position"<<endl;
cout<<"3. Insert node at Last"<<endl;
cout<<"4. Modify an existing record"<<endl;
cout<<"5. Delete a Particular Node"<<endl;
cout<<"6. Deleet last Node"<<endl;
cout<<"7. Insert at front"<<endl;
cout<<"8. delete from front"<<endl;
cout<<"9.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
//get record from file
sl.display();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
sl.display();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at last position:"<<endl;
sl.insert_last();
sl.display();
cout<<endl;
break;
case 4:
cout<<"Modify Record: "<<endl;
sl.update();
sl.display();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
sl.delete_pos();
sl.display();
break;
case 6:
cout<<"Delete last node:"<<endl;
sl.delete_last();
sl.display();
cout<<endl;
break;
case 7:
cout<<"Insert at begin: "<<endl;
sl.insert_begin();
sl.display();
cout<<endl;
break;
case 8:
cout<<"Delete from front"<<endl;
sl.delete_begin();
sl.display();
cout<<endl;
break;
case 9:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
/*
* Creating Node
*/
node *single_llist::create_node(char *name, char *phone_number)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->name = name;
temp->phone_number = phone_number;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void single_llist::insert_begin()
{
struct node *temp, *p;
char *name;
char *phone_number;
cout<<"Enter the Name to be inserted: ";
cin>>name;
cout<<"Enter the phone number to be inserted: ";
cin>>phone_number;
temp = create_node(name, phone_number);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Inserting Node at last
*/
void single_llist::insert_last()
{
struct node *temp, *s;
char *name;
char *phone_number;
cout<<"Enter the Name to be inserted: ";
cin>>name;
cout<<"Enter the phone number to be inserted: ";
cin>>phone_number;
temp = create_node(name, phone_number);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}
/*
* Insertion of node at a given position
*/
void single_llist::insert_pos()
{
struct node *temp, *s, *ptr;
int value, pos, counter = 0;
char *name;
char *phone_number;
cout<<"Enter the Name to be inserted: ";
cin>>name;
cout<<"Enter the phone number to be inserted: ";
cin>>phone_number;
temp = create_node(name, phone_number);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
/*
* Delete element at a given position
*/
void single_llist::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *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 = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
/*
* Delete first element
*/
void single_llist::delete_begin()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct node *s, *ptr;
s = start;
start = s->next;
free(s);
cout<<"Element Deleted"<<endl;
}
/*
* Delete element at last position
*/
void single_llist::delete_last()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
struct node *s, *ptr;
s = start;
while (s->next != NULL)
{
ptr = s;
s = s->next;
}
ptr->next = NULL;
free(s);
cout<<"Element Deleted"<<endl;
}
/*
* Update a given Node
*/
void single_llist::update()
{
int value, pos, i;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be updated: ";
cin>>pos;
char *name;
char *phone_number;
cout<<"Enter the Name to be inserted: ";
cin>>name;
cout<<"Enter the phone number to be inserted: ";
cin>>phone_number;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start->name = name;
start->phone_number = phone_number;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<pos<<" elements";
return;
}
s = s->next;
}
s->name = name;
s->phone_number = phone_number;
}
cout<<"Node Updated"<<endl;
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->name<<" ";
cout<<temp->phone_number<<" -> ";
temp = temp->next;
}
cout<<"NULL"<<endl;
}