Wednesday, May 22, 2013

C Program to delete the node in the linked list where you will be given the node to delete but not the head pointer .

1->2->3->4->5->6->7->NULL

Consider the above linked list and you are given the node 4 and you have to delete that node . In this scenario you don't have the head pointer . At some point you might think that this is not possible . Because we need to make its previous node 3 to point it to the node 5  like this      1->2->3->5->6->7->NULL and you dont have reference to node 3 . Think about it .......

Is there any way to do it !!! ??? Yes there is a way to do it .

Steps are given below .

1. You have reference to node 4 and you can traverse nodes 5,6,7 . Now copy the data from node 5 and store it in node 4 . So it will become
1->2->3->5->5->6->7->NULL . 
2.Now delete the 5th node . you have its previous node and next node . so deleting will be simple .
1->2->3->5->6->7->NULL.
But there is an Exception 
Deleting the last node is not possible .If the linked list is 1->2->3->4->NULL and if you want to delete 4th node its not possible .

Code :(in C)

void deleteNode(struct node* node)
{
if(node==null || node->link==null)
return;
struct node *temp;
node->data=node->link->data;
temp=node->link->link;
free(node->link);
node->link=temp;
}
view raw deletenode.c hosted with ❤ by GitHub

Please let me know if you have any questions .

Monday, May 6, 2013

Iterative code for Inorder Traversal in C - Binary Tree

Iterative code for inorder traversal uses Stack to print the tree in inorder. The idea behind this code is to push the elements into stack , popping and printing it . 

Steps are given below 

     1. Get the root element and left side children and push it into stack .
     2. Pop the element from stack , print it and check whether is has right child or not . If it has push right child and its left children into stack . 
     3. Continue step 2 until there is no element in stack . 


For the above diagram inorder traversal is 

1 2 3 4 5 6 7 

Code :(in C)
void inorderTraversal(struct node *root)
{
if(root==null)
return null;
struct node *temp=root,temp1;
while(true)
{
while(temp!=null)
{
push(temp);
temp=temp->left;
}
temp1=pop();
if(temp1==null)
break;
printf("%d ",temp1->data);
if(temp1->right!=null)
temp=temp1->right;
}
}
view raw inorder.c hosted with ❤ by GitHub

Please let me know if you have any questions . 

Thursday, April 18, 2013

Simple C Program to get the kth node from last in a linked list

This can be done in single traversal using two pointers . First move to the kth node from first and point it using temp pointer . Point the head node using another pointer called kthnodefromlast .Now Move the temp pointer and kthnodefromlast to next node till temp node reaches the end of the linked list.

Example:

 1->2->3->4->5->6->7->NULL

 For the above linked list kth(k=3) node from last is 5.

 Code:(in C)
struct node* getKthNodeFromLast(struct node* head , int k)
{
if(head==null || k<1)
return null;
struct node* temp=head,*kthnodefromlast=head;
while(--k>0)
{
temp=temp->link;
if(temp==null)
return null;
}
while((temp=temp->link)!=null)
kthnodefromlast=kthnodefromlast->link;
return kthnodefromlast;
}
view raw kthfromlast.c hosted with ❤ by GitHub
Time Complexity : O(n)
Space Complexity: O(1)

 Please let me know if you have any questions .