Thursday, 14 May 2015

Single Linked list (FULL)


#include<stdio.h>
#include<stdlib.h>

struct node
{
   int info;
   struct node *link;
   
};
typedef struct node *NODE;
NODE insert_front(NODE);
NODE insert_end(NODE);
NODE delete_front(NODE);
NODE delete_end(NODE);
void display(NODE);

main(){
   
   int num,choice;
   NODE first=NULL,temp;
   
   printf("Enter first element : ");
   scanf("%d",&num);
   
   temp=(NODE ) malloc(1*sizeof(NODE));
   
   temp->info=num;
   temp->link=first;
   
   first=temp;
   
   do
   {
       printf("\nMENU\n1.INSERT AT FRONT\n2.INSERT AT REAR\n3.DELETE AT FRONT\n4.DELETE AT REAR \n5.DISPLAY\n6.EXIT\nEnter your choice : ");
       scanf("%d",&choice);
       
       switch(choice)
       {
           
           case 1:
                     first=insert_front(first);
                     printf("\n successfully Inserted\n");
                   break;
           case 2:
                     first=insert_end(first);
                   printf("\n successfully Inserted\n");
                   break;
           case 3:
                     first=delete_front(first);
                      
                      break;
          case 4:
                     first=delete_end(first);
                     
                     break;
           case 5:
                    display(first);
                    break;
                    
           case 6:
                       printf("\n EXITED \n");
                        exit(0);
           default :
                      printf("\n wrong choice!! Try again");   
       }
       
   }while(choice!=6);
   
}
NODE insert_front(NODE first)
{
   NODE temp;
   temp=(NODE) malloc(1*sizeof(NODE));
   
   printf("Enter number to insert : ");
   scanf("%d",&(temp->info));
   temp->link=first;
   
   return temp;
}
NODE insert_end(NODE first)
{
   NODE temp,cur=first;
   temp=(NODE )malloc (1*sizeof(NODE));
   
       printf("Enter number to insert : ");
   scanf("%d",&(temp->info));
   temp->link=NULL;
   
   while(cur->link!=NULL)
   {
       cur=cur->link;
   }
   cur->link=temp;
   
   return first;
}
NODE delete_front(NODE first)
{
   if(first==NULL)
   {
       printf("\nLinked List is empty \n");
       return;
   }
   NODE temp;
   temp=first->link;
   
   free(first);
   printf("\n DELETED\n");
   
   return temp;
   
}
NODE delete_end(NODE first)
{
   if(first==NULL)
   {
       printf("\n Linked List is empty \n");
       return;
   }
   NODE temp=first,cur=temp;
   
   printf("\n DELETED\n");
   
   //this condition if only one node is present and it is necessary
   if(temp->link==NULL)
   {
       free(temp);
       first=NULL;
       return first;
   }
   while(temp->link!=NULL)
   {
       cur=temp;
       temp=temp->link;
   }
   cur->link=NULL;
   free(temp);
   
   return first;
   
}
void display(NODE first)
{
   NODE temp=first;
   
    if(temp==NULL)
    {
       printf("\n Linked list is empty \n");
       return;
    }
   while(temp!=NULL)
   {
      printf("%d-->",temp->info);
      temp=temp->link;
       
   }
   printf("NULL");
}


No comments:

Post a Comment