Thursday, November 27, 2014

How to create ,insert, reverse, delete and display doubly linklist

#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
void create_list();
void display();
void addatbeg( );
void addafter(int);
void del(int);
void rev();



struct node{
        char name[20]; //name
        int r_no; //roll no
        int con_no; // contact no.
       struct node *next;
       struct node *prev;
}* start;

int main(){

int choice,n,position,i;
int a;
int b;
start=NULL;
while(1)
{
cout<<"1. Create List"<<endl;
cout<<"2. Display List"<<endl;
cout<<"3. Add at Begining"<<endl;
cout<<"4. Add in middle"<<endl;
cout<<"5. Deletion"<<endl;
cout<<"6. Reverse"<<endl;
cout<<"7. Quit Program"<<endl;


cin>>choice;
switch(choice)
{
case 1:
cout<<"How many Nodes you Want? "<<endl;
cin>>n;
for (i=1;i<=n;i++)
{

create_list();
}
break;
case 2:
display();
break;
case 3:
    

addatbeg();
break;
case 7:
exit(0);
case 4:
    

cout<<"Enter position";
cin>>position;
addafter(position);
break;
case 5:
     int m;
     cout<<"enter Element to be deleted"<<endl;
     cin>>m;
     del(m);
     break;
     case 6:
          rev();
          display();
          break;
     default:
cout<<"Wrong Choice"<<endl;
}
}
getch();
return 0;
}
void create_list()
{
struct node *q,*tmp;
tmp=(node *)malloc(sizeof(struct node));
cout<<"Enter name"<<endl;
cin>>tmp->name;
cout<<"Enter the roll no: "<<endl;
cin>>tmp->r_no;
cout<<"Enter contact no"<<endl;
cin>>tmp->con_no;

tmp->next=NULL;
if(start==NULL)
{
tmp->prev=NULL;
start=tmp;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
q->next=tmp;
tmp->prev=q;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
cout<<"Empty List"<<endl;
return;
}
q=start;
cout<<"List is: "<<"\n"<<endl;
while(q!=NULL)
{
cout<<q->name<<"\t";
cout<<q->r_no<<"\t";
cout<<q->con_no<<"\t";
q=q->next;
}
cout<<"\n";
}
void addatbeg()
{
struct node *tmp;
tmp=(node *)malloc(sizeof(struct node));

cout<<"enter name"<<endl;
cin>>tmp->name;
cout<<"enter roll no"<<endl;
cin>>tmp->r_no;
cout<<"enter con_no."<<endl;
cin>>tmp->con_no;

tmp->prev=NULL;
tmp->next=start;
start->prev=tmp;
start=tmp;
}
void addafter(int pos)
{
struct node *tmp, *q;
tmp=(node *)malloc(sizeof(struct node));
int i;
q=start;
for(i=1;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
cout<<"There are less elements"<<pos;
return;
}
}
tmp=(node *)malloc(sizeof(struct node));
cout<<"Enter Name"<<endl;
cin>>tmp->name;
cout<<"Enter roll no"<<endl;;
cin>>tmp->r_no;
cout<<"Enter Contact no"<<endl;
cin>>tmp->con_no;

q->next->prev=tmp;
tmp->next=q->next;
tmp->prev=q;
q->next=tmp;
}

void del(int data)
{
     struct node *q,*tmp;
     if(start->r_no==data)
     {
                          tmp=start;
                          start=start->next;
                          start->prev=NULL;
                          free(tmp);
                          return;
                          }
                          q=start;
                          while(q->next->next!=NULL)
                          {
                                                    if(q->next->r_no==data)
                                                    {
                                                                           tmp=q->next;
                                                    q->next=tmp->next;
                                                    tmp->next->prev=q;
                                                    free(tmp);
                                                    return;
                                                    }
                                                    q=q->next;
                                                    }
                                                    if(q->next->r_no==data)
                                                    {
                                                                           tmp=q->next;
                                                                          
                                                                           q->next=NULL;
                                                                           free(tmp);
                                                                           return;
                                                                           }
}
void rev()
{
     struct node *p1,*p2;
     p1=start;
     p2=p1->next;
     p1->next=NULL;
     p1->prev=p2;
     while(p2!=NULL)
     {
                    p2->prev=p2->next;
                    p2->next=p1;
                    p1=p2;
                    p2=p2->prev;
                    }
                    start=p1;
}
              
                                                                               

No comments:

Post a Comment