Tuesday, 3 November 2015

Double linked list program in CPP - Data Structures

#include<iostream>
using namespace std;
typedef struct node
{
int data;
struct node *plink;
struct node *slink;
};
node *slist=NULL;
int insBeg(int val)
{
node *newnode=new node;
newnode->data=val;
newnode->plink=NULL;
newnode->slink=slist;
slist=newnode;
return 0;
}
void insEnd(int val)
{
if(slist==NULL)
{
node *newnode=new node;
newnode->data=val;
newnode->plink=NULL;
newnode->slink=NULL;
slist=newnode;
}
else
{
node *temp=slist;
while(temp->slink!=NULL)
temp=temp->slink;
node *newnode =new node;
newnode->data=val;
newnode->plink=temp;
newnode->slink=NULL;
temp->slink=newnode;
}
}
int insLoc(int val,int loc)
{
node *temp=slist;
for(int i=1;i<loc;i++)
{
temp=temp->slink;
if(temp==NULL)
{
cout<<"\nINVALID LOCATION";
return 0;
}
}
node *newnode=new node;
newnode->data=val;
newnode->plink=temp;
newnode->slink=temp->slink;
temp->slink->plink=newnode;
temp->slink=newnode;
}

int delnode(int val)
{
if(slist->slink==NULL && slist->data==val)
{
slist=NULL;
return 0;
}
node *temp=slist;
while(temp!=NULL)
{
if(temp->data==val)
{
   if(temp==slist)
      {
      slist=temp->slink;
      slist->plink=NULL;
      }
    else if(temp->slink==NULL)
      temp->plink->slink=NULL;
     else
      {
       temp->plink->slink=temp->slink;
       temp->slink->plink=temp->plink;
      }
     delete temp;
     return 0;
}
temp=temp->slink;
}
cout<<"\nData not found";
return 0;
}
int retrieve(int loc)
{
node *temp=slist;
int count=1;
while(temp!=NULL)
 {
 if(count==loc)
  return temp->data;
temp=temp->slink;
if(temp==slist)
break;
count++;
}
cout<<"\nInvalid location";
return 0;
}
int count()
{
node *temp=slist;
int count=0;
while(temp!=NULL)
{
temp=temp->slink;
count++;
if(temp==slist)
break;
}
return count;
}
void display()
{
node *temp=slist;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->slink;
}
}


int main()
{
int choice,val,loc;
while(1)
{
cout<<"\nDOUBLE LINKED LIST\n1.Insert at the Beginning \n2.Insert at end\n3.Insert at location\n4.delete\n5.retrive\n6.Count\n7.Display\n8.Exit\nEnter choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nEnter the data:";cin>>val;
insBeg(val);
break;
case 2:
cout<<"\nEnter the data:";cin>>val;
insEnd(val);
break;
case 3:
cout<<"\nEnter the data,location:";cin>>val;cin>>loc;
insLoc(val,loc);
break;
case 4:
cout<<"\nEnter the data:";cin>>val;
delnode(val);
break;
case 5:
cout<<"\nEnter the location:";cin>>loc;
cout<<"\nretrieved data:"<<retrieve(loc);
break;
case 6:
cout<<"\nNumber of nodes:";cout<<count();
break;
case 7:
display();
break;
case 8:
return 0;
break;
}


}
return 0;
}



OUTPUT:
DOUBLE LINKED LIST
1.Insert at the Beginning
2.Insert at end
3.Insert at location
4.delete
5.retrieve
6.Count
7.Display
8.Exit

Enter choice:1
Enter the data:10

Enter choice:2
Enter the data:30

Enter choice:2
Enter the data:20

Enter choice:3
Enter the data,location:15 2

Enter choice:7
10      30      15      20

Enter choice:4
Enter the data:30

Enter choice:5
Enter the location:2
retrieved data:15

Enter choice:6
Number of nodes:3

Enter choice:7
10      15      20

No comments:

Post a Comment