In this program i used to pointer first and last its create less complexity. It also done by one pointer but it is difficult to handle
Header File
#ifndef _DOUBLY_CIRCULAR_LINKEDLIST_
#define _DOUBLY_CIRCULAR_LINKEDLIST_
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *prev,*next;
};
class dclinkedlist
{
private:
Node *first,*last;
public:
dclinkedlist();
void insetfirst(int);
void print();
bool isEmpty();
void deletefirst();
void append(int );
void deletelast();
};
#endif
Cpp file
#include "doublycircularlinkedlist.h"
dclinkedlist::dclinkedlist()
{
first=last=NULL;
}
void dclinkedlist::insetfirst(int val)
{
Node *temp=new Node;
temp->data=val;
if (first==NULL)
{
first=last=temp;
temp->next=temp->prev=first;
}
else
{
temp->next=first;
temp->prev=last;
last->next=temp;
first->prev=temp;
first=temp;
}
}
void dclinkedlist::print()
{
if(isEmpty())
{
cout <<"No any node to display "<<endl;
}
else
{
Node *ptr=first;
do
{
cout <<ptr->data<<endl;
ptr=ptr->next;
}while (ptr!=last->next);
}
}
bool dclinkedlist::isEmpty()
{
return first==NULL;
};
void dclinkedlist::deletefirst()
{
if (isEmpty())
{
cout <<"No node for delete "<<endl;
}
else if (first==last)
{
first=last=NULL;
}
else
{
Node *temp=first;
first=first->next;
first->prev=last;
last->next=first;
delete temp;
}
}
void dclinkedlist::append(int val)
{
if (isEmpty())
{
insetfirst(val);
}else
{
Node *temp=new Node;
temp->data=val;
temp->next=first;
temp->prev=last;
last->next=temp;
last=last->next;
}
}
void dclinkedlist::deletelast()
{
if (isEmpty())
{
cout <<"No Any Node to delete "<<endl;
}else if (first==last)
{
deletefirst();
}
else
{
Node *temp=last;
last=last->prev;
last->next=first;
delete temp;
}
}
Main function
#include "doublycircularlinkedlist.h"
int main ()
{
dclinkedlist dc;
cout <<"inset first "<<endl;
dc.insetfirst(10);
dc.insetfirst(36);
dc.insetfirst(70);
dc.insetfirst(88);
dc.print();
cout <<"delte first "<<endl;
dc.deletefirst();
dc.print();
cout <<"append fucntion "<<endl;
dc.append(99);
dc.print();
cout <<"Dele last function"<<endl;
dc.deletelast();
dc.print();
return 0 ;
}