Delete from Beginning in Single Linked List Using C

Description

 This program is used to delete elements from the beginning of a list in Single Linked List data structure.

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

struct node
{
 int data;
 struct node* next;
};
struct node *head=NULL;

void create();
void delBeg();
void print();

void main()
{
  int choice;
  do
  {
   printf("\n__MENU__\n1.Create List\n2.Delete from the beginning.\n3...EXIT...\n");
   printf("\nENTER your choice\n");
   scanf("%d",&choice);
   switch(choice)
   {
    case 1: create();break;
    case 2 :delBeg();break;
    case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break;
   default:printf("INVALID input !!!");break;   
  }
  }while(choice!=3);
}
void delBeg()
{
    struct node*temp;
    temp=head;
    if(head==NULL)
    printf("LIST is empty\n");
    else
    {
     head=temp->next;
     free(temp);
 }
 if(head!=NULL)
 print();
}

void create()
{
 int x, i,n;
 head=NULL;
 struct node* newnode,*temp;
 temp=head;
 printf("Enter no. of elements in the LIST \n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  temp=head; 
  newnode=(struct node*)malloc(sizeof(struct node*)); 
  scanf("%d",&x);
     newnode->data=x;
     if(head==NULL)
     {
      
      head=newnode;
      newnode->next=NULL;
  }
     else
  {
         while(temp->next!=NULL)
   temp=temp->next;
   temp->next=newnode;
   newnode->next=NULL; 
  }
 }
 print();
}

void print()
{
 struct node* temp;
 temp=head;
 if(head==NULL)
 printf("list is empty !!!\n\n");
 else
 {
 
  printf("\nLIST elements are\t");
  while(temp!=NULL)
  {
   printf("%d\t",temp->data);
   temp=temp->next;
  }
 }
}


OUTPUT

Delete From Beginning Output 1 Delete From Beginning Output 2

Labels: ,