Friday 11 October 2013

How to reverse one way link list

Reversing link one way list is also a tough job for beginners. So to make it easy to understand we provide you A Flash.. IT mention each steps to followed to do this.
Download This flash tutorial

            C program :-




 #include<stdio.h>  
 #include<malloc.h>  
 #include<stdlib.h>  
 struct node  
 {  
      int data;  
      struct node *link;  
 };  
 typedef struct node node;  
 main()  
 {  
      int i,j;  
      node *p;  
      p=NULL;  
   int n,m,c;  
   printf("\nselect..\n\n\t0 to creat link list");       
      printf("\n\t 1 for inserion");       
   printf("\n\t 2 for reversing link list");  
   printf("\n\t 3 for exit\n\n");  
   while(1)  
   {  
        printf("\n enter ur choice :: ");  
        scanf("%d",&n);  
        switch(n)  
        {  
             case 0:  
                  printf("\n no of element of link list ::");  
                  scanf("%d",&j);  
                  for(i=0;i<j;i++)  
                  {  
                  printf("\n enter element ::");  
                  scanf("%d",&m);  
                   create(&p,m);  
                }  
                display(&p);  
                break;  
             case 1:  
                  printf("\n enter no u wan't to insert ::");  
                  scanf("%d",&m);  
                  append(&p,m);  
                  break;  
             case 2:  
                   reverse(&p);  
                   display(&p);  
                   break;  
             case 3:  
                  printf("\n exit \n");  
                  exit(0);  
                  break;                  
        }  
   }  
 }  
 create(node **q,int num)  
 {  
      node *temp;  
      temp=*q;  
      if(*q==NULL)  
      {  
           temp=(node*)malloc(sizeof(node));  
           temp->data=num;  
           temp->link=NULL;  
           *q=temp;  
      }  
      else  
      {  
      temp=(node*)malloc(sizeof(node)); // creating new node dynamicaly  
      temp->data=num;  
      temp->link=*q;  
      *q=temp;  
      }  
 }  
 int append(node **q,int num) // inserting data at end of list  
 {  
      node *temp,*r;  
      if(*q == NULL)  
      {  
                 temp=(node*)malloc(sizeof(node));  
                 temp->data=num;  
          temp->link=*q;  
                *q=temp;  
                display(q);  
      }  
      else  
      {  
           temp=*q;  
           while(temp->link!=NULL)  
           {  
                temp=temp->link;  
           }  
           r=(node*)malloc(sizeof(node));  
           r->data=num;  
           r->link=NULL;  
           temp->link=r;  
           display(q);  
      }  
 }  
 int reverse(node **q) // to reverslink list  
 {  
      node *temp,*r,*s;  
      temp=*q;  
      r=NULL;  
      while(temp!=NULL)  
      {  
           s=r;  
           r=temp;  
           temp=temp->link;  
           r->link=s;  
      }  
      *q=r;  
 }  
 int display(node **q)  
 {  
      node *temp;  
      temp=*q;  
      printf("\n data element of list are :");  
      while(temp != NULL)  
        {  
        printf(" %d ",temp->data);  
        temp=temp->link;  
     }  
 }  

OUTPUT :-


No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets