Contact Learn C
Copy

Program 325:Single Linked List for linking Names

Single Linked List for linking Names

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
void InsertAtBeginning(char value[30]);
void InsertAtEnd(char value[30]);
void InsertAtPosition(char value[30],int position);
void RemoveAtBeginning();
void RemoveAtEnd();
void RemoveAtPosition(int position);
void display();
int CheckEmpty();
struct Node{
 char data[30];
 struct Node *next;
}* head=NULL;
int main()
{
 int choice;
 char c,value[30];

 do{
         printf("Enter\n1-Insert\n2-Remove\n3-Display\n");
         scanf("%d",&choice);
         switch(choice)
         {
           case 1:
           {
             int x;
             printf("Enter\n1-Insert at Beginning\n2-Insert at End\n3-Insert at Position\n");
             scanf("%d",&x);
             printf("Enter Name to be Inserted\n");
             scanf("%s",&value);
             switch(x)
             {
                case 1:
                {
                 InsertAtBeginning(value);
                  break;
                }
                case 2:
                {
                  InsertAtEnd(value);
                   break;
                  }
                case 3:
                {
                   int position;
                   printf("Enter position to insert a value(counted from 0)\n");
                   scanf("%d",&position);
                   InsertAtPosition(value,position);
                    break;
                 }
                 default :
                  {
                     printf("Enter Valid Choice\n");
                     break;
                   }
              }
   
              break;
           }
  
    case 2:
    {
      int x;
      printf("Enter\n1-Delete at Beginning\n2-Delete At End\n3-Delete at Position\n");
      scanf("%d",&x);
      switch(x)
      {
        case 1:
        {
         RemoveAtBeginning();
         break;
        }
        case 2:
        {
         RemoveAtEnd();
         break;
        }
        case 3:
        {
         int position;
         printf("Enter position to be removed starting from 1\n");
         scanf("%d",&position);
         RemoveAtPosition(position);
         break;
        }
        default :
        {
         printf("Enter Valid Choice\n");
         break;
        }
      }
     break;
    }
    case 3:
    {
     display();
     break;
    }
    default:
    {
     printf("Enter Valid Choice\n");
     break;
    }
 }
 printf("Enter 'Y' to continue else any letter\n");
 fflush(stdin);
 c=getche();
 printf("\n");
 }while(c=='Y' || c=='y');
 
 return(0);
}

void InsertAtBeginning(char value[30]){
   struct Node *newNumber;
   newNumber = (struct Node*)malloc(sizeof(struct Node));
   strcpy(newNumber->data,value);
   if(head == NULL)
   {
      newNumber->next = NULL;
      head = newNumber;
   }
   else
   {
      newNumber->next = head;
      head = newNumber;
   }
   printf("Given Name %s is inserted at beginning Successfully\n",value);
    
}
void InsertAtEnd(char value[30]){
   struct Node *newNumber;
   newNumber = (struct Node*)malloc(sizeof(struct Node));
   strcpy(newNumber->data,value);
   newNumber->next=NULL;
 if(head==NULL)
 {
  head=newNumber;
 }
 else
 {
  struct Node *temp;
  temp=head;
  while(temp->next!=NULL)
  {
   temp=temp->next;
  }
  temp->next=newNumber;
 }
 
}
void InsertAtPosition(char value[30],int position){
   struct Node *newNumber,*temp;
   int count,flag;
   newNumber = (struct Node*)malloc(sizeof(struct Node));
   strcpy(newNumber->data,value);
   temp=head;
   flag=CheckEmpty();
   if(flag==1)
   {
    int flag1=0;
    count=0;
      while(temp!=NULL)
        {
         if(count==position-1)
          {
        flag1=1;
        newNumber->next=temp->next;
           temp->next=newNumber; 
       }
       else
    {
        temp=temp->next;
           
       }
       count++;
          }
            if(flag1==0)
            {
        printf("Entered Position Not available\n"); 
            }
      else
      {
       printf("Given Name %s is inserted at position %d successfully\n",value,position);
      }
     } 
     else
     {
      printf("List is Empty\n");
     }
}
void RemoveAtBeginning(){
 int flag=CheckEmpty();
 if(flag==1)
 {
  struct Node *temp;
  temp=head;
  if(temp->next==NULL)
  {
   head=NULL;
   free(temp);
  }
  else{
   head=temp->next;
         free(temp);
  }
     printf("Deleted Successfully\n");
 }
 else
 {
  printf("List is Empty\n");
 }
}
void RemoveAtEnd(){
 int flag=CheckEmpty();
 if(flag==1)
 {
  if(head->next==NULL)
  {
   head=NULL;
  }
  else
  {
   struct Node *temp=head,*temp1;
      while(temp->next!=NULL)
      {
        temp1=temp;
        temp=temp->next;
      } 
      temp1->next=NULL;
      free(temp);
  }
  
 }
 else
 {
  printf("List Empty.Try again!\n");
 }
}
void RemoveAtPosition(int position){
  int flag=CheckEmpty();
 if(flag==1)
 {
  int count=0,flag1=0,i;
  struct Node *temp=head;
   if(position==1)
   {
    head=temp->next;
    free(temp);
    return;
   }
   for(i=0;i<position-2;i++)
   {
        temp=temp->next;
   }
   struct Node *temp1=temp->next;
   temp->next=temp1->next;
   free(temp1);
   
  
 }
 else
 {
  printf("List is empty\n");
 }
 
}

void display(){
 int flag=CheckEmpty();
 if(flag==1)
 {
      struct Node *temp;
     temp=head;
      while(temp->next!=NULL)
     {
      printf("%s->",temp->data);
      temp=temp->next;
      }
      printf("%s",temp->data);
        printf("\n");
 }
 else
 {
  printf("No List Available\n");
 }
}

int CheckEmpty()
{
 if(head==NULL)
 return 0;
 else
 return 1;
}
Explanation:
This program is same as the old program Single Linked List for Numbers instead we are taking Names here
I am starting this assuming that you already know "switch case and what functions are and how to call them etc"(basic C).

1.When we create struct Node then the variable declared under Node like "struct Node *newNumber"
then newNumber will have
newNumber= data   Next  
So you can point to data as newNumber->data and for Next as newNumber->Next.
Where data can store Name and Next will point to the adjacent Node which means it stores adjacent node address
Example how the linked list will look like
Head->Hello   200  -> Program    300  -> World  X   

      100             200                300------------------>addresses of respective nodes


If address of Hello is 100
then Head will store 100 which is address of "Hello" and Hello's Next will store Program's address which is 200 and so on

This is basic which you need to understand before going into linked list
But to avoid confusion just think linked list will be like
Head->Hello     -> Program     -> World  X   

and think the blank box which is newNumber->Next points to adjacent node and "X" represents Null which means it is not pointing to any other node.

Lets start!
In linked list we can Insert,Delete,Display any data easily when compared to arrays.

  1. Insert Name at Beginning and by default head is Null as we have declared
    struct Node{
     char data[30];
     struct Node *next;
    }* head=NULL;
    • void InsertAtBeginning(char value[30]){
         struct Node *newNumber;
         newNumber = (struct Node*)malloc(sizeof(struct Node));
         strcpy(newNumber->data,value);
         if(head == NULL)
         {
            newNumber->next = NULL;
            head = newNumber;
         }
         else
         {
            newNumber->next = head;
            head = newNumber;
         }
         printf("Given Name %s is inserted at beginning Successfully\n",value);
          
      }
      Here we are creating newNumber to store name and next node
    • Then copying Name from value to  newNumber->data(lets take Hello)
    • So newNumber will be having
      newNumber->Hello    
      
    • Now we are checking if head is null or not.
    • If head is Null then we should assign NULL to Next of the newNumber and finally assigning head to newNumber
      head->Hello X 
    • If head is Not Null and it is already having Data attached to it.For Example:
      Head->Hello    -> Program    -> World  X   
    • If above is the case then to add cprograms at the beginning.head should point newNumber but before this we need to attach newNumber->Next(that means "cprograms" Next) should be attached to "Hello".
    • To achieve this we need to link up as shown in below image by breaking the existing link
    • Head->Hello    -> Program    -> World  X   
       
      linked list explanation
    • So newNumber->next=head means (before head is pointed to "cprograms"it is storing "Hello" so we are pointing newNumber->next to "Hello"
    • Then head=newnumber. Then head stores the new Name (cprograms)
    • Finally the linked list will be
      linked list explanation
  2. Insert At End
  3. //Coming Soon




Output:
Single Linked List for linking Names

Single Linked List for linking Names

Single Linked List for linking Names

Single Linked List for linking Names

Single Linked List for linking Names







Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide