Singly Linked List in C with Code and Explanation

                       Singly LinkedList (SLL) 

A Singly Linked List is a linear data structure where each element (called a node) points to the next node in the sequence. 
It is a dynamic structure, meaning it can grow or shrink in size during runtime.  Structure of a Node: 
Each node contains: - Data: The value stored in the node. - Next: A pointer/reference to the next node in the list.

There are Five operation in SLL:


1) Insert - Insert the element in our node like, insert(34); 
                                                                          insert(3);
                                                                          insert(4);
2) Traverse - Print all inserted element like, 34->3->4->NULL
3) Delete - We can delete the inserted element like, I want to delete element delete(3); over this result =34->3->4->NULL, so we get after delete the element of delete(3); then we get like this, 34->4->NULL
4) Search - In search operation, if you want to search element like 4, 4 is present in node or not so, on that time we can perform search operation.
5) Reverse - Reverse the original order like from this 34->3->4->NULL to 4->3->34->NULL

 




Code for Singly Linked List:

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

node* head=NULL;
node* createNode(int value){
      node* newNode=(node*)malloc(sizeof(node));
      newNode->data=value;
      newNode->next=NULL;
      return newNode;
}

void insert(int value){
     node* newNode=createNode(value);
     if(head == NULL){
      head = newNode;
     return;
    }
    
    node* temp=head;
    while(temp->next!=NULL){
      temp=temp -> next;
    } 
    temp->next=newNode;
}

void traverse(){
  node* temp=head;
  while(temp!=NULL){
    printf("%d -> ",temp->data);
    temp=temp->next;
    }
    printf("NULL\n");
}

void delete(){
  node* temp=head;
  head=head->next;
  free(temp);
}

void search(int key){
    node* temp=head;
    int count=1;
    while(temp!=NULL){
      count++;
      if(temp->data==key){
      
        printf("\nElement %d found at %d node\n",key,count);
      }
      temp=temp->next;
    }
}

void reverse(){
  node* prev=NULL;
  node* curr=head;
  node* next=NULL;
  while(curr!=NULL){
    next=curr->next;
    curr->next=prev;
    prev=curr;
    curr=next;
    
  }
  head=prev;
}
int main()
{   
    // int d,s;
    insert(34);
    insert(89);
    insert(30);
    insert(98);
    insert(45);
    printf("--------------Traversing node ---------------\n");
    traverse();
    
    // printf("Delete which Element:");
    // scanf("%d",&d);
    delete(98);
    printf("\n--------------After deleting node ---------------\n");
    traverse();
    
    // printf("\nSearch which Element:");
    // scanf("%d",&s);
    printf("\n-------------- Searching LL ---------------");
    search(45);  
    
    reverse();
    printf("\n-------------- Reversing LL ---------------\n");
    traverse();
    return 0;
    
}

 Output:
--------------Traversing node ---------------
34 -> 89 -> 30 -> 98 -> 45 -> NULL

--------------After deleting node ---------------
89 -> 30 -> 98 -> 45 -> NULL

-------------- Searching LL ---------------
Element 45 found at 5 node

-------------- Reversing LL ---------------
45 -> 98 -> 30 -> 89 -> NULL

Here all operations done as you can see.





Comments

Popular posts from this blog

Selection sort (code & Algorithm)

Multiplication of two number by using C Programs

Addition of two Number using c language code