Friday, 4 October 2013

Operation on One way link list

In one way Link List we can travel only along forward direction.

operation on one way link list done in this program:-

INSERTION :- we can insert data at end of list (append) or at beginning of list or at given postion

DELETION :- similarly like insertion deletion can take place at beginning ,at end ,at given position or delete a particular data from the list (in this program we use only last one)

NOTE : to understand algorithm much better download this flash tutorial data_structure


C code :





 // one way link list beginer2cs.blogspot.in  
 #include<stdio.h>  
 #include<malloc.h>  
 #include<stdlib.h>  
 struct node         //defining data type for node  
 {  
      int data;  
      struct node *link;  
 };  
 typedef struct node node; // typedef   
 main()  
 {  
      node *p;  
      p=NULL;  
   int n,m,c;  
      printf("\n select \n\t 1 for inserion at end \n");       
      printf("\n\t 2 for insertion at beggning\n");  
   printf("\n 3 for insertion at given position \n");  
   printf("\n 4 for delete given no from list \n");  
   printf("\n 5 for exit \n");  
   while(1)  // loop continue ucantill we stop using break or exit  
   {  
        printf("\n enter ur choice :: ");  
        scanf("%d",&n);  
        switch(n) // switch to select option  
        {  
             case 1:  
                  printf("\n enter no u wan't to insert ::");  
                  scanf("%d",&m);  
                  append(&p,m);  
                  break;  
             case 2:  
         printf("\n enter no u wan't to insert ::");  
                     scanf("%d",&m);  
                     inatbegg(&p,m);  
                     break;                        
             case 3:  
                  printf("\n enter no u wan't to insert & position::");  
                    scanf("%d %d",&m,&c);  
                    atpos(&p,m,c);  
                    break;  
             case 4:  
                  printf("\n enter no do u want to del\n");  
                  scanf("%d",&m);  
                  del(&p,m);  
                  break;  
             case 5:  
                  printf("\n exit \n");  
                  exit(0);  
                  break;                  
        }  
   }  
 }  
 int inatbegg(node **q,int num) // funtion to insert at begg  
 {  
      node *temp; // declare new node  
           if(*q == NULL)  //if list is emplty add as first node  
      {  
                 temp=(node*)malloc(sizeof(node));  
                 temp->data=num;  
          temp->link=*q;  
                *q=temp;  
                display(q);  
      }  
      else  
      {  
      temp=(node*)malloc(sizeof(node)); // creating new node  
      temp->data=num; // inserting data to node  
      temp->link=*q; //ponting to first node  
      *q=temp;  // make head point to new node  
      display(q);  
   }  
 }  
 int append(node **q,int num) //insertion at end  
 {  
      node *temp,*r; // declare new node  
      if(*q == NULL) //if list is emplty add as first node  
      {  
                 temp=(node*)malloc(sizeof(node));  
                 temp->data=num;  
          temp->link=*q;  
                *q=temp;  
                display(q);  
      }  
      else  
      {  
           temp=*q;  
           while(temp->link!=NULL) // move to last node  
           {  
                temp=temp->link;  
           }  
           r=(node*)malloc(sizeof(node));  
           r->data=num;  
           r->link=NULL;  
           temp->link=r;  
           display(q);  
      }  
 }  
 int atpos(node **q,int num,int pos)  
 {  
      node *temp,*r;  
      temp=*q;  
      int i=1;  
      while(i<pos) //move to given postion  
       {  
        temp=temp->link;  
        if(temp==NULL)  
         {  
         printf("\n this position is beyond sequense\n");  
         return;  
            }  
            i++;  
    }  
      r=(node*)malloc(sizeof(node));  // craete new node  
      r->data=num;  
      r->link=temp->link;  
      temp->link=r;  
      display(q);  
 }  
 int del(node **q,int num)  
 {  
      node *temp,*r;  
      temp=*q;  
      if(temp->data==num) // if first node is required node  
      {  
           *q=temp->link;  
           display(q);  
      }  
      while(temp->link != NULL) // searching for node to be delete  
       {  
            if(temp->link->data == num)  
            {  
                 r=temp->link;  
                 temp->link=r->link;  
                 free(r);  
                 display(q);  
                 return;  
        }  
        else  
        temp=temp->link;  
       }    
       printf("\n no can't found\n");  
 }  
 int display(node **q)  
 {  
      node *temp;  
      temp=*q;  
      printf("\n data element of list are :");  
      while(temp != NULL) // travers & display each dislay data   
        {  
        printf(" %d ",temp->data);  
        temp=temp->link;  
     }  
 }  

OUTPUT :



No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets