niti Suppose that we have some people and each person has id, name, surname and
ID: 3765822 • Letter: N
Question
niti Suppose that we have some people and each person has id, name, surname and age information. We want to store each person in a Doubly Linked List. In doubly linked list, each node a reference to next node and a reference to previous node that can be seen from Figure 1. node item prev next next item prev next Figure 1. Double Linked List Write a Ct+ program that implements this logic and you should write some methods in the program that are listed below: Inserting a person to beginning of the list. Inserting a person to end of the list. Deleting a person from beginning of the list. Deleting a person from end of the list. Display all people with their names and surnames in the list. Display all people with their names and surnames in the list from in reverse form. Find person who has minimum age. After insertion and deletion operations, make appropriate connections between nodes. m Outp Insert to Last Insert te Be Delete Pron Beginning Delete Fron Last Display Display Reverse Mininun ress1 to quit nter your cheice: 1 lease enter person id :1 lease enter person surnane iren lease enter person age 24 erson ecen iren inserted to end of the list Figure 1. Inserting to LastExplanation / Answer
Here is the code for you. If you have any further problems, just get back to me.
/*
* C++ Program to Implement Doubly Linked List
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
/*
* Node Declaration
*/
using namespace std;
struct node
{
int ID;
string name;
string surname;
int age;
struct node *next;
struct node *prev;
} *head, *tail;
/*
Class Declaration
*/
class double_llist
{
public:
void insert_begin(int, string, string, int);
void insert_last(int, string, string, int);
void delete_begin();
void delete_last();
void display_dlist();
void display_reverse_dlist();
int find_minimum_age();
double_llist()
{
head = NULL;
tail = NULL;
}
};
/*
* Main: Conatins Menu
*/
int main()
{
int choice, minAge;
int ID, age;
string name, surname;
double_llist dl;
while (1)
{
cout<<endl<<"***********USER MENU**********"<<endl;
cout<<"1) Insert to Last"<<endl;
cout<<"2) Insert to Beg"<<endl;
cout<<"3) Delete From Beginning"<<endl;
cout<<"4) Delete From Last"<<endl;
cout<<"5) Display"<<endl;
cout<<"6) Display Reverse"<<endl;
cout<<"7) Find Minimum Age"<<endl;
cout<<"Press -1 to quit!"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch ( choice )
{
case 1:
cout<<"Please enter person id: ";
cin>>ID;
cout<<"Please enter person name: ";
cin>>name;
cout<<"Please enter person surname: ";
cin>>surname;
cout<<"Please enter person age: ";
cin>>age;
dl.insert_last(ID, name, surname, age);
cout<<"Person "<<name<<" "<<surname<<" inserted to end of the list"<<endl;
cout<<endl;
break;
case 2:
cout<<"Please enter person id: ";
cin>>ID;
cout<<"Please enter person name: ";
cin>>name;
cout<<"Please enter person surname: ";
cin>>surname;
cout<<"Please enter person age: ";
cin>>age;
dl.insert_begin(ID, name, surname, age);
cout<<"Person "<<name<<" "<<surname<<" inserted to beginning of the list"<<endl;
cout<<endl;
break;
case 3:
dl.delete_begin();
cout<<"Doubly Linked List after deletion: ";
dl.display_dlist();
cout<<endl;
break;
case 4:
dl.delete_last();
cout<<"Doubly Linked List after deletion: ";
dl.display_dlist();
cout<<endl;
break;
case 5:
cout<<"People in the list: "<<endl;
dl.display_dlist();
cout<<endl;
break;
case 6:
cout<<"People in the list in reverse form: "<<endl;
dl.display_reverse_dlist();
cout<<endl;
break;
case 7:
minAge = dl.find_minimum_age();
if(minAge != 500)
cout<<"Minimum age is: "<<minAge<<endl;
else
cout<<"No persons."<<endl;
cout<<endl;
break;
case -1:
exit(1);
}
}
return 0;
}
/*
* Insertion at the beginning
*/
void double_llist::insert_begin(int ID, string name, string surname, int age)
{
struct node *temp;
temp = new(struct node);
temp->prev = NULL;
temp->ID = ID;
temp->name = name;
temp->surname = surname;
temp->age = age;
temp->next = head;
if(head == NULL)
head = tail = temp;
else
{
head->prev = temp;
temp->next = head;
head = temp;
}
}
/*
* Insertion at the end
*/
void double_llist::insert_last(int ID, string name, string surname, int age)
{
struct node *temp;
temp = new(struct node);
temp->next = NULL;
temp->ID = ID;
temp->name = name;
temp->surname = surname;
temp->age = age;
temp->prev = tail;
if(tail == NULL)
head = tail = temp;
else
{
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
/*
* Deletion at the beginning
*/
void double_llist::delete_begin()
{
struct node *temp;
if(head != NULL)
{
if(head == tail)
{
free(head);
head = tail = NULL;
}
else
{
temp = head;
head = head->next;
free(temp);
head->prev = NULL;
}
}
}
/*
* Deletion at the end
*/
void double_llist::delete_last()
{
struct node *temp;
if(tail != NULL)
{
if(head == tail)
{
free(head);
head = tail = NULL;
}
else
{
temp = tail;
tail = tail->prev;
free(temp);
tail->next = NULL;
}
}
}
/*
* Display in forward order
*/
void double_llist::display_dlist()
{
struct node *q;
q = head;
while (q != NULL)
{
cout<<"Person "<<q->ID<<" is "<<q->name<<" "<<q->surname<<endl;
q = q->next;
}
}
/*
* Display in reverse order
*/
void double_llist::display_reverse_dlist()
{
struct node *q;
q = tail;
while (q != NULL)
{
cout<<"Person "<<q->ID<<" is "<<q->name<<" "<<q->surname<<endl;
q = q->prev;
}
}
/*
* Finding the minimum age.
*/
int double_llist::find_minimum_age()
{
struct node *q;
q = head;
int minAge = 500;
while(q != NULL)
{
if(q->age < minAge)
minAge = q->age;
q = q->next;
}
return minAge;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.