Ordered Singly Linked List program #1

26. Write a menu driven program to implement following operations on the ordered singly linked list.
i. Insert a node such that linked list is in ascending order. (According to info. field)
ii. Delete a first node of the linked list.
iii. Delete a last node of the linked list.
iv. Delete a node from specified position.

#include<stdio.h>
#include<stdlib.h>
struct node
{
     int info;
     struct node * link;
};
typedef struct node Node;
Node * insert_in_order(int x);
Node * delete_from_first();
Node * delete_from_last();
Node * delete_from_specified(int p);
void display();
Node * first;
int a,count=0;
void main()
{
     int x,ch,p;
 
     printf ("SINGLY LINKED LIST OPERATIONS\n");
     while (1)
     {
        printf ("------------------------------------------\n");
        printf ("      1    -->    INSERT IN ORDERED             \n");
        printf ("      2    -->    DELETE FROM FIRST             \n");
        printf ("      3    -->    DELETE FROM LAST             \n");
        printf ("      4    -->    DELETE FROM SPECIFIED             \n");   
        printf ("      5    -->    DISPLAY               \n");
        printf ("      6    -->    EXIT           \n");
        printf ("------------------------------------------\n");
        printf ("Enter your choice\n");
        scanf    ("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Enter data which you want to add\n");
            scanf("%d",&x);
            first=insert_in_order(x);
           
            break;
        case 2: 
             first=delete_from_first();
            
             break;
        case 3: 
             first=delete_from_last();
            
             break;
        case 4: 
             printf("From which position you want to delete\n");
             scanf("%d",&p);
             first=delete_from_specified(p); 
            
             break;
        case 5:
            display();
            break;
        case 6:
            return;
        default :
            printf("Unvalid entered choice, please enter choice between 1 to 6.\n");
        }
       
     }
    
}

Node * insert_in_order(int x)
{
     Node * New,*save;
     New=(Node * )malloc(sizeof(Node));
     if(New=='\0')
     {
          printf("Memory is not available\n");
          return(first);
     }
    else
     {
          New->info=x;
          count=count+1;
          if(first=='\0')
          {
               New->link='\0';
               return(New);
          }
          if(New->info<=first->info)
          {
               New->link=first;
               return(New);
          }
          save=first;
          while(save->link!='\0' && New->info>=(save->link->info))
          {
               save=save->link;
          }
          New->link=save->link;
          save->link=New;
          return(first);
       }
}

Node * delete_from_first()
{
     Node * save;
     if(first=='\0')
     {
          printf("Underflow");
          return(first);
     }
     else
     {
          save=first;
          first=first->link;
          count--;
          a=save->info;
          printf("Deleted = %d\n",a); 
          free(save);
          return(first);
     }
}

Node * delete_from_last()
{
     Node * save,*pred;
     if(first=='\0')
     {
          printf("Underflow");
          return(first);
     }
     else
     {
          save=first;
          if(save->link=='\0')
          {
               first='\0';
               a=save->info;
               printf("Deleted = %d\n",a);
               return(first);
          }
          while(save->link!='\0')
          {
               pred=save;
               save=save->link;
          }
          pred->link='\0';
          count--;
          a=save->info;
          printf("Deleted = %d\n",a); 
          free(save);
          return(first);
      }
}

Node * delete_from_specified(int p)
{
     int i=1;
     Node * save,*x,*pred;
     x=first;
     save=first;
     while(i<p)
     {
          save=save->link;
          x=save;
          i++;
     }
     if(first=='\0')
     {
          printf("List is empty\n ");
          return(first);
     }
     save=first;
     while(save!=x && save->link!='\0')
     {
          pred=save;
          save=save->link;
     }
     if(save!=x)
     {
          printf("Node not found\n");
          printf("enter position between 1 to %d\n",count);
          return(first);
     }
     if(x==first)
     {
          first=first->link;
          a=x->info;
          count=count-1;
          printf("\t%d Is Deleted From %d position\n",a,p);
          return(first);
     }
     else
     {
          pred->link=x->link;
          a=x->info;
          count=count-1;
          printf("\t%d Is Deleted From %d position\n",a,p);
          return(first);
     } 
     free(x);   
}


void display()
{
     Node * temp;
     temp=first;
     if(first=='\0')
     {
          printf("Link list is empty.\n");
     }
     else
     {
          printf ("\nThe status of the Linked list is \n");
          while(temp!='\0')
          {
               printf("-->%d\t",temp->info);
               temp=temp->link;
          }
          printf("\n total element is %d (position of last element)\n",count);
     }
}

Output

  1. SINGLY LINKED LIST OPERATIONS
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    11
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    14
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    12
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    15
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    13
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    5

    The status of the Linked list is
    -->11    -->12    -->13    -->14    -->15   
     total element is 5 (position of last element)
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    2
    Deleted = 11
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    5

    The status of the Linked list is
    -->12    -->13    -->14    -->15   
     total element is 4 (position of last element)
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    3
    Deleted = 15
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    5

    The status of the Linked list is
    -->12    -->13    -->14   
     total element is 3 (position of last element)
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    4
    From which position you want to delete
    2
        13 Is Deleted From 2 position
    ------------------------------------------
          1    -->    INSERT IN ORDERED            
          2    -->    DELETE FROM FIRST            
          3    -->    DELETE FROM LAST            
          4    -->    DELETE FROM SPECIFIED            
          5    -->    DISPLAY              
          6    -->    EXIT          
    ------------------------------------------
    Enter your choice
    6







For download this program please check the link ➡➡ Download

Comments

Popular

Basic Program #3

Pointer Program #1

Array Program #4

Singly Linked List program #1

Basic program #2