delete fist and last node in double link list

// Dobly Link List DELETE First
#include
#include
using namespace std;
struct node
{
int info;
node *plink,*flink;
}*head=NULL,*tail=NULL,*nptr,*ptr;

void insert()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
if (head==NULL)
{
head=nptr;
tail=nptr;
nptr->plink=NULL;
nptr->flink=NULL;
}
else
{
ptr=head;
nptr->flink=ptr;
nptr->plink=NULL;
ptr->plink=nptr;
head=nptr;
}
}

void deletef()
{
ptr=head;
head=ptr->flink;
head->plink=NULL;
}

void traverse()
{
ptr=head;
while(ptr!=NULL)
{
cout<info<<endl;
ptr=ptr->flink;
}
}

int main()
{
int n,i;
cout<<"Enter the no of element you want to enter";
cin>>n;
for(i=1;i<=n;i++)
{
insert();
}
deletef();
traverse();
}


======================================================================
// Dobly Link List DELETE Last
#include
#include
using namespace std;
struct node
{
int info;
node *plink,*flink;
}*head=NULL,*tail=NULL,*nptr,*ptr;

insert()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
if (head==NULL)
{
head=nptr;
tail=nptr;
nptr->plink=NULL;
nptr->flink=NULL;
}
else
{
ptr=head;
nptr->flink=ptr;
nptr->plink=NULL;
ptr->plink=nptr;
head=nptr;
}
}

deletel()
{
ptr=tail;
tail=ptr->plink;
tail->flink=NULL;
}

traverse()
{
ptr=head;
while(ptr!=NULL)
{
cout<info<<endl;
ptr=ptr->flink;
}
}

int main()
{
int n,i;
cout<<"Enter the no of element you want to enter";
cin>>n;
for(i=1;i<=n;i++)
{
insert();
}
deletel();
traverse();
}

0 comments:

Post a Comment