Showing posts with label Single Linked list Program.. Show all posts
Showing posts with label Single Linked list Program.. Show all posts

Saturday, 9 July 2011

Compare Two Linked Lists

int compare_linked_lists(struct node *q, struct node *r) 
{
static int flag;
if((q==NULL ) && (r==NULL))
{
flag=1;
}
else
{
if(q==NULL || r==NULL)
{
flag=0;
}
if(q->data!=r->data)
{
flag=0;
}
else
{
compare_linked_lists(q->link,r->link);
}
}
return(flag); 
}

Reversing

struct node reverse(struct node * first)
{
struct node* cur,temp;
cur = NULL;
while(first != NULL)
{
temp = first;
first = first->link;
temp->link = cur;
cur = temp;
}
return cur;
}

Finding Nth from Tail.


Node * FindNthFromBack(struct node *Head, int n)
{
struct node *ptr1, *ptr2;  // we need 2 pointers
ptr1 = ptr2 = Head; // set the pointers to point to the list head initially

while(ptr1->link != NULL) // keep looping until we reach the tail (next will be NULL for the last node)
{
if(n > 0)
{
ptr1 = ptr1->link; //increment only the 1st pointer
n--;
}
else
{
ptr1 = ptr1->link; //increment both pointers
ptr2 = ptr2->link;
}
}
return ptr2;    //now return the ptr2 which points to the nth node from the tail
}

Finding the middle element.


struct node * FindMiddle(struct node *Head)
{
struct node *ptr1, *ptr2;  // we need 2 pointers
ptr1 = ptr2 = Head; // set the pointers to point to the list head initially
int i=0;
while(ptr1->link != NULL) // keep looping until we reach the tail
                              // (next will be NULL for the last node)
{
if(i == 0)
{
ptr1 = ptr1->link; //increment only the 1st pointer
i=1;
}
else if( i == 1)
{
ptr1 = ptr1->link; //increment both pointers
ptr2 = ptr2->link;
i = 0;
}
}
return ptr2;        //now return the ptr2 which points to the middle node
}

Loop Detection.


bool ListContainsLoop(node * head)
{
node * slowPtr = head;
node * fastPtr = head;
while(slowPtr  && fastPtr)
{
fastPtr = fastPtr->link; // advance the fast pointer
if(fastPtr == slowPtr)   // and check if its equal to the slow pointer
return true;                   // loop detected
if(fastPtr == NULL)
{
return false;        // since fastPtr is NULL we reached the tail
}
fastPtr = fastPtr->link; //advance and check again
if(fastPtr == slowPtr)
return true;
slowPtr = slowPtr->link;  // advance the slow pointer only once
}
return false;                // we reach here if we reach the tail
}

Inserting in the sorted linked list.

// Assuming that Linked List is sorted in ascending order.

void sortedInsert(node * head, node* newNode)
{
node *current = head;
         
// traverse the list until you find item bigger the // new node value
   
while (current!= NULL && current->data < newNode->data)
{
current = current->link);
}

// insert the new node before the big item

newNode->link = current->link;
current = newNode;
}

Sorting the Linked List.

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

Sort( struct node *Head)
{
  struct node* first,second,temp;
  first= Head;
  while(first!=null)
  {
    second=first->link;
    while(second!=null)
    {
      if(first->data < second->data) // sorting in descending order.
      {
        temp = new node();
        temp->data =first->data;
        first->data =second->data;
        second->data =temp->data;
        delete temp;
      }
      second=second->link;
    }
   first=first->link;
  }
}

Friday, 8 July 2011

Simple Single Linked List Program :

# include <stdio.h>
# include <stdlib.h>
struct node //creating a structure called node
{
int data; //Linked list data(value)
struct node *link; // Linked list node(pointer)
};
struct node *insert(struct node *p , int num) //Function to insert a node in linked list
{
struct node *temp;
 if(p==NULL)
{
p=(struct node *)malloc(sizeof(struct node)); //Allocating memory for the new node.


if(p==NULL)
{
printf("Error Occurred\n");
exit(0);
}
        p-> data = num;
p-> link = NULL;
}
else
{
temp = p;
while (temp-> link != NULL) // Loop to traverse to the end of the list
temp = temp-> link;
temp-> link = (struct node *)malloc(sizeof(struct node)); //Allocating memory
if(temp -> link == NULL)
{
printf("Error Occurred\n");
exit(0);
}
temp = temp-> link;
temp-> data = num;
temp-> link = NULL;
}
return (p);
}
void printlist ( struct node *p  )
{
printf("The data values of your list are\n");
while (p!= NULL)
{
printf("%d\t",p-> data);
p = p-> link;
}
}