Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

17 July 2012

C Program to implement Singly Linked List

Here is the simple Program to Implement Singly Linked list [ SLL ] with Detail Explanation.


#include
struct node                            //Each node in list will contain data and next pointer
{
      int data;
      struct node *next;
};
struct node *start;
void insertbeg(void)
{
      struct node *nn;
      int a;
      nn=(struct node *)malloc(sizeof(struct node));
      printf("enter data:");
      scanf("%d",&nn->data);
      a=nn->data;
      if(start==NULL)                           //checking if List is empty
      {
            nn->next=NULL;
            start=nn;
      }
      else
      {
            nn->next=start;
            start=nn;
      }
      printf("%d succ. inserted\n",a);
      return;
}
void insertend(void)
{
      struct node *nn,*lp;int b;
      nn=(struct node *)malloc(sizeof(struct node));
      printf("enter data:");
      scanf("%d",&nn->data);
      b=nn->data;
      if(start==NULL)
      {
            nn->next=NULL;
            start=nn;
      }
      else
      {
            lp=start;
            while(lp->next!=NULL)
            {
                  lp=lp->next;
            }
            lp->next=nn;
            nn->next=NULL;
      }
      printf("%d is succ. inserted\n",b);
      return;
}
void insertmid(void)
{
      struct node *nn,*temp,*ptemp;int x,v;
      nn=(struct node *)malloc(sizeof(struct node));
      if(start==NULL)
      {
            printf("sll is empty\n"); return;
      }
      printf("enter data before which no. is to be inserted:\n");
      scanf("%d",&x);
      if(x==start->data)
      {
            insertbeg();
            return;
      }
      ptemp=start;
      temp=start->next;
      while(temp!=NULL&&temp->data!=x)
      {
            ptemp=temp;
            temp=temp->next;
      }
      if(temp==NULL)
      {
            printf("%d data does not exist\n",x);
      }
      else
      {
                  printf("enter data:");
                  scanf("%d",&nn->data);
                  v=nn->data;
                  ptemp->next=nn;
                  nn->next=temp;
                  printf("%d succ. inserted\n",v);
      }
      return;
}
void deletion(void)
{
      struct node *pt,*t;
      int x;
      if(start==NULL)
      {
            printf("sll is empty\n");
           return;
      }
      printf("enter data to be deleted:");
      scanf("%d",&x);
      if(x==start->data)
      {
            t=start;
            start=start->next;
            free(t);
            printf("%d is succ. deleted\n",x);
            return;
      }
      pt=start;
      t=start->next;
      while(t!=NULL&&t->data!=x)
      {
            pt=t;t=t->next;
      }
      if(t==NULL)
      {
            printf("%d does not exist\n",x);return;
       }
      else
      {
            pt->next=t->next;
      }
      printf("%d is succ. deleted\n",x);
      free(t);
      return;
}
void display(void)
{
      struct node *temp;
      if(start==NULL)
      {
            printf("sll is empty\n");
            return;
      }
      printf("elements are:\n");
      temp=start;
      while(temp!=NULL)
      {
            printf("%d\n",temp->data);
            temp=temp->next;
      }
      return;
}
void main()
{
      int c,a;    start=NULL;
      do
      {
            printf("1:insert\n2:delete\n3:display\n4:exit\nenter choice:");
            scanf("%d",&c);
            switch(c)
            {
                  case 1:
                  printf("1:insertbeg\n2:insert end\n3:insert mid\nenter choice:");
                  scanf("%d",&a);
                  switch(a)
                  {
                         case 1:insertbeg();break;
                        case 2:insertend();break;
                        case 3:insertmid();break;
                  }
                  break;
                  case 2:deletion();break;
                  case 3:display();break;
                  case 4:printf("program ends\n");break;
                  default:printf("wrong choice\n");
                  break;
            }
      }while(c!=4);
}
Detail Explanation:

Initially Start pointer is NULL i.e linked list is empty and does not points to any node.

C Program to implement Singly Linked List
When Linked List is Empty and you try to insert.

At this time Start which was NULL initially,will contain the address of new node created by malloc().
Start->Next of new node will contain NULL as no other nodes are present.From the Diagram Start pointer will contain 8000 address i.e of new node and thus start will point to new node.
Insert List at the end of Singly Linked list

In this case to insert the node at the end we will require temporary pointers which will be traversed using while loop until its next part is not NULL.Consider this Diagram here temp will traverse upto 15 and then new node would be added by placing the address of new node in the temp->next so that link is created.

Insert List at the mid of singly Linked listIn order to insert in middle i.e before the given number.Here arises 2 cases i.e if the new node to be inserted is before the current Start node or actually in between two nodes.Considering the second case,Here we require two temporary pointers.consider this code

 while(temp!=NULL&&temp->data!=x)
      {
            ptemp=temp;
            temp=temp->next;
      }

Insert List at the mid of singly Linked listLoop will terminate when temp points to the node containing the required data and thus ptemp points to the node previous of that as temp.the new node is to be inserted in between this 2 pointers.So ptemp->next is placed in new nodes next and ptemp->next is changed to new nodes address




delete the first node in the singly linked list


Consider the case to Delete the first node.In this we will take the address contain by Start pointer to temp pointer and traversed the start to the next node.Thus on freeing the temp will delete the node.From the diagram it can be easily deduced.




delete the middle node in the singly linked listconsider the following code for deletion:

    while(t!=NULL&&t->data!=x)
      {
            pt=t;
            t=t->next;
      }
      if(t!=NULL)
      {
            pt->next=t->next;
      }
      printf("%d is succ. deleted\n",x);
      free(t);
delete the end node in the singly linked listhere we are traversing the pointer t till it gets NULL or it contains the address of the node that contains require data and the pointer pt will contain the address previously contained by t.So pt->next will contain the address contain by t and then free t.




(Note:Click on image for better view)

Similar programs:


C Program to implement Singly circular Linked List
C Program to implement Doubly Linked List
C Program to implement Doubly Circular Linked List.
C program to implement Queue [using Linked List]
Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.
Thank You.
-Romil shah


20 comments:

  1. Anonymous22 July, 2012

    Awesome man!
    I needed this code since a long time and you have given it in a very simple way. Thanks a lot!

    ReplyDelete
  2. Anonymous27 July, 2012

    Really awesome!!!

    ReplyDelete
  3. thanq very very..................................................................much

    ReplyDelete
  4. Thanks a loooot.... this program is superb and simple to understand ..........

    ReplyDelete
  5. Make it more simpler!!!!

    ReplyDelete
    Replies
    1. I have tried my level best still if You have any doubts let me know and you can help me to improve this post or any complicated part of this program to make it more simpler

      Delete
  6. <- what does this symbol means in c programming???

    ReplyDelete
  7. @Anonymous...its not '<-',, its '->'...its called an Arrow operator...used when accessing a structure member variable through a pointer to struct variable. for eg: p->id...where 'p' is of pointer-to-struct type & 'id' is a member variable of a structure. alternatively you may write as: (*p).id... don't miss the round brackets, as the priority of '.' operator is higher than that of '*'.

    ReplyDelete
  8. i'm looking for the explanation....thnks bro..

    ReplyDelete
  9. Very good article,,pls suggest idea for reversal of number

    ReplyDelete
  10. Thanks,
    Provide the exact question statement

    ReplyDelete
  11. To know more related to implementation of Singly Linklist in C refer : Singly LinkedList Implementation Using C Programming

    ReplyDelete

Back To Top