Saturday 8 February 2014

Problem statement: Reverse a list in single traversal in C


 #include<stdio.h>  
 struct node  
 {  
   int data;  
   struct node *link;  
 };  
 void addNode(int data,struct node **rt)  
 {  
   struct node *temp=(struct node *)malloc(sizeof(struct node));  
   temp->data = data;  
   temp->link = (*rt);  
   (*rt)=temp;  
   printf("%d\n",(*rt)->data);  
 }  
 struct node *reverse(struct node **rt)  
 {  
   struct node *prev,*curr,*next;  
   prev= NULL;  
   curr=(*rt);  
   while(curr!=NULL)  
   {  
     printf("%d\n",curr->data);  
     next = curr->link;  
     curr->link=prev;  
     prev=curr;  
     curr=next;  
   }  
   return prev;  
 }  
 int main()  
 {  
   struct node *root=(struct node *)malloc(sizeof(struct node));  
   root->data = -1;  
   root->link=NULL;  
   addNode(5,&root);  
   addNode(4,&root);  
   addNode(9,&root);  
   addNode(1,&root);  
   addNode(3,&root);  
   addNode(8,&root);  
   addNode(10,&root);  
   addNode(13,&root);  
   addNode(2,&root);  
   struct node *head=(reverse(&root))->link;  
   while(head!=NULL)  
   {  
     printf("%d->",head->data);  
     head=head->link;  
   }  
 }  

Problem statement: Find a mid of list in single traversal in C


 #include<stdio.h>  
 struct node  
 {  
   int data;  
   struct node *link;  
 };  
 struct node *root;  
 void addNode(int val)  
 {  
   struct node *temp = (struct node *)malloc(sizeof(struct node));  
   temp->data = val;  
   temp->link = NULL;  
   if(root == NULL)  
   {  
     root = temp;  
     printf("%d\n",root->data);  
   }    
   else  
   {  
     struct node *ptr = root;  
     while(ptr->link !=NULL)  
     {  
       ptr = ptr->link;  
     }  
     ptr->link = temp;  
     printf("%d\n",ptr->link->data);  
   }  
 }  
 void findmid()  
 {  
   struct node *start , *end;  
   start = end = root;  
   while(end->link !=NULL)  
   {  
     start = start->link;  
     end = end->link;  
     if(end->link!=NULL)  
       end = end->link;  
   }  
   printf("\nMid element is %d\n",start->data);    
 }  
 int main()  
 {  
   addNode(4);  
   addNode(14);  
   addNode(54);  
   addNode(1);  
   addNode(23);  
   addNode(89);  
   addNode(56);    
   addNode(30);  
   addNode(55);  
   addNode(100);  
   //addNode(77);  
   findmid();  
 }  

problem statement: Given n, display nth node in a list in C.


 problem statement: Given n, display nth node in a list in C.  
 #include<stdio.h>  
 struct node  
 {  
   int data;  
   struct node *link;  
 };  
 struct node *root;  
 void addNode(int val)  
 {  
   struct node *temp = (struct node *)malloc(sizeof(struct node));  
   temp->data = val;  
   temp->link = NULL;  
   if(root == NULL)  
   {  
     root = temp;  
     printf("%d\n",root->data);  
   }    
   else  
   {  
     struct node *ptr = root;  
     while(ptr->link !=NULL)  
     {  
       ptr = ptr->link;  
     }  
     ptr->link = temp;  
     printf("%d\n",ptr->link->data);  
   }  
 }  
 void displaynthnode(int pos)  
 {  
   struct node *start , *move;  
   start = move = root;  
   int cnt = 0;  
   while(cnt != pos-1)  
   {  
     move = move->link;  
     cnt++;  
   }  
   while(move->link!=NULL)  
   {  
     start = start->link;  
     move = move->link;  
   }  
   printf("\nElement at %d position is:%d\n",pos,start->data);  
 }  
 int main()  
 {  
   int pos;  
   addNode(4);  
   addNode(14);  
   addNode(54);  
   addNode(1);  
   addNode(23);  
   addNode(89);  
   addNode(56);    
   addNode(30);  
   addNode(55);  
   addNode(100);  
   printf("\nEnter the element position:");  
   scanf("%d",&pos);  
   displaynthnode(pos);  
 }