Code Scrappers


Singly Linked List C Program


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

                    void Display();

                    void InsertBegin();

                    void DeleteEnd();

                    struct node
                    {
                            int data;

                            struct node *link;
                    };

                    struct node *start=NULL;

                    void main()
                    {
                            int choice;

                            while(1)
                            {

                                    printf("\n singly linked list\n");

                                    printf(" 1.Display  \n");

                                    printf(" 2.Insert at beginning  \n");

                                    printf(" 3.Delete from end \n");

                                    printf(" 4.exit \n");

                                    printf("enter your choice:\t");

                                    scanf("%d",&choice);

                                    switch(choice)
                                    {

                                            case 1:   Display();
                                                      break;

                                            case 2:   InsertBegin();
                                                      break;

                                            case 3:   DeleteEnd();
                                                      break;

                                            case 4:   exit(0);
                                                      break;

                                            default:   printf("\n Incorrect Choice:\n");
                                                      break;
                                    }
                            }
                    }

                    void Display()
                    {
                            struct node *ptr;

                            if(start == NULL)
                            {
                                    printf(" empty:\n");

                            }
                            else
                            {
                                    ptr=start;

                                    printf(" The List elements are:\n");

                                    while(ptr != NULL)
                                    {
                                            printf("%d\t",ptr->data);

                                            ptr=ptr->link ;
                                    }

                            }

                    }

                    void InsertBegin()
                    {
                            struct node *temp;

                            temp=(struct node *)malloc(sizeof(struct node));

                            printf("\n enter the data :\t" );

                            scanf("%d",&temp->data);

                            temp->link = NULL;

                            if(temp == NULL)
                            {
                                    printf("\n no Space:\n");
                            }

                            else if(start==NULL)
                            {
                                    start=temp;
                            }

                            else
                            {
                                    temp->link = start;

                                    start=temp;
                            }
                    }

                    void DeleteEnd()
                    {
                            struct node *temp, *ptr;

                            if(start==NULL)
                            {
                                    printf(" Empty");

                            }
                            else if(start->link == NULL)
                            {
                                    ptr=start;

                                    start=NULL;

                                    printf("\ndeleted element = %d\t",ptr->data);

                                    free(ptr);
                            }
                            else
                            {
                                    ptr=start;

                                    while(ptr->link!=NULL)
                                    {
                                            temp=ptr;

                                            ptr=ptr->link;
                                    }

                                    temp->link = NULL;

                                    printf("\n deleted element = %d\t",ptr->data);

                                    free(ptr);
                            }
                    }


                



Output

   singly linked list

   1.Display
   2.Insert at beginning
   3.Delete from end
   4.exit

   enter your choice:      2

   enter the data :       10

   singly linked list
   1.Display
   2.Insert at beginning
   3.Delete from end
   4.exit

   enter your choice:      2

   enter the data :       20

   singly linked list
   1.Display
   2.Insert at beginning
   3.Delete from end
   4.exit

   enter your choice:      2

   enter the data :       30

   singly linked list
    1.Display
    2.Insert at beginning
    3.Delete from end
    4.exit

   enter your choice:      1

   The List elements are:
   30      20      10

   singly linked list
    1.Display
    2.Insert at beginning
    3.Delete from end
    4.exit

   enter your choice:      3

  deleted element = 10

  singly linked list
    1.Display
    2.Insert at beginning
    3.Delete from end
    4.exit

  enter your choice:      1

  The List elements are:
    30      20

  singly linked list
    1.Display
    2.Insert at beginning
    3.Delete from end
    4.exit

   enter your choice:      4