Showing posts with label Doubly Link list. Show all posts
Showing posts with label Doubly Link list. Show all posts

Monday, 3 November 2014

Single and Doubly Linked List Implementation with addition and removal of nodes, C++ code

#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
int display(void);
int single(int d,int count);
int doubly(int d,int cnt);
void out(int c);
void outy(int cn);



struct single_ll
{
       int data;
       struct single_ll *e;
}*f,*l,*t,*q,*n;

struct double_ll
{
       int entry;
       struct double_ll *nxt;
       struct double_ll *prev;
}*first,*last,*temp,*m,*r;

int ch,p=0,z;
int main()
{   f=NULL;
    first=NULL;
    int dec,count=0,cnt=0;
    char c='Y';
    while(c=='Y' || c=='y')
    {             system("cls");
                  cout<<"\n"<<"Choose:";
                  cout<<"\n"<<"1==Single Linked List";
                  cout<<"\n"<<"2==Doubly Linked List";
                  cout<<"\n"<<"Your choice (1 or 2) : ";
                  cin>>ch;
                  if(ch==1)
                  {

                  dec=display();
                  count=single(dec,count);

                  }
                  else if(ch==2)
                  {

                  dec=display();
                  cnt=doubly(dec,cnt);

                  }
                  else
                  cout<<"\n"<<"You entered a wrong choice!!";
                  cout<<"\n"<<"Want to continue by re-entering your choice?(Y/N): ";
                  cin>>c;
    }
    cout<<"\n"<<"ThankYou!! Have A Nice Day :) "<<"\n"<<"\n";
    getch();
}

int display(void)
{   int d;
    cout<<"\n"<<"Choose:";
    cout<<"\n"<<"1>Adding node"<<"\n"<<"2>Deleting node"<<"\n"<<"3>Displaying the contents"<<"\n";
    cout<<"\n"<<"Your choice : ";
    cin>>d;
    return d;
}

int single(int d,int count)
{
     switch(d)
     {
              case 1:{
                      int flag=0;
                      if(count<0)
                      count=0;
                      if(count==0)
                      {
                      cout<<"\n"<<"Link List is empty"<<"\n";
                      cout<<"You can only add first node"<<endl;
                      f=new single_ll;
                      cout<<"\n"<<"Enter the data in the node:";
                      cin>>f->data;
                      f->e=NULL;
                      l=f;
                      cout<<"\n"<<"Node added at position 1";
                      count++;
                      }

                      else
                      {
                      cout<<"\n"<<"The list has "<<count<<" nodes";
                      cout<<"\n"<<"Where you want to add the node?(Enter position.,eg.1 or 2...)";
                      cin>>p;
                      z=p;

                      if(p<0 || p>(count+1))
                      {
                      cout<<"\n"<<"Wrong Entry!!";
                      flag=1;

                      }

                      else if(p>1 && p<(count+1))
                      {

                      t=new single_ll;
                      n=f;

                      //Logic for inserting in b//

                      while((p--)!=1)
                      {
                      q=n;
                      n=n->e;
                      }
                      q->e=t;
                      t->e=n;
                      q=NULL;
                      n=NULL;
                      cout<<"\n"<<"Enter the data for the node:";
                      cin>>t->data;

                      }

                      else if(p==1)
                      {
                      t=new single_ll;
                      cout<<"\n"<<"Enter the data for the node:";
                      cin>>t->data;
                      t->e=f;
                      f=t;
                      t=NULL;
                      }

                      else if(p==(count+1))
                      {
                      t=new single_ll;
                      cout<<"\n"<<"Enter the data for the node:";
                      cin>>t->data;
                      l->e=t;
                      t->e=NULL;
                      l=t;
                      t=NULL;
                      }
                      count++;
                      if(flag==0)
                      cout<<"\n"<<"Node added at position "<<z;
                      }

                      return count;
                      }
                      break;
              case 2: {

                      if(count<0)
                      count=0;
                      int flag=0;
                      if(count==0)
                      {
                      cout<<"\n"<<"List is empty!";
                      flag=1;
                      }

                      else
                      {
                      if(count>0)
                      cout<<"\n"<<"The list has "<<count<<" node(s)";
                      cout<<"\n"<<"Which node to delete(Enter position.,eg.1 or 2...):";
                      cin>>p;
                      z=p;

                      //Logic for deletion
                      if(p<=count)
                      {

                      if(p>1 && p<(count+1))
                      {
                      t=f;
                      while(p--!=1)
                      {
                      q=t;
                      t=t->e;
                      }
                      q->e=t->e;
                      t=NULL;
                      }

                      else if(p==1)
                      {
                      t=f;
                      f=f->e;
                      t=NULL;
                      }

                      if(flag==0)
                      {
                      cout<<"\n"<<"Deleted node is node"<<z;
                      return --count;
                      }
                      }


                      else
                      {
                      cout<<"\n"<<"Wrong entry!!";
                      return count;
                      }
                      }
                      }
                      break;
              case 3: {
                      out(count);
                      return count;
                      }
              break;
              default:cout<<"\n"<<"Wrong Choice!";
              }
              }

void out(int c)
{
     if(c==0)
     cout<<"\n"<<"List is empty!";
     else
     {   cout<<"\n";
         t=f;
         do
         {
         cout<<t->data<<"-->>";
         t=t->e;
         }
         while(t!=NULL);
     }
}

//***********************************************//

int doubly(int d,int cnt)
{

     switch(d)
     {
              case 1:{
                      int flag=0;
                      if(cnt<0)
                      cnt=0;
                      if(cnt==0)
                      {
                      cout<<"\n"<<"Link List is empty"<<"\n";
                      cout<<"You can only add first node"<<endl;
                      first=new double_ll;
                      cout<<"\n"<<"Enter the data in the node:";
                      cin>>first->entry;
                      first->nxt=NULL;
                      first->prev=NULL;
                      last=first;
                      cout<<"\n"<<"Node added at position 1";
                      cnt++;
                      }

                      else
                      {
                      cout<<"\n"<<"The list has "<<cnt<<" nodes";
                      cout<<"\n"<<"Where you want to add the node?(Enter position.,eg.1 or 2...)";
                      cin>>p;
                      z=p;

                      if(p<0 || p>(cnt+1))
                      {
                      cout<<"\n"<<"Wrong Entry!!";
                      flag=1;
                      return cnt;
                      }

                      else if(p>1 && p<(cnt+1))
                      {

                      temp=new double_ll;
                      m=first;

                      //Logic for inserting in b//

                      while((p--)!=1)
                      {
                      r=m;
                      m=m->nxt;
                      }
                      r->nxt=temp;
                      temp->nxt=m;
                      m->prev=temp;
                      temp->prev=r;
                      r=NULL;
                      m=NULL;
                      cout<<"\n"<<"Enter the data for the node:";
                      cin>>temp->entry;

                      }

                      else if(p==1)
                      {
                      temp=new double_ll;
                      cout<<"\n"<<"Enter the data for the node:";
                      cin>>temp->entry;
                      temp->nxt=first;
                      first->prev=temp;
                      first=temp;
                      temp=NULL;
                      }

                      else if(p==(cnt+1))
                      {
                      temp=new double_ll;
                      cout<<"\n"<<"Enter the data for the node:";
                      cin>>temp->entry;
                      last->nxt=temp;
                      temp->prev=last;
                      temp->nxt=NULL;
                      last=temp;
                      temp=NULL;
                      }
                      cnt++;
                      if(flag==0)
                      cout<<"\n"<<"Node added at position "<<z;
                      }

                      return cnt;
                      }
                      break;
              case 2: {

                      if(cnt<0)
                      cnt=0;
                      int flag=0;
                      if(cnt==0)
                      {
                      cout<<"\n"<<"List is empty!";
                      flag=1;
                      }

                      else
                      {
                      if(cnt>0)
                      cout<<"\n"<<"The list has "<<cnt<<" node(s)";
                      cout<<"\n"<<"Which node to delete(Enter position.,eg.1 or 2...):";
                      cin>>p;
                      z=p;

                      //Logic for deletion
                      if(p<=cnt)
                      {

                      if(p>1 && p<(cnt+1))
                      {
                      temp=first;
                      while(p--!=1)
                      {
                      r=temp;
                      temp=temp->nxt;
                      m=temp->nxt;
                      }
                      r->nxt=m;
                      m->prev=r;
                      temp=NULL;
                      }

                      else if(p==1)
                      {
                      temp=first;
                      first=first->nxt;
                      first->prev=NULL;
                      temp=NULL;
                      }

                      if(flag==0)
                      {
                      cout<<"\n"<<"Deleted node is node"<<z;
                      return --cnt;
                      }
                      }


                      else
                      {
                      cout<<"\n"<<"Wrong entry!!";
                      return cnt;
                      }
                      }
                      }
                      break;
              case 3: {
                      outy(cnt);
                      return cnt;
                      }
              break;
              default:cout<<"\n"<<"Wrong Choice!";
              }


}

void outy(int c)
{
     if(c==0)
     cout<<"\n"<<"List is empty!";
     else
     {   cout<<"\n";
         temp=first;
         do
         {
         cout<<temp->entry<<"-->>";
         temp=temp->nxt;
         }
         while(temp!=NULL);
     }
}











Implementation of Doubly Link List with addition of nodes C++ code

#define DELAY 150000000
#include<iostream>
using namespace std;
#include<stdlib.h>
int count;
void swap();
void visual();
void delay();
class dll
{
    public:
    int data;
    class dll *next,*prev;
}*first,*last,*pf,*pl,*qf,*ql,*xf,*temp;

int main()
{
    visual();
    int c;
     char ch='y';
     while(ch=='y'|| ch=='Y')
    {
    system("cls");
    cout<<"\nOPERATIONS:\n1:ADD NODE\n2:DISPLAY DOUBLY LIST\n\nYour choice:";
    cin>>c;

    switch(c)
    {
        case 1:{

                    if(first==nullptr)
                    {
                        first=new dll;
                        cout<<"Enter data to node "<<++count<<" : ";
                        cin>>first->data;
                        first->prev=nullptr;
                        first->next=nullptr;
                        last=first;
                    }
                    else
                    {
                        temp=new dll;
                        cout<<"Enter data to node "<<++count<<" : ";
                        cin>>temp->data;
                        temp->prev=last;
                        last->next=temp;
                        temp->next=nullptr;
                        last=temp;
                        temp=nullptr;
                    }
                }
              break;

        case 2:{
                    if(count==0)
                    {
                        cout<<"\n\a\aEmpty List!!";
                        break;
                    }
                    system("cls");
                    cout<<"\nList Details before swapping:->>\n\n";
                    temp=first;
                    cout<<"\nElement   Current Address   Next Address   Previous Address\n\n";
                    for(int i=0;i<count;i++,temp=temp->next)
                    {
                    cout<<"  "<<temp->data<<"         "<<temp<<"          "<<temp->next<<"               "<<temp->prev;
                    cout<<endl<<endl;
                    }

                    cout<<"swapswapswapswapswapswapswapswapswapswapswapswapswapswapswapswapswapswapswapswap";
                    swap();
                    cout<<"\n\nList Details after swapping:->>\n\n";
                    temp=first;
                    cout<<"\nElement   Current Address   Next Address   Previous Address\n\n";
                    for(int i=0;i<count;i++,temp=temp->next)
                    {
                    cout<<"  "<<temp->data<<"         "<<temp<<"          "<<temp->next<<"               "<<temp->prev;
                    cout<<endl<<endl;
                    }
               }
               break;

        default:cout<<"\nWrong entry!!";

    }
     cout<<"\a\nWANT TO CONTINUE?(Y/N):";
     cin>>ch;
    }
    return 0;
}

void swap()
{
                    pl=first;
                    ql=last;
                    pf=pl;
                    qf=ql;
                    if(count==2 || count==3)
                    {
                        pf->prev=pf->next;
                        pf->next=nullptr;
                        qf->next=qf->prev;
                        qf->prev=nullptr;
                        first=qf;
                        last=pf;
                        return;

                    }
                    while((pf->next)!=(qf->prev) && (pf->next)!=qf)
                    {
                        pl=pl->next;
                        ql=ql->prev;

                        if(pf==first)
                        {
                            pl->prev=qf;
                            qf->next=pl;
                            qf->prev=nullptr;
                            first=qf;

                            ql->next=pf;
                            pf->prev=ql;
                            pf->next=nullptr;
                            last=pf;
                        }
                        else
                        {
                            pl->prev=qf;
                            (pf->prev)->next=qf;

                            ql->next=pf;
                            (qf->next)->prev=pf;

                            qf->prev=pf->prev;
                            pf->next=qf->next;

                            pf->prev=ql;
                            qf->next=pl;

                        }
                        pf=pl;
                        qf=ql;
                    }

                    if(count%2==0)
                    {
                    pf=pl->prev;
                    qf=ql->next;

                    pl->next=qf;
                    pl->prev=ql;

                    ql->next=pl;
                    ql->prev=pf;

                    pf->next=ql;
                    qf->prev=pl;
                    }

                    else
                    {
                    xf=pl->next;
                    pf=pl->prev;
                    qf=ql->next;

                    pl->next=qf;
                    pl->prev=xf;

                    ql->next=xf;
                    ql->prev=pf;

                    pf->next=ql;
                    qf->prev=pl;

                    xf->prev=ql;
                    xf->next=pl;
                    }
}

void visual()
{
    system("cls");
    cout<<"LOADING,PLEASE WAIT";
    for(int j=0;j<8;j++)
    {
    cout<<".\a";
    delay();
    }
}
void delay()
{
    for(int i=0;i<DELAY;i++)
    continue;
}