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

C++ (1). Implement a doubly linked list that can perform the following: insert (

ID: 3829327 • Letter: C

Question

C++

(1). Implement a doubly linked list that can perform the following: insert (inserts elements), delete(deletes the elements), display (displays the numbers)

(2). Based on doubly linked list implement heaps that allows the user to chose from the following options: inset, delete, display (displays the elements in level order traversal). (3). Also use operator "==" in this function. (4). The implementation should also perform a heap

All of the above must be implemented as single project using one header (.h) file and one .cpp file

Explanation / Answer

//program to implement doubly linked list that can perform insertion ,deletion and display -

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*start;
class double_llist
{
public:
void create_list(int data);
void add_begin(int data);
void add_after(int data, int position);
void delete_element(int data);
void search_element(int data);
void display_dlist();
double_llist()
{
start = NULL;
}
};
int main()
{
int choice, element, position;
double_llist dl;
while (1)
{
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<"1.Create Node 2.Add at begining 3.Add after position 4.Delete 5.Display 6.exit"<<endl;
cout<<"Enter your choice : "<<endl;
cin>>choice;
switch ( choice )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element: ";
cin>>element;
dl.add_begin(element);
cout<<endl;
break;
case 3:
cout<<"Enter the element: ";
cin>>element;
cout<<"Insert Element after postion: ";
cin>>position;
dl.add_after(element, position);
cout<<endl;
break;
case 4:
if (start == NULL)
{
cout<<"List empty,nothing to delete"<<endl;   
break;
}
cout<<"Enter the element for deletion: ";
cin>>element;
dl.delete_element(element);
cout<<endl;
    break;
   case 5:
   dl.display_dlist();
cout<<endl;
break;
case 6:
exit(1);
default:
cout<<"Invalid Selection of Choice ! Try Again !"<<endl;
}
}
return 0;
}
//creating linked list ....
void double_llist::create_list(int data)
{
struct node *s,*temp;
temp=new(struct node);
temp->info=data;
temp->next=NULL;
if(start==NULL)
{
temp->prev=NULL;
start=temp;
}
else
{
s=start;
while(s->next!=NULL)
s=s->next;
s->next=temp;
temp->prev=s;
}
}
//insertion at first / start ......
void double_llist::add_begin(int data)
{
if(start==NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp=new(struct node);
temp->prev=NULL;
temp->info=data;
temp->next=start;
start->prev=temp;
start=temp;
cout<<"Element Inserted"<<endl;
}
//insertion at fixed position......
void double_llist::add_after(int data, int pos)
{
if(start==NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp, *q;
int i;
q=start;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
cout<<"There are less than ";
cout<<pos<<" elements."<<endl;
return;
}
}
temp=new(struct node);
temp->info=data;
if(q->next==NULL)
{
q->next=temp;
temp->next=NULL;
temp->prev=q;
}
else
{
temp->next=q->next;
temp->next->prev=temp;
q->next=temp;
temp->prev=q;
}
cout<<"Element is inserted in the list "<<endl;
}
// Deletion of element from the list..........
void double_llist::delete_element(int data)
{
struct node *temp, *q;
//starting element deletion
if(start->info==data)
{
temp=start;
start=start->next;
start->prev=NULL;
cout<<"Element Deleted"<<endl;
free(temp);
return;
}
q=start;
while(q->next->next!=NULL)
{   
//deletion in between of the list
if(q->next->info==data)
{
temp=q->next;
q->next=temp->next;
temp->next->prev=q;
cout<<"Element Deleted"<<endl;
free(temp);
return;
}
q=q->next;
}
//deletion of last element.....
if(q->next->info==data)
{   
temp=q->next;
free(temp);
q->next=NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element "<<data<<" not found"<<endl;
}
// Display elements of Doubly Link List...........
void double_llist::display_dlist()
{
struct node *q;
if(start==NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
}
q=start;
cout<<"The Doubly Link List is :"<<endl;
while(q!=NULL)
{
cout<<q->info<<" <-> ";
q=q->next;
}
cout<<"NULL"<<endl;
}