Thursday, September 27, 2012

Doubly Linked Lists Implementation using oops of c plus plus | Data Structure

Doubly Linked Lists Implementation using oops of c plus plus | Data Structure

Doubly Linked Lists Implementation using oops of c plus plus | Data Structure
By Sanjay Sinalkar
Write a program in c++ using object oriented concept to create Double Linklist which having following functionality 

1)Insert element at Begin
2)Insert element at given position
3)Insert element at End
4)Delete element from Begin
5)Delete element From End
6)Delete element from Given position
 7) Show all element Inserted in Display function()
Program should validate given position before calling given position operation such as Insert or Delete



Solution :-


/* Program for Double Linklist implementation */
#include<iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;
Node *prev;
Node() //default constructor
{
next=prev=NULL;
}
Node(int a) //Parameterized Constructor
{
data=a;
next=NULL;
prev=NULL;
}
};


// Class with Double linklist functionality
class Doublell
{
Node *start;
Node *last;
public:
Doublell() //default constructor
{
start=NULL;
last=NULL;
} 

//Insert at given postion
void insertp(int p,int i)
{
Node *temp=new Node(p);
Node *q=start;
for(int c=0;c<i-2;c++)
{
q=q->next;
}
temp->next=q->next;
temp->prev=q;
q->next=temp;
temp->next->prev=temp;
//cout<<"last:="<<temp->data<<endl;
//q->next=temp;
cout<<"Recent attached:-"<<temp->data<<endl;
//start=q;
cout<<"Start Position Element:-"<<start->data<<endl;

}



//Insert at Begin
void insertb(int i)
{
Node *temp=new Node(i);
if(start==NULL)
{
start=temp;
last=temp;
}
else
{
temp->next=start;
start->prev=temp;
start=temp;
cout<<"last:="<<temp->data<<endl;
//q->next=temp;
cout<<"Recent attached:-"<<temp->data<<endl;
//start=q;
cout<<"Start Position Element:-"<<start->data<<endl;
}
}


//Insert at End
void inserte(int i)
{
Node *temp=new Node(i);
if(last==NULL)
{
start=temp;
last=temp;
}
else
{
temp->prev=last;
last->next=temp;
last=temp;
cout<<"last:="<<last->data<<endl;
//q->next=temp;
cout<<"Last attached:-"<<temp->data<<endl;
//start=q;
cout<<"Start Position Element:-"<<start->data<<endl;
}
}


//Delete at Begin
void delb()
{
if(start==last)
{
cout<<"List is Empty From Delete function...!!!"<<endl;
start=last=NULL;
}
else
{
Node *temp=start;
start=start->next;
temp->next=NULL;
temp->prev=NULL;
cout<<"Element "<<temp->data<<" is deleted!"<<endl;
delete temp;
}
}

//Delete at given postion
void delp(int pos)
{
if(start==NULL)
{
cout<<"List is Empty From Delete function...!!!"<<endl;
}

else
{
Node *p,*q=start;
for(int i=0;i<pos-2;i++)
{
q=q->next;
}
if(q->next==NULL)
{
delb();
}
else
{
p=q->next;
q->next=p->next;
p->next->prev=q;
cout<<"Element "<<p->data<<" is deleted!"<<endl;;
delete p;
}
}
}


//Delete at end
void dele()
{
if(start==last)
{
cout<<"List is Empty From Delete function...!!!"<<endl;
start=last=NULL;
}
else
{
Node *temp=last;
Node *q;

q=last->prev;
q->next=NULL;
last=q;
cout<<"\n";
cout<<"Element "<<temp->data<<" is deleted!"<<endl;

}
}

//Count No of elements present in Double linkkist
int no_ele()
{
Node *e=start;
int c=0;
while(e!=NULL)
{
c++;
e=e->next;
}
return c;
}


// Display all inserted elements
void display()
{
if(start==NULL)
{
cout<<"List is Empty Now from Display..!!!"<<endl;
}
else
{
if(start->next==NULL)
{
cout<<"\t"<<start->data<<endl;
}
else
{
Node *d=start;
do
{
cout<<"\t"<<d->data;
//cout<<"\t"<<d->next;
//if()
d=d->next;

}while(d!=NULL);
//cout<<"\t"<<d->data;
cout<<endl;
}
}
}
};


int main()
{
Doublell s;
while(1)
{
cout<<"\n-1 Insert at Begin\n-2 Insert at Position\n-3 Insert at End \n-4 Delete at Begin\n-5 Delete at Position\n-6 Delete at End  \n-7 Show \n-8 Exit"<<endl;
int vb;
cin>>vb;
if(vb==8)
 break;
switch(vb)
{
case 1: int ib;
 cout<<"Enter no in List:"<<endl;
 cin>>ib;s.insertb(ib);
 s.display();
 break;
case 2: int ip,p;
 cout<<"Enter no in List:"<<endl;
 cin>>ip;
 s.display();
 cout<<"Enter the position:"<<endl;
 cin>>p;
 if(p<=s.no_ele()) // check for given postion is valid or not to overcome from Segmentation fault
 {
 s.insertp(ip,p);
 s.display();
 }
 else 
 {
 cout<<"Please Enter Valid Position...!!!"<<endl;
 }
 break;
case 3: int x;
 cout<<"Enter no in List:"<<endl;
 cin>>x;
 s.inserte(x);
 s.display();
 break;
case 4: s.delb();
 s.display();
 break;
case 5: int dp;
 cout<<"Enter Element's position to delete:"<<endl;
 cin>>dp;
  if(dp<=s.no_ele()) // check for given postion is valid or not to overcome from Segmentation fault
        {
        s.delp(dp);
        s.display();
        }
        else
        {
        cout<<"Please Enter Valid Position...!!!"<<endl;
        }
 break;
case 6:
 s.dele();
 s.display();
 break;
case 7: s.display();
 break;
default:cout<<"Invalid Choice...!"<<endl;
 break;
}
}
return 0;
}

0 comments:

Post a Comment