Singly Linked List program #2

24. Write a menu driven program to implement following operations on the singly linked list.
i. Insert a node at the front of the linked list.
ii. Display all nodes.

#include<stdio.h>
#include<stdlib.h>
struct node
{
     int info;
     struct node * link;
};
typedef struct node Node;
Node * insert_at_first(int x);
void display();
Node * first='\0';
void main()
{
     int x,ch;
   
     printf ("SINGLY LINKED LIST OPERATIONS\n");
     while (1)
     {
        printf ("------------------------------------------\n");
        printf ("      1    -->    INSERT AT FIRST              \n");
        printf ("      2    -->    DISPLAY               \n");
        printf ("      3    -->    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_at_first(x);
           
            break;
        case 2:
            display();
            break;
        case 3:
            return;
        default :
            printf("Unvalid entered choice, please enter choice between 1 to 3.\n");
        }
       
     }
    
}

Node * insert_at_first(int x)
{
     Node * New;
     New=(Node * )malloc(sizeof(Node));
     if(New=='\0')
     {
          printf("Memory is not available\n");
          return(first);
     }
    else
     {
          New->info=x;
          New->link=first;
          return(New);
     }
}

void display()
{
     Node * temp;
     temp=first;
     if(temp=='\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");
     }
}

Output

  1. SINGLY LINKED LIST OPERATIONS
    ------------------------------------------
          1    -->    INSERT AT FIRST             
          2    -->    DISPLAY              
          3    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    11
    ------------------------------------------
          1    -->    INSERT AT FIRST             
          2    -->    DISPLAY              
          3    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    12
    ------------------------------------------
          1    -->    INSERT AT FIRST             
          2    -->    DISPLAY              
          3    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    13
    ------------------------------------------
          1    -->    INSERT AT FIRST             
          2    -->    DISPLAY              
          3    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    14
    ------------------------------------------
          1    -->    INSERT AT FIRST             
          2    -->    DISPLAY              
          3    -->    EXIT          
    ------------------------------------------
    Enter your choice
    1
    Enter data which you want to add
    15
    ------------------------------------------
          1    -->    INSERT AT FIRST             
          2    -->    DISPLAY              
          3    -->    EXIT          
    ------------------------------------------
    Enter your choice
    2

    The status of the Linked list is
    -->15    -->14    -->13    -->12    -->11   
    ------------------------------------------
          1    -->    INSERT AT FIRST             
          2    -->    DISPLAY              
          3    -->    EXIT          
    ------------------------------------------
    Enter your choice
    3




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