Single Linked List in C - Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Single Linked List in C

Hello, Friends!

Single Link List:

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *next;
};
struct node *start=NULL;

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

struct node *getnode()        
{
  return((struct node *)malloc(sizeof(struct node)));
}
 
 
void freenode(struct node *ptr)
{
 free(ptr);
}

void search()
{
 struct node *temp;
 int d,c=0,k;
 printf("\nEnter the number to be searched:\t");
 scanf("%d",&k);
 temp=start;
 if (temp==NULL)
 {
  printf("List is empty!\n");
 }
 else
 {
  while(temp!=NULL)
  {
   c++;
   if(temp->data==k)
   {
    d=c;
    break;
   }
   temp=temp->next;
  }
  if(temp==NULL)
  {
   printf("Element not found\n");
  }
  else
  {
   printf("Element is present at position %d",d);
  }
 }
}


int count()       
{
 struct node *temp;
 int c=0;
 temp=start;
 if (temp==NULL)
 {
  printf("List is empty!\n");
  return 0;
 }
 else
 {
  while(temp!=NULL)
  {
   c++;
   temp=temp->next;
  }
  return(c);
 }
}

void delbeg()       
{
 struct node *temp;
 temp=start;
 if(start== NULL)
 {
  printf("\nSorry,List is empty!!!!!\n");
 }
 else
 {
  printf("First element %d successfully deleted\n",temp->data);
  if(start->next!=NULL)
  {
   start=start->next;
   freenode(temp);
   display(start);
  }
  else
  {
   start=NULL;
   display(start);
  }
 }
}

void delend()        
{
 int x;
 struct node *temp,*ptr;
 temp=start;
 if(start== NULL)
 {
  printf("\nList is empty!\n");
 }
 else
 {
  if(temp->next!=NULL)
  {
   while(temp->next!=NULL)
   {
    ptr=temp;
    temp=temp->next;
   }
   x=temp->data;
   ptr->next=NULL;
  }
  else
  {
   x=temp->data;
   start=NULL;
  }
 }
 printf("Last element %d successfully deleted\n",x);
 freenode(temp);
 display(start);
}


void delloc()       
{
 int x,p,i;
 struct node *t,*del,*newnode;
 t=start;
 printf("Enter the location of node to be deleted : \n");
 scanf("%d",&p);
 if(start== NULL)
 {
  printf("\nEmpty list\n");
 }
 else if(p==1)
  delbeg();
 else
 { 
  for(i=1;i<p-1;i++)
  {
   if(t->next==NULL)
   {
    printf("There are less than %d elements in the list\n",p);
    break;
   }
   t=t->next;  
  }
  if(i==p-1)
  {
   if(t->next!=NULL)
   {
    x=(t->next)->data;
    del=t->next;
    t->next=del->next;
    freenode(del);
    printf("Element %d present at desired node is successfully deleted\n",x);
    display(start);
   }
   else
   {
    printf("There are less than %d elements in the list\n",p);
   }
  }
 }
}

 
void insertbeg()        
{
 struct node *newnode;
 int n;
 printf("\nEnter the data to be inserted\n");
 scanf("%d",&n);
 newnode=getnode();
 newnode->data=n;
 newnode->next=start;
 printf("\nElement %d successfully inserted in the first position",n);
 start=newnode;
 display(start);
}
 
void insertend()        
{
 struct node *temp,*newnode;
 int n;
 printf("\nEnter the data to be inserted\n");
 scanf("%d",&n);
 temp=start;
 if(temp==NULL)
 {
  newnode=getnode();
  start=newnode;
  newnode->data=n;
  newnode->next=NULL;
  display(start);
 }
 else
 {
   while(temp->next!=NULL)
   {
    temp=temp->next;
  }
  newnode=getnode();
  newnode->data=n;
  newnode->next=NULL;
  temp->next=newnode;
  display(start);
 }
}
 
void insertloc()        
{
 struct node *temp,*newnode;
 int n,i,p;
 printf("Enter the position in which element is to be inserted : \n");
 scanf("%d",&p);
 temp=start;
 if(p==1)
 {
  insertbeg(); 
 }
 else if(start==NULL)
 {
  printf("List is empty!\n");
 }
 else
 {
  for(i=1;i<p-1;i++)
  {
   if(temp->next==NULL)
   {
    printf("\nThe no. of Elements are less than %d\n",p);
    break;
   }
   temp=temp->next;
  }
  if(i==p-1)
  {
   printf("Enter the data to be inserted\n");
   scanf("%d",&n);
   newnode=getnode();
   newnode->data=n;
   newnode->next=temp->next;
   temp->next=newnode;
   display(start);
  }
 }
}
 

 
struct node *create()
{
 struct node *newnode,*temp;
 int num;
 printf("Enter elements for the list : \n");
 printf("Please enter data (Press -1 to end)\n");
 scanf("%d",&num);
 start=NULL;
 temp=start;
 while(num!=-1)
 {
  if(temp==NULL)
  {
   newnode=getnode();
   newnode->data=num;
   newnode->next=NULL;
   start=newnode;
   temp=start;
  }
  else
  {
   newnode=getnode();
   newnode->data=num;
   newnode->next=NULL;
   temp->next=newnode;
   temp=temp->next;
  }
  printf("Please enter data(Press -1 to end)\n");
  scanf("%d",&num);
 }  
 display(start); 
 return(start);
}


// modified
void concatenate()
{
 struct node *newnode,*temp,*list1,*list2;
 printf("Current list : \n");
 display(start);
 list1 = start;
 printf("Enter second list : \n");
 list2=create();
 temp=list1;
 while(temp->next!=NULL)
 {
  temp=temp->next;
 }
 temp->next=list2;
 printf("Concatenated list :\n");
 display(list1);
 start = list1;
}

//modified
void split()
{
 struct node *list1,*list2,*temp,*a;
 int k,c=0,i,m;
 temp = start;
 m=count();
 do
 {
  printf("\nEnter the no of elements in list1\n");
  printf("\n Remember that we have %d elements in the list",m);
  scanf("%d",&k);  //count from original list
  if(k>m)
  {
   printf("Invalid input\n");
   break;
  } 
  list1 = temp;
  a = temp;
  for(i=1;i<k;i++)
  {
   a = a->next;
  }
  list2 = a->next;
  a->next = NULL;
  printf("List1 : \n");
  display(list1);
  printf("List2 : \n");
  display(list2);
 }while(0);
}

// modified     
void reverse()           
{
 struct node *temp,*newnode,*list2;
 temp = start;
 list2=NULL;
 if (temp==NULL)
 {
  printf(" List is empty!!!!!\n");
 }
 else
 {
  while(temp!=NULL)
  {
   newnode=getnode();
   newnode->data=temp->data;
   newnode->next=list2;
   list2=newnode;
   temp=temp->next;
  }
  printf(" Reversed List : \n");
  display(list2);
  start = list2;
 }
}
 
//modified
void copy()
{
 struct node *list2,*temp;
 temp = start;
 printf("List1 \n");
 display(temp);
 list2 = temp;
 printf("List2 \n");
 display(list2);
}

void main()
{
 int ch,k;
 do
 {
  printf("~~~ MENU ~~~\n");
  printf("1. Create List \n");
  printf("2. Insert At Beginning \n");
  printf("3. Insert At End \n");
  printf("4. Insert At Location \n");
  printf("5. Delete At Beginning\n");
  printf("6. Delete At End \n");
  printf("7. Delete At Location\n");
  printf("8. Display \n");
  printf("9. Search\n");
  printf("10. Count\n");
  printf("11. Copy \n");
  printf("12. Concatenate \n");
  printf("13. Split \n");
  printf("14. Reverse \n");
  printf("15. Quit \n");
  printf("\nEnter your choice\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:      
    start=create();
    break;
   case 2:      
    insertbeg();
    break;
   case 3:      
    insertend();
    break;
   case 4:      
    insertloc();
    break;
   case 5:      
    delbeg();
    break;
   case 6:      
    delend();
    break;
   case 7:      
    delloc();
    break;

   case 8: display(start);  
    break;
   case 9:      
    search();
    break;
   case 10:     
    k=count();
    printf("\n Count = %d\n",k);
    break;
   case 11:
    copy();     
    break;
   case 12:
    concatenate ();   
    break;
   case 13:
    split();    
    break;
   case 14:     
    reverse();
    break;
   case 15:
    printf("\nThank you !!!\n");   
    break;
   default:
    printf("\nInvalid input!!!\n"); 
  }
 }while(ch!=15);
} 


Output:

Create a LinkList

Insert At beginning

Insert at End
Insert at Location
Delete at Beginning
Delete at End
Delete at Location
Display
Search Element
Count
Copy
Concatenate
Split
Reverse





















PropellerAds