Showing posts with label Link list. Show all posts
Showing posts with label 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;
}

Binary Search Tree(BST) Array representation to Link List representation using zero index array

#include<iostream>
using namespace std;
#include<stdlib.h>

struct BST_Ar_L
{
    int data,dex;
    struct BST_Ar_L *left;
    struct BST_Ar_L *right;
}*root,*l,*r,*temp,*ctr;

int main()
{
    int a[100],t,index,m,n,key,flag=0;
    char ch='y';
    for(int i=0;i<99;i++)
    a[i]=-1;
    cout<<"\nEnter the elements of BST as per array implementation(starting from '0' index)\n";
    while(ch=='y' || ch=='Y')
    {
        cout<<"\nEnter the index of array:";
        cin>>t;
        cout<<"\nEnter the element of BST at that index:";
        cin>>a[t];
        cout<<"\nWant to enter more elements?(Y/N):";
        cin>>ch;
    }
    if(a[0]==-1)
    {
        cout<<"\nNo root element\nProgram will now exit...";
        return 0;
    }

    root=new BST_Ar_L;
    root->data=a[0];
    root->dex=0;
    root->left=NULL;
    root->right=NULL;

    if(a[1]!=-1)
    {
        l=new BST_Ar_L;
        l->data=a[1];
        l->dex=1;
        l->left=NULL;
        l->right=NULL;
        root->left=l;
    }
    if(a[2]!=-1)
    {
        r=new BST_Ar_L;
        r->data=a[2];
        r->dex=2;
        r->left=NULL;
        r->right=NULL;
        root->right=r;
    }
    index=3;
    while(index!=99)
    {
        if(a[index]==-1)
        {
        index++;
        continue;
        }
        else
        {
            temp=new BST_Ar_L;
            temp->data=a[index];
            if(index%2==1)
            {
                n=(index-1)/2;
                m=n;

                if(m%2==0)
                while(m!=1)
                m=(m-2)/2;
                else
                while(m!=2)
                m=(m-1)/2;

                    if(m==1)
                    ctr=l;
                    else
                    ctr=r;
                    while(ctr->dex!=n)
                    ctr=n%2?ctr->left:ctr->right;
                    index%2?ctr->left=temp:ctr->right=temp;
                    temp->dex=index;
                    temp->left=NULL;
                    temp->right=NULL;
            }
            else
            {

                n=(index-2)/2;
                m=n;

                if(m%2==0)
                while(m!=2)
                m=(m-2)/2;
                else
                while(m!=1)
                m=(m-1)/2;

                    if(m==1)
                    ctr=l;
                    else
                    ctr=r;

                    while(ctr->dex!=n)
                    n%2?ctr=ctr->left:ctr=ctr->right;
                    index%2?ctr->left=temp:ctr->right=temp;
                    temp->dex=index;
                    temp->left=NULL;
                    temp->right=NULL;

            }
             index++;
        }

    }
    system("cls");
    ch='y';
    while(ch=='y' || ch=='Y')
    {
    cout<<"\n********************************************************************************";
    cout<<"\n********************************************************************************\n";
    cout<<"\nEnter the element to display its details in BST:";
    cin>>key;
    flag=0;
    for(int i=0;i<100;i++)
    {
        if(a[i]==key)
        flag=1;
    }
    if(flag==1)
    {
    if(key==a[0])
    {
    cout<<"\nIt is the root element";
    if(a[1]!=-1 && a[2]!=-1)
    cout<<"\nRight Child="<<a[2]<<"\t\tLeft Child="<<a[1] ;
    else if(a[1]!=-1 && a[2]!=1)
    cout<<"\nRight Child=empty"<<"\t\tLeft Child="<<a[1] ;
    else
    cout<<"\nRight Child="<<a[2]<<"\t\tLeft Child=empty" ;
    }
    else
    {
        key<a[0]?ctr=l:ctr=r;
        temp=ctr;
        flag=0;
        while(flag!=1)
        {
            if(ctr->data==key)
            {
                if(ctr->dex==1 || ctr->dex==2)
                {
                if(ctr->right!=NULL && ctr->left!=NULL)
                cout<<"\nParent element="<<a[0]<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child="<<ctr->left->data;
                else if(ctr->right!=NULL && ctr->left==NULL)
                cout<<"\nParent element="<<a[0]<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child=empty";
                else if(ctr->right==NULL && ctr->left!=NULL)
                cout<<"\nParent element="<<a[0]<<"\nRight Child=empty"<<"\t\tLeft Child="<<ctr->left->data;
                else
                cout<<"\nParent element="<<a[0]<<"\nRight Child=empty"<<"\t\tLeft Child=empty";
                flag=1;
                }
                else
                {
                if(ctr->right!=NULL && ctr->left!=NULL)
                cout<<"\nParent element="<<temp->data<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child="<<ctr->left->data;
                else if(ctr->right!=NULL && ctr->left==NULL)
                cout<<"\nParent element="<<temp->data<<"\nRight Child="<<ctr->right->data<<"\t\tLeft Child=empty";
                else if(ctr->right==NULL && ctr->left!=NULL)
                cout<<"\nParent element="<<temp->data<<"\nRight Child=empty"<<"\t\tLeft Child="<<ctr->left->data;
                else
                cout<<"\nParent element="<<temp->data<<"\nRight Child=empty"<<"\t\tLeft Child=empty";
                flag=1;
                flag=1;
                }
            }
            else
                {
                    if(key<(ctr->data))
                    {
                        temp=ctr;
                        ctr=ctr->left;
                    }
                    else
                    {
                        temp=ctr;
                        ctr=ctr->right;
                    }
                }
        }
    }
    }
    else
    cout<<"\nElement not present in BST!!";
            cout<<"\nWant to display more elements?(Y/N):";
            cin>>ch;
    system("cls");
    }

}