Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

program to create a doubly link list

Posted By: Easy Tutor     Category: C++ Programming     Views: 13812

Write a program to create a double-ended queue (Dequeue) with the following operations:

a) insert in the beginning
b) insert in the end
c) delete from beginning
d) delete from end

Code for program to create a doubly link list in C++ Programming

# include <iostream.h>
# include <conio.h>
# include <malloc.h>
# include <process.h>

int display_menu();

struct link_list
{
 int no;
 struct link_list *next;
 struct link_list *prev;
};
class dqueue
{
 link_list *list,*head;
 public:
    dqueue()
    {
     head=NULL;
    }
    void insert_first();
    void insert_last();
    void delete_first();
    void delete_last();
    void display();
};
void dqueue :: insert_first()
{
  link_list *newnode;
  newnode = new link_list;
  cout<<"Enter Number :";
  cin>>newnode->no;
  list=head;
  if(list==NULL)
  {
   head=newnode;
   newnode->next=NULL;
   newnode->prev=NULL;
   return;
  }
  newnode->next=list;
  newnode->prev=NULL;
  list->prev=newnode;
  head=newnode;
}

void dqueue :: insert_last()
{
 link_list *newnode;
 newnode= new link_list;
 cout<<"Enter Number :";
 cin>>newnode->no;





 list=head;
 if(list==NULL)
 {
  head=newnode;
  newnode->next=NULL;
  newnode->prev=NULL;
  return;
 }
 while(list->next!=NULL)
 {
  list=list->next;
 }
 list->next=newnode;
 newnode->prev=list;
 newnode->next=NULL;
}

void dqueue :: display()
{
 cout<<endl;
 link_list *tail;
 list=head;
 if(head==NULL)
 {
  cout<<"Empty Queue !!!";
  return;
 }
 cout<<endl;
 cout<<"Forward Display ..."<<endl;
 while(list!=NULL)
 {
  cout<<list->no<<"\t";
  if(list->next==NULL)
  {
    tail=list;
  }
    list=list->next;
 }
 list=tail;
 cout<<endl<<"Backward Display ..."<<endl;
 while(list!=NULL)
 {
  cout<<list->no<<"\t";
  list=list->prev;
 }
}

void dqueue :: delete_first()
{
  list=head;
  list->next->prev=NULL;
  head=list->next;
}

void dqueue :: delete_last()
{
  list=head;
  while(list->next->next!=NULL)
  {
   list=list->next;
  }
  list->next=NULL;
}







void main()
{
 dqueue dq1;
 clrscr();
 while(1)
 {
  switch(display_menu())
  {
   case 1 : dq1.insert_first();
        dq1.display();
        getch();
        break;
   case 2 : dq1.insert_last();
        dq1.display();
        getch();
        break;
   case 3 : dq1.delete_first();
        dq1.display();
        getch();
        break;
   case 4 : dq1.delete_last();
        dq1.display();
        getch();
        break;
   case 5 : dq1.display();
        getch();
        break;
   case 6 : exit(0);
  }
 }
}

int display_menu()
{
 int ch;
 clrscr();
 cout<<"[ 1 ] : Insert at First"<<endl;
 cout<<"[ 2 ] : Insert at Last"<<endl;
 cout<<"[ 3 ] : Delete From First"<<endl;
 cout<<"[ 4 ] : Delete From Last"<<endl;
 cout<<"[ 5 ] : Display"<<endl;
 cout<<"[ 6 ] : Exit"<<endl;
 cout<<"Enter your choice :";
 cin>>ch;
 return(ch);
}
  
Share: 


Didn't find what you were looking for? Find more on program to create a doubly link list Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of program to create a doubly link list is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!