Tuesday, 17 September 2013

Double Linked List

HTML Online Editor Sample

 

#include<iostream>

using namespace std;
class doubly_linked_list
{private:
struct node
{int element;
node* front;
node* rear;
}*head;
public:
doubly_linked_list();
void insertatbig(int element);
void insertatend(int element);
void  insertatkth(int element);
void insertatkthfromend(int  element);
void remove(int element);
void display();
bool isempty();
void swap(int i,int j);
~doubly_linked_list();
};
doubly_linked_list::doubly_linked_list()
{
  head=NULL;
}
bool doubly_linked_list::isempty()
{
  if(head==NULL)
  return true;
  else return false;
}
void doubly_linked_list::insertatbig(int element)
{
  if(isempty())
  {head=new node;
    head->element=element;
    head->front=NULL;
    head->rear=NULL;
    return;
  }
  node*t=new node();
  t->element=element;
  t->front=head;
  head->rear=t;
  t->rear=NULL;
  head=t;
  return;
 
}
void doubly_linked_list::insertatend(int element)
{
  if(isempty())
  {head=new node;
    head->element=element;
    head->front=NULL;
    head->rear=NULL;
    return;
  }
  node* t=new node;
  t->element=element;
  t->front=NULL;
  node*curr;
  curr=head;
  while(curr->front!=NULL)
  curr=curr->front;
  curr->front=t;
  t->rear=curr;
  return;
}
void doubly_linked_list::display()
{
if(isempty())
{
  cout<<"\n linked list iis empty\n";
  return;
}
node*curr=head;
while(curr!=NULL){
cout<<"\t"<<curr->element<<"->";
curr=curr->front;
}}
void doubly_linked_list::insertatkth(int pos)
{int count=0;
  node*curr=head;
  while(curr!=NULL)
  {
    count++;
    if(count==pos){
      node*t=new node;
    cout<<"\n enter the value of node to be inserted";
    int a;
    cin>>a;
    t->element=a;
    t->front=NULL;
    t->rear=NULL;
    if(curr->front!=NULL){
    t->front=curr->front;
    curr->front=t;
    curr->front->rear=t;
    t->rear=curr;}
    else{
    t->front=NULL;
    t->rear=curr;
    curr->front=t;}
    }
    curr=curr->front;
  }
}
void doubly_linked_list::remove(int element)
{node* curr=head;
if(isempty())
{
  cout<<"\n list is empty";
  return ;
 
}
  if(head->element==element)
  {
    node* t=head;
    head=head->front;
    head->rear=NULL;
    delete (t);return;
  }
  while(curr->front!=NULL)
  {if(curr->front->element==element)
  {if(curr->front->front==NULL){
    node*t=curr->front;
  curr->front=NULL;
  return;}
else if(curr->front->front!=NULL)  {
node*t=curr->front;
curr->front=t->front;
t->front->rear=curr;
delete(t);
return;}
 
  }
    curr=curr->front;
  }
}
doubly_linked_list::~doubly_linked_list()
{
 
}
int main()
{
  doubly_linked_list  dl;
dl.insertatend(5);dl.display();cout<<endl;
dl.insertatend(6);dl.display();cout<<endl;
dl.insertatend(7);dl.display();cout<<endl;
dl.insertatbig(8);dl.display();cout<<endl;
dl.insertatbig(9);dl.display();cout<<endl;
dl.insertatend(4);dl.display();cout<<endl;
dl.remove(6);dl.display();cout<<endl;
dl.remove(7);dl.display();cout<<endl;
dl.remove(9);dl.display();cout<<endl;
return 0;
}

No comments:

Post a Comment