Concatenation in Single Linked List Using C

Description

This program is used to Create and Concatenate elements of a Single Linked List Using C .


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

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

void concatenate();
void print();

void main()
{
  int choice;
  do
  {
   printf("\n__MENU__\n1.Create New List\n2.Concatenate in the List\n2...EXIT...\n");
   printf("\nENTER your choice\n");
   scanf("%d",&choice);
   switch(choice)
   {
    case 1: head=NULL; concatenate();break;
    case 2 :concatenate();break;
    case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break;
   default:printf("INVALID input !!!");break;   
  }
  }while(choice!=3);
}
 
void concatenate()
 {
 int x, i,n1,n2;
 
 struct node* newnode,*temp;
 temp=head;
 printf("\nEnter no. of elements in LIST \n");
 scanf("%d",&n1);
 printf("enter elements\n");
 for(i=0;i<n1;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

Concatenation Output

Labels: ,