Write a program to implement Singly Linked List using C programming language

 






#include<stdio.h>

#include<conio.h>
#include<stdlib.h>
void insertAtBeg(int);
void insertAtEnd(int);
void insertBet(int,int);
void display();
void removeBeg();
void removeEnd();
void removeSpecific(int);
struct Node
{
   int data;
   struct Node *next;
}*head = NULL;
void main()
{
   int choice,num,choice1,loc,loc1,loc2;
   clrscr();
   while(1)
   {
    printf("\n\n****** MENU ******");
    printf("\n 1. Insert at begining");
    printf("\n 2. Insert at end");
    printf("\n 3. Insert at specific position");
    printf("\n 4. Deletion at begining");
    printf("\n 5. Deletion at end");
    printf("\n 6. Deletion at specific position");
    printf("\n 7. Display the list");
    printf("\n 8. Exit");
    printf("\n\n Enter your choice: ");
    scanf("%d",&choice);
    switch(choice)
    {
      case 1:
printf("\n Enter the value you want to insert at the begining :");
scanf("%d",&num);
insertAtBeg(num);
break;
      case 2:
printf("\n Enter the value you want to insert  at the end :");
scanf("%d",&num);
insertAtEnd(num);
break;
      case 3:
printf("\n Enter the value you want to insert at specific location :");
scanf("%d",&num);
printf("Enter the position: ");
scanf("%d",&loc);
insertBet(num,loc);
break;
      case 4:
removeBeg();
break;
      case 5:
removeEnd();
break;
      case 6:
printf("Enter the specific location which you want to delete: ");
scanf("%d",&loc);
removeSpecific(loc);
break;
      case 7:
display();
break;
      case 8:
exit(1);
      default:
printf("\nWrong Input!! Try again!!!\n\n");
    }
   }
}
void insertAtBeg(int num)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = num;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }
   printf("\n Node inserted at the begining of the list!!!");
   getch();
}
void insertAtEnd(int num)
{
   struct Node *newNode, *temp;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = num;
   newNode->next = NULL;
   if(head == NULL)
head = newNode;
   else
   {
      temp= head;
      while(temp->next != NULL)
temp = temp->next;
      temp->next = newNode;
   }
   printf("\n Node inserted at the end of the list!!!");
   getch();
}
void insertBet(int num, int loc)
{
   int pos=loc;
   struct Node *newNode,*temp,*ptr1,*ptr2;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = num;
   newNode->next=NULL;
   if(head == NULL)
   {
      printf("\n List is empty or location is not valid so new node cannot inserted at %d position",pos);
      getch();
      return;
   }
   else
   {
      ptr1 = head;
      loc--;
      while(loc!=1)
      {
ptr1=ptr1->next;
loc--;
      }
      newNode->next=ptr1->next;
      ptr1->next=newNode;
    }
    printf("%d inserted at %d location",newNode->data,pos);
    getch();
}
void removeBeg()
{
   if(head == NULL)
   {
printf("\n\nList is Empty!!!");
return;
   }
   else
   {
      if(head->next == NULL)
      {
printf("%d  node deleted ",head->data);
head = NULL;
getch();
      }
      else
      {
struct Node *temp=head;
head = temp->next;
printf("\n %d node deleted",temp->data);
temp=NULL;
free(temp);
getch();
      }
   }
}
void removeEnd()
{
   if(head == NULL)
   {
printf("\n\nList is Empty!!!");
return;
   }
   else if(head->next == NULL)
   {
printf("%d  node deleted ",head->data);
head = NULL;
getch();
   }
   else
   {
struct Node *temp1=head, *temp2;
while(temp1->next != NULL)
{
    temp2 = temp1;
    temp1 = temp1->next;
}
temp2->next = NULL;
printf("\n %d node deleted", temp1->data);
free(temp1);
      }
      getch();
}
void removeSpecific(int loc)
{
   struct Node *ptr1,*ptr2;
   if(head==NULL)
   {
     printf("\n List is already empty....");
     getch();
     return;
   }
   ptr1=ptr2=head;
   if(loc==1)
   {
    head=ptr1->next;
    printf("\n %d node deleted",ptr1->data);
    free(ptr1);
    getch();
   }
   else
   {
       while(loc!=1)
       {
ptr1=ptr2;
ptr2=ptr2->next;
loc--;
       }
       ptr1->next=ptr2->next;
       printf("%d node deleted...",ptr2->data);
       getch();
       free(ptr2);
       getch();
   }
}
void display()
{
   if(head == NULL)
   {
      printf("\nList is Empty\n");
      getch();
      return;
   }
   else
   {
      struct Node *temp = head;
      printf("\n\nList elements are - \n");
      while(temp!= NULL)
      {
printf("%d --->",temp->data);
temp = temp->next;
      }
      printf("NULL");
      getch();
   }
}

Post a Comment

0 Comments