Pages

Sunday 26 August 2012

Datastructures material page3


 How to traverse singly linked list as like a doubly linkedlist:

That is given a linked list we need traverse till end and then traverse back to start :

note:for all my programs assume head node is available
 
Algorithm:

Every time maintain previous node address and then do XOR operation with current next then move forword do same process till u find current next as null then make previous as null again then traverse
back by using XOR operation of next with previous

Code:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *next;
}*head=NULL;

//for my appliction i need to insert some elements in to linked list that is why im writing insert //function
void insert(struct node *head,int i)
{
    struct node *temp,*q;
    if(head->next==NULL)
    {
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=i;
    temp->next=NULL;
    head->next=temp;
        printf("inserted when head is null");
  
    }
      
    else
    {
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=i;
        temp->next=NULL;
        q=head;
        while(q->next!=NULL)
        q=q->next;
        q->next=temp;
        printf("inserted when head is not null");
      
    }
}//end of insert

void display(struct node *head)
{
    struct node *temp;
    temp=head->next;
    while(temp!=NULL)
    {
        printf("%d    ",temp->data);
        temp=temp->next;
        }
    }

void backtrack(struct node *head)
    {
    struct node *temp,*prev,*q;
    temp=head;
    prev=NULL;
    while(temp!=NULL)
    {
        q=temp;
        printf("%d   ",temp->data);
        temp=temp->next;
        q->next=((int)(q->next))^((int)(prev));
        prev=q;
        }
        temp=prev;
        prev=NULL;
        printf("\t");
        while(temp!=head)
        {
            printf("%d  ",temp->data);
            temp->next=((int)(temp->next))^((int)(prev));
            prev=temp;
            temp=temp->next;
            }
              
        printf("%d",temp->data);
     }  


void main()
{
    int i=1,j;
    struct node *head;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
while(i)
{
    printf("\nenter 1 to insert elements ");
    printf(" \n 2 display");
    printf("\n 3 traverse linked list front and back again");
    printf("\n 0 to exit");
    scanf("%d",&i);
  
 switch(i)
 {
    case 1:printf("enter element");
           scanf("%d",&j);
           insert(head,j);
           break;

    case 2:display(head);
           break;
         
    case 3:backtrack(head->next);
           break;

  }
}
}

Datasructures material page2

                              Datastructures material and frequently asked questions in interview
 
                                                                          page2:

  Finding kth node from last in a linked list
       
   Algorithm:
         
           take two pointer at start of linked list traverse first pointer for k nodes
           and then move both the points untill first pointer reach end of linklist by that time 
           second  pointer will reach to kth node from last
    
   Code:
                     
int findkthlast(struct node *head)
  {
       int i=0,j,k;
       struct node *temp=head->next;

       printf("enter which node u want to find from last");
       scanf("%d",&j);
       while(temp!=NULL)
       {
        i++;
        temp=temp->next;
       }
       temp=head->next;
       for(k=0;k<i-j;k++)
       {
       temp=temp->next;
       }
       printf("%d is %d node from last",temp->data,j);
       return 0;
 }     
 
 How to find loop in linked list
 Algorithm:
     Take pointer at start and move one pointer two nodes at a time and other pointer one node
      if there exist any loop surely at one point of these two pointers will meet at same nodes

 Code:
 
void detectloop(struct node *head)
      {
            struct node *slow,*fast;
            slow=fast=head->next;
            do
            {
                  slow=slow->next;
                  fast=fast->next->next;
             }while(fast!=slow&&fast!=NULL);
                  if(fast==NULL)
                  printf("\n no loop present in given list");
                  else
                  printf("\n loop present in given list");
                 
       }


How remove loop in linked list:
 Algorithm:
   if two students running in loop if one student running 
   at a speed double of other they surely meet at start of 
   of race
   in the same way





   so take two pointer move first pointer two nodes and other 
   one node agian if there is loop these two will meet a point
   then take third pointer at start of linked list and move 
   third and second pointer untill thier next node will be equal 
   so we can simply remove link from second pointer

code:
  
 
void removeloop(struct node *head)
      {
            struct node *slow,*fast,*temp;//temp is third pointer
            slow=fast=head->next;
            do
            {
                  slow=slow->next;
                  fast=fast->next->next;
                  }while(fast!=slow&&fast!=NULL);
                 
            fast=head->next;
            while(fast!=slow)
            {
                  fast=fast->next;
                  temp=slow;
                  slow=slow->next;
                  }
                  temp->next=NULL;
                  printf("loop removed successfully");
            }//end of loop remove


  Inserting in sorted order:

Algorithm:

maintain two pointers for previous and next if we are inserting increse order when find element greter than the given number maintain previous node and keep previous next as newly created node and newly created next as current node 

Code:
 
void insertsort(struct node *head,int i)
{
      struct node *temp,*q,*t;
      if(head->next==NULL)
      {
temp=(struct node *)malloc(sizeof(struct node));
      temp->data=i;
      temp->next=NULL;
      head->next=temp;
      printf("inserted when head is null");
     
    }
           
      else
      {
            temp=(struct node *)malloc(sizeof(struct node));
            temp->data=i;
            temp->next=NULL;
            t=head;
            q=head->next;
            while(i>q->data&&q->next!=NULL)
            {
            t=q;
            q=q->next;
           }
           if(i<=q->data)
           {
           t->next=temp;
           temp->next=q;
            }
            else if(q->next==NULL)
            {
                  q->next=temp;
                  }
            printf("inserted when head is not null");
           
      }
}/