Sunday, March 10, 2013

Circular Linked List


Circular Linked List

A linked list where the link field of last node contains address of first node is called a circular linked list .It is represented as follows.

Circular linked list have certain advantageous over single linked list.


1.  Traversing is simple.

           In a single linked list, it is not possible to traverse the entire linked list starting from any node. In other words, if we start from first node only, we can visit all nodes. Starting from the middle node we cannot visit all nodes. However in circular linked list one can traverse the entire linked list starting from any node.

2.      Deletion requires less number of inputs in circular linked list over  single
linked list.

            In single linked list if we wish to delete a node, we require the address of that node and also we require the address of first node. This is because the predecessor of the node to be deleted had to be kept track off. However, in circular linked list, if we want to delete a node we require the address of that node. However, we do not require the address of first node. Since the initialization of the temp pointer which has to point to the predecessor node is made at deleted node itself  i.e. In single linked list if we require two address, one X, which is the address of node deleted, second is first, this is required because temp is initialized to first and moved it tempàlink=X.

            Where as in circular linked list we require only one address of X i.e. X, which is the address of node to be deleted. Here temp is initialized to X itself and moved till tempàlink=X.
           
            However there is a disadvantageous in circular linked list. If one is not careful while processing a circular linked list, then we can end up in an infinite loop. In circular linked list it is not possible to distinguish last node from other nodes since its link field also contain address .To over come  this we use a special node called head node at beginning of the linked list .The head node link field points to first node and first link field of last node contain address of head node. The data field of head node is unused. The circular linked list with head node is as follows:



Write C program to Delete nodes at Beginning, Middle and at End using functions in Single Linked List


Write C program to Delete nodes at Beginning, Middle and at End using functions

  #include <stdio.h>
  struct node
  {
   int data;
   struct node*link;
  };

  struct node * getnode()
  {
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node));
    printf("enter data into the node:");
    scanf("%d",&p->data);
    p->link=NULL;
    return p;
  }

  int  countnodes(struct node *f)
   {
    struct node *temp;
    int ct=0;
    temp=f;
    while(temp!=NULL)
    {
      ct++;
      temp=temp->link;
    }
    return ct;
  }

  struct node * createnodes()
  {
    struct node *first,*t,*temp;
    char ch='Y';
    int ct=0;
    while(ch=='Y')
    {
      if(ct==0)
      {
       first=getnode();
       ct++;
       temp=first;

      }
      else
      {
       temp=first;
       while(temp->link!=NULL)
       {
            temp=temp->link;
       }
       t=getnode();
       temp->link=t;
       temp=t;
      }
      printf("do you want to continue:");
      fflush(stdin);
      ch=getchar();
     }
     return first;
   }

   void displaylist (struct node *f)
   {
     struct node *temp;
     temp=f;
     while(temp!=NULL)
     {
      printf("%d-->",temp->data);
      temp=temp->link;
     }
     printf("NULL\n");
  }

/* struct node * deletefirstnode(struct node *first)
  {
     struct node *temp;
     temp=first;
     first=temp->link;
     free(temp);

   return first;
  }
  struct node * deletelastnode(struct node *first)
   {
    struct node *temp,*temp1;
    temp=first;
    temp1=first;
    while(temp->link!=NULL)
    {
     temp=temp->link;
    }
    while(temp1->link!=temp)
    {
     temp1=temp1->link;
    }
     temp1->link=NULL;
     free(temp);
     return first;

   }

   struct node *deletemiddlenode(struct node *first, int pos)
   {
     struct node *temp,*temp1;
     int i;
     temp=first;
     temp1=first;
     for(i=1;i<pos;i++)
     {
      temp=temp->link;
     }
     for(i=1;i<pos-1;i++)
     {
      temp1=temp1->link;
     }
     temp1->link=temp->link;
     free(temp);
     return first;
    }    */

    struct node * deletenodes(struct node *f,int pos)
    {
     struct node *t,*t1;
     int ct,i;
     ct=countnodes(f);
     t=f;
     t1=f;
     if(ct<pos)
     {
      printf("please cheak number of nodes & position to deleted\n");


     }

     else if(pos==1)
     {
      f=f->link;
      free(t);
     }
     else if(pos==ct)
     {
      for(i=1;i<pos;i++)
      {
       t=t->link;
      }
      for(i=1;i<pos-1;i++)
      {
       t1=t1->link;
      }
      t1->link=NULL;
      free(t);
     }
     else
     {
      for(i=1;i<pos;i++)
      {
       t=t->link;
      }
      for(i=1;i<pos-1;i++)
      {
       t1=t1->link;
      }
      t1->link=t->link;
      free(t);
     }
     return f;
   }

   void main()
   {
     int ct,n;
     struct node *first;
     clrscr();
     first=createnodes();
     displaylist(first);
     ct=countnodes(first);
     printf("number of nodes=%d\n",ct);
    /* first=deletefirstnode(first);
     displaylist(first);
     first=deletelastnode(first);
     displaylist(first);
     first=deletemiddlenode(first,4);*/
     printf("enter which node to be deleted\n");
     scanf("%d",&n);
     first=deletenodes(first,n);
     displaylist(first);
     getch();
  }

Write C program to Create, insert nodes at Beginning, Middle and at End using functions in Single Linked List


Write C program to Create, insert nodes at Beginning, Middle and at End using functions

  #include <stdio.h>
  struct node
  {
   int data;
   struct node*link;
  };

  struct node * getnode()
  {
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node));
    printf("enter data into the node:");
    scanf("%d",&p->data);
    p->link=NULL;
    return p;
  }

  int  countnodes(struct node *f)
   {
    struct node *temp;
    int ct=0;
    temp=f;
    while(temp!=NULL)
    {
      ct++;
      temp=temp->link;
    }
    return ct;
  }

  struct node * createnodes()
  {
    struct node *first,*t,*temp;
    char ch='Y';
    int ct=0;
    while(ch=='Y')
    {
      if(ct==0)
      {
       first=getnode();
       ct++;
       temp=first;

      }
      else
      {
       temp=first;
       while(temp->link!=NULL)
       {
            temp=temp->link;
       }
       t=getnode();
       temp->link=t;
       temp=t;
      }
      printf("do you want to continue:");
      fflush(stdin);
      ch=getchar();
     }
     return first;
   }

   void displaylist (struct node *f)
   {
     struct node *temp;
     temp=f;
     while(temp!=NULL)
     {
      printf("%d-->",temp->data);
      temp=temp->link;
     }
     printf("NULL\n");

   }

   struct node * AddnodeFirst(struct node *f)
   {
     struct node *t;
     t=getnode();
     t->link=f;
     f=t;
     return f;

   }
   struct node * addnodeLast(struct node *f)
   {
     struct node *t,*temp;
     t=getnode();
     temp=f;
     while(temp->link!=NULL)
     {
       temp=temp->link;
     }
     temp->link=t;

     return f;
    }

   struct node * addnodemiddle(struct node *f,int pos)
   {
     struct node *temp,*t;
     int i;
     temp=f;
     for(i=1;i<pos-1; i++)
     {
       temp=temp->link;
     }
     t=getnode();
     t->link=temp->link;
     temp->link=t;
     return f;
   }


  void main()
  {
     int ct;
     struct node *first;
     clrscr();
     first=createnodes();
     displaylist(first);
     ct=countnodes(first);
     printf("number of nodes=%d\n",ct);
     first=AddnodeFirst(first);
     displaylist(first);
     first=addnodeLast(first);
     displaylist(first);
     first=addnodemiddle(first,3);
     displaylist(first);
     getch();
  }


Note: Instead of Addnodefirst, addnodelast and addnodemiddle function use InsertNodes function which as the functionality of above three functions

struct node * InsertNodes(struct node *f,int pos)
   {
     struct node *temp,*t;
     int i,ct;
     ct=countnodes(f);
     if (pos==1)
     {
       t=getnode();
       t->link=f;
       f=t;
     }
     else if (pos>ct)
     {
       temp=f;
       while(temp->link!=NULL)
       {
             temp=temp->link;
       }
       t=getnode();
       temp->link=t;
     }
     else
     {
       for(i=1;i<pos-1; i++)
       {
             temp=temp->link;
       }
       t=getnode();
       t->link=temp->link;
       temp->link=t;
     }
     return f;
   }

Write C program to create and print Single linked list using functions


Write C program to create and print linked list using functions

struct node
{
   int data;
   struct node *link;
};

struct node * getnode()
{
  struct node *p;
  p=(struct node *)malloc(sizeof(struct node));
  printf("Enter data into the node: ");
  scanf("%d", &p->data);
  p->link=NULL;
  return p;
}


struct node * createnodes()
{
   struct node *first,*t,*temp;
   char ch='y';
   int sct=0;
    while (ch=='y')
   {
      if (sct==0)
      {
              first=getnode();
              temp=first;
              sct++;
      }
      else
      {
            temp=first;
            while(temp->link!=NULL)
            {
               temp=temp->link;
            }
            t=getnode();
            temp->link=t;
            temp=t;
      }
      printf("Do you want to continue: ");
      fflush(stdin);
      ch=getchar();
   }
   return first;
}



void slinkdisplay(struct node *first)
{
  struct node *temp;
  temp=first;
    while (temp->link!=NULL)
    {
      printf("%d --> ",temp->data);
      temp=temp->link;
    }
    printf("%d -->NULL",temp->data);
}



void main()
{
   struct node *f;
   clrscr();
   f=createnodes();
   slinkdisplay(f);
   getch();
}

Write C program to create and print Single linked list without using function


Write C program to create and print linked list without using function

struct node
{
   int data;
   struct node *link;
};

main()
{
  struct node *first,*temp;
  char ch=’y’;
  int ct=0;
//Creates Linked List 
  while (ch==’y’)
  {
        if (ct==0)
        {
           first=(struct node *)malloc(sizeof(struct node));
           printf("Enter data into the node: ");
           scanf("%d", &first->data);
           first->link=NULL;
           temp=first;
           ct++;
        }
        else
        {
           temp=first;
              while(temp->link!=NULL)
              {
                 temp=temp->link;
              }
             t=(struct node *)malloc(sizeof(struct node));
             printf("Enter data into the node: ");
             scanf("%d", &t->data);
             t->link=NULL;
             temp->link=t;
        }
      printf("Do you want to continue to create another node: ");
      fflush(stdin);
     ch=getchar();
   }

// Displaying Linked List
  temp=first;
    while (temp->link!=NULL)
    {
      printf("%d --> ",temp->data);
      temp=temp->link;
    }
    printf("%d -->NULL",temp->data);
 
 getch();
}

Linked List


Linked List

            In  a linked list, the elements are not stored in continuous memory locations i.e., the elements of linked list are non-continuous.  A linked list is represented as follows:
     Since the elements are non-continuous, the next element is stored in current element i.e., the address of second element is stored in first element.  The address of third element is stored in second element and so on.  In general, address of (i+1)th is stored in ith element.  This address is called Link or Pointer and  element is called Data. The combination Data and Link is called Node.  Typically, node is shown as follows:

  First field in the node is Data Field and Second field is link field.  The link field contains address of the next node.  In the above linked list, the address of second node is 2000, which is stored in link field of Ist node.  The address of the last node is 7000, which is stored in link field of last but one node.  The link field of the last node contains ‘Nil’, which  indicates the end of Linked List.  Nil is the pointer constant.  The address of the first node is stored in the pointer variable “First”.  First always points to the “First Node”.  In other words, ‘First’ is the pointer to the entire linked list.


 Operations on Linked List

Insertion
Case  1.  Insertion in the middle

            Whenever any particular node is to be inserted, all we need to do is change the link field of preceding node to point to new node and link field of new node point to succeeding node.  For example, if we want to insert a new node between ith and (i+1)th node, point ith node link to new node and new node link to (i+1)th node.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

                                                                 Before Insertion
                                                                  After Insertion
 
Case 2: Insertion at the Beginning

            Suppose, if we want to insert a new node at the beginning of Linked List, point new node link to first node and change “First” Pointer to new node.

Before Insertion
After Insertion

Case 3: Insertion at the End

            Suppose, if  we want to insert a new element at the end of the linked list, change link field of the last node to point to new node and new node link field contain Nil.
 Before Insertion


 After Insertion

Deletion

Case  1.  Deleting middle node from Linked List
           
            To delete ith node form linked list, change link field of (i-1)th node to (i+1)th node.

Before Deletion
After Deletion 

Case  2.  Deleting first node from Linked List

            To Delete first node, move “first’ pointer to next node.




Before Deletion
 After Deletion

Case  3.  Deleting Last node from the Linked List

            To delete last node change link field of last but one node to contain ‘Nil’

 Before Deletion
After Deletion

Thus for insertion we have to modify two links and for deletion we have to modify one link.

Creating a Node:

            To create a new node we have to use function called “getnode()”. This function creates a new node and ‘p’ is pointer points to that node.   Whenever we want to insert a node first we have to create it.   malloc() is a function which will allocate memory of size “struct node”.  free() is a function will de-allocate memory of node.

To declare a node  in ‘C’,  following is the syntax

struct node
{
   int data;
   struct node *link;
};

struct node * getnode()
{
  struct node *p;
  p=(struct node *)malloc(sizeof(struct node));
  printf("Enter data into the node: ");
  scanf("%d", &p->data);
  p->link=NULL;
  return p;
}

To access each field in the node, we denote p->data to data field and p->link to link field