Showing posts with label data structure by mskuthar. Show all posts
Showing posts with label data structure by mskuthar. Show all posts

Circular Doubly Linked List

Circular Doubly Linked List in Data Structure

A circular doubly linked list is a more complicated data structure in which each node has references to both the previous and next node. There are no NULLs in any of the nodes in a circular doubly linked list. The address of the first node of the list is included in the last node of the list. In its previous pointer, the first node in the list also contains the address of the last node.
  • The following diagram depicts a circular doubly linked list.

  • Because a circular doubly linked list's structure is made up of three components, it requires more space per node and more costly fundamental operations. 
  • A circular doubly linked list, on the other hand, allows for easy manipulation of the pointers and makes searching twice as fast.
Circular Doubly linked list memory management

  • The allocation of memory for a circular doubly linked list is depicted in the diagram below. 
  • The variable head includes the address of the list's first element, i.e. 1, therefore the list's first node contains data A, which is stored at address 1. 
  • Because each node of the list is expected to have three pieces, the first node of the list comprises the addresses of the last node, 8, and the following node, 4, respectively. 
  • As seen in the picture, the last node of the list, which is stored at address 8 and includes data as 6, carries the address of the first node of the list, which is 1.
  • The last node of a circular doubly linked list is identified by the address of the first node, which is stored in the next section of the last node, therefore the node that holds the address of the first node is really the list's last node.
C program to implement all the operations on a circular doubly linked list

#include<stdio.h>  
#include<stdlib.h>  
struct node  
{  
    struct node *prev;  
    struct node *next;  
    int data;  
};  
struct node *head;  
void insertion_beginning();  
void insertion_last();  
void deletion_beginning();  
void deletion_last();  
void display();  
void search();  
void main ()  
{  
int choice =0;  
    while(choice != 9)  
    {  
        printf("\n*********Main Menu*********\n");  
        printf("\nChoose one option from the following list ...\n");  
        printf("\n===============================================\n");  
        printf("\n1.Insert in Beginning\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last\n5.Search\n6.Show\n7.Exit\n");  
        printf("\nEnter your choice?\n");  
        scanf("\n%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            insertion_beginning();  
            break;  
            case 2:  
                    insertion_last();  
            break;  
            case 3:  
            deletion_beginning();  
            break;  
            case 4:  
            deletion_last();  
            break;  
            case 5:  
            search();  
            break;  
            case 6:  
            display();  
            break;  
            case 7:  
            exit(0);  
            break;  
            default:  
            printf("Please enter valid choice..");  
        }  
    }  
}  
void insertion_beginning()  
{  
   struct node *ptr,*temp;   
   int item;  
   ptr = (struct node *)malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
    printf("\nEnter Item value");  
    scanf("%d",&item);  
    ptr->data=item;  
   if(head==NULL)  
   {  
      head = ptr;  
      ptr -> next = head;   
      ptr -> prev = head;   
   }  
   else   
   {  
       temp = head;   
    while(temp -> next != head)  
    {  
        temp = temp -> next;   
    }  
    temp -> next = ptr;  
    ptr -> prev = temp;  
    head -> prev = ptr;  
    ptr -> next = head;  
    head = ptr;  
   }  
   printf("\nNode inserted\n");  
}  
     
}  
void insertion_last()  
{  
   struct node *ptr,*temp;  
   int item;  
   ptr = (struct node *) malloc(sizeof(struct node));  
   if(ptr == NULL)  
   {  
       printf("\nOVERFLOW");  
   }  
   else  
   {  
       printf("\nEnter value");  
       scanf("%d",&item);  
        ptr->data=item;  
       if(head == NULL)  
       {  
           head = ptr;  
           ptr -> next = head;   
           ptr -> prev = head;   
       }  
       else  
       {  
          temp = head;  
          while(temp->next !=head)  
          {  
              temp = temp->next;  
          }  
          temp->next = ptr;  
          ptr ->prev=temp;  
          head -> prev = ptr;  
      ptr -> next = head;  
        }  
   }  
     printf("\nnode inserted\n");  
}  
  
void deletion_beginning()  
{  
    struct node *temp;  
    if(head == NULL)  
    {  
        printf("\n UNDERFLOW");  
    }  
    else if(head->next == head)  
    {  
        head = NULL;   
        free(head);  
        printf("\nnode deleted\n");  
    }  
    else  
    {  
        temp = head;   
        while(temp -> next != head)  
        {  
            temp = temp -> next;  
        }  
        temp -> next = head -> next;  
        head -> next -> prev = temp;  
        free(head);  
        head = temp -> next;  
    }  
  
}  
void deletion_last()  
{  
    struct node *ptr;  
    if(head == NULL)  
    {  
        printf("\n UNDERFLOW");  
    }  
    else if(head->next == head)  
    {  
        head = NULL;   
        free(head);   
        printf("\nnode deleted\n");  
    }  
    else   
    {  
        ptr = head;   
        if(ptr->next != head)  
        {  
            ptr = ptr -> next;   
        }  
        ptr -> prev -> next = head;  
        head -> prev = ptr -> prev;    
        free(ptr);  
        printf("\nnode deleted\n");  
    }  
}  
  
void display()  
{  
    struct node *ptr;  
    ptr=head;  
    if(head == NULL)  
    {  
        printf("\nnothing to print");  
    }     
    else  
    {  
        printf("\n printing values ... \n");  
          
        while(ptr -> next != head)  
        {  
          
            printf("%d\n", ptr -> data);  
            ptr = ptr -> next;  
        }  
        printf("%d\n", ptr -> data);  
    }  
              
}  
  
void search()  
{  
    struct node *ptr;  
    int item,i=0,flag=1;  
    ptr = head;   
    if(ptr == NULL)  
    {  
        printf("\nEmpty List\n");  
    }  
    else  
    {   
        printf("\nEnter item which you want to search?\n");   
        scanf("%d",&item);  
        if(head ->data == item)  
        {  
        printf("item found at location %d",i+1);  
        flag=0;  
        }  
        else   
        {  
        while (ptr->next != head)  
        {  
            if(ptr->data == item)  
            {  
                printf("item found at location %d ",i+1);  
                flag=0;  
                break;  
            }   
            else  
            {  
                flag=1;  
            }  
            i++;  
            ptr = ptr -> next;  
        }  
        }  
        if(flag != 0)  
        {  
            printf("Item not found\n");  
        }  
    }     
          
}  

Expected Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
1

Enter Item value123

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
2

Enter value234

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
1

Enter Item value90

Node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
2

Enter value80

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
3

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
4

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
6

 printing values ... 
123

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
5

Enter item which you want to search?
123
item found at location 1
*********Main Menu*********

Choose one option from the following list ...

============================================

1. Insert in Beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search
6. Show
7. Exit

Enter your choice?
7

Circular Singly Linked List

Circular Linked list in Data Structure

In a circular Sinly linked list, The last node of a singly linked list carries a pointer to the list's first node. Circular singly-linked lists and circular doubly linked lists are also possible.

We traverse a circular singly linked list until we reach the starting node. There is no beginning or conclusion to the circular list of singly liked items. In the following part of any of the nodes, there is no null value.

A circular singly linked list is depicted in the image below.


  • Circular linked lists are commonly used in operating systems for task maintenance. 
  • Circular linked lists are used in a variety of ways in computer science, including browser surfing, where a record of pages seen previously by the user is kept in the form of circular linked lists and can be accessed again by clicking the previous button.

Circular linked list memory representation.
  • A memory representation of a circular linked list comprising a student's marks in four disciplines is shown in the image below. 
  • The graphic, on the other hand, depicts how the circular list is stored in memory. 
  • The list's beginning, or head, points to the element with index 1 and 13 marks in the data part and 4 in the following section. 
  • That is, it is linked to the node that is recorded at the fourth index in the list.
  • However, because we are dealing with a circular linked list in memory, the last node of the list contains the address of the first node of the list.

  • We may also have many linked lists in memory, each with its own set of start pointers corresponding to the list's various start nodes. 
  • The next section of the last node, which holds the address of the list's start node, is used to identify it. 
  • Any linked list's last node must be identified in order to determine the number of iterations that must be done when traversing the list.
All operations on a circular singly linked list are implemented using a menu-driven C programme.

#include<stdio.h>  
#include<stdlib.h>  
struct node   
{  
    int data;  
    struct node *next;   
};  
struct node *head;  
  
void beginsert ();   
void lastinsert ();  
void randominsert();  
void begin_delete();  
void last_delete();  
void random_delete();  
void display();  
void search();  
void main ()  
{  
    int choice =0;  
    while(choice != 7)   
    {  
        printf("\n*********Main Menu*********\n");  
        printf("\nChoose one option from the following list ...\n");  
        printf("\n===============================================\n");  
        printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last\n5.Search for an element\n6.Show\n7.Exit\n");  
        printf("\nEnter your choice?\n");         
        scanf("\n%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            beginsert();      
            break;  
            case 2:  
            lastinsert();         
            break;  
            case 3:  
            begin_delete();       
            break;  
            case 4:  
            last_delete();        
            break;  
            case 5:  
            search();         
            break;  
            case 6:  
            display();        
            break;  
            case 7:  
            exit(0);  
            break;  
            default:  
            printf("Please enter valid choice..");  
        }  
    }  
}  
void beginsert()  
{  
    struct node *ptr,*temp;   
    int item;   
    ptr = (struct node *)malloc(sizeof(struct node));  
    if(ptr == NULL)  
    {  
        printf("\nOVERFLOW");  
    }  
    else   
    {  
        printf("\nEnter the node data?");  
        scanf("%d",&item);  
        ptr -> data = item;  
        if(head == NULL)  
        {  
            head = ptr;  
            ptr -> next = head;  
        }  
        else   
        {     
            temp = head;  
            while(temp->next != head)  
                temp = temp->next;  
            ptr->next = head;   
            temp -> next = ptr;   
            head = ptr;  
        }   
        printf("\nnode inserted\n");  
    }  
              
}  
void lastinsert()  
{  
    struct node *ptr,*temp;   
    int item;  
    ptr = (struct node *)malloc(sizeof(struct node));  
    if(ptr == NULL)  
    {  
        printf("\nOVERFLOW\n");  
    }  
    else  
    {  
        printf("\nEnter Data?");  
        scanf("%d",&item);  
        ptr->data = item;  
        if(head == NULL)  
        {  
            head = ptr;  
            ptr -> next = head;    
        }  
        else  
        {  
            temp = head;  
            while(temp -> next != head)  
            {  
                temp = temp -> next;  
            }  
            temp -> next = ptr;   
            ptr -> next = head;  
        }  
          
        printf("\nnode inserted\n");  
    }  
  
}  
  
void begin_delete()  
{  
    struct node *ptr;   
    if(head == NULL)  
    {  
        printf("\nUNDERFLOW");    
    }  
    else if(head->next == head)  
    {  
        head = NULL;  
        free(head);  
        printf("\nnode deleted\n");  
    }  
      
    else  
    {   ptr = head;   
        while(ptr -> next != head)  
            ptr = ptr -> next;   
        ptr->next = head->next;  
        free(head);  
        head = ptr->next;  
        printf("\nnode deleted\n");  
  
    }  
}  
void last_delete()  
{  
    struct node *ptr, *preptr;  
    if(head==NULL)  
    {  
        printf("\nUNDERFLOW");  
    }  
    else if (head ->next == head)  
    {  
        head = NULL;  
        free(head);  
        printf("\nnode deleted\n");  
  
    }  
    else   
    {  
        ptr = head;  
        while(ptr ->next != head)  
        {  
            preptr=ptr;  
            ptr = ptr->next;  
        }  
        preptr->next = ptr -> next;  
        free(ptr);  
        printf("\nnode deleted\n");  
  
    }  
}  
  
void search()  
{  
    struct node *ptr;  
    int item,i=0,flag=1;  
    ptr = head;   
    if(ptr == NULL)  
    {  
        printf("\nEmpty List\n");  
    }  
    else  
    {   
        printf("\nEnter item which you want to search?\n");   
        scanf("%d",&item);  
        if(head ->data == item)  
        {  
        printf("item found at location %d",i+1);  
        flag=0;  
        }  
        else   
        {  
        while (ptr->next != head)  
        {  
            if(ptr->data == item)  
            {  
                printf("item found at location %d ",i+1);  
                flag=0;  
                break;  
            }   
            else  
            {  
                flag=1;  
            }  
            i++;  
            ptr = ptr -> next;  
        }  
        }  
        if(flag != 0)  
        {  
            printf("Item not found\n");  
        }  
    }     
          
}  
  
void display()  
{  
    struct node *ptr;  
    ptr=head;  
    if(head == NULL)  
    {  
        printf("\nnothing to print");  
    }     
    else  
    {  
        printf("\n printing values ... \n");  
          
        while(ptr -> next != head)  
        {  
          
            printf("%d\n", ptr -> data);  
            ptr = ptr -> next;  
        }  
        printf("%d\n", ptr -> data);  
    }  
              
}  

Expected Output:

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
1

Enter the node data?10

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
2

Enter Data? 20

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
2

Enter Data?30

node inserted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
3

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
4

node deleted

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
5

Enter item which you want to search?
20
item found at location 1
*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
6

 printing values ... 
20

*********Main Menu*********

Choose one option from the following list ...

===============================================

1. Insert in beginning
2. Insert at last
3. Delete from Beginning
4. Delete from last
5. Search for an element
6. Show
7. Exit

Enter your choice?
7

Insertion in singly linked list 2

Insertion in singly linked list after specified Node

  • To insert an element into a linked list after a certain number of nodes, we must skip the desired number of elements in the list and change the pointer to the spot where the node will be put. 
  • The following statements will be used to do this.
emp=head;  
            for(i=0;i<loc;i++)  
            {  
                temp = temp->next;  
                if(temp == NULL)  
                {  
                    return;  
                }  
            }
  • Allocate space for the new node and include the item in the data section. The following statements will be used to do this.
ptr = (struct node *) malloc (sizeof(struct node));  
        ptr->data = item; 
  • Only a few more link adjustments are needed, and our node will be put at the correct location. 
  • Because the loop pointer temp will be pointing to the node when the new node is placed at the end of the loop. 
  • As a result, the address of the next portion of the temp must be included in the next part of the new node ptr (since, ptr will be in between temp and the next of the temp). 
  • The following statements will be used to do this.
ptr→ next = temp → next
  • Now, we just need to make the next part of the temp, point to the new node ptr. This will insert the new node ptr, at the specified position.
temp ->next = ptr;   

Insertion in singly linked list

Algorithm

  • STEP 1: IF PTR = NULL
              WRITE OVERFLOW
              GOTO STEP 12
              END OF IF
  • STEP 2: SET NEW_NODE = PTR
  • STEP 3: NEW_NODE → DATA = VAL
  • STEP 4: SET TEMP = HEAD
  • STEP 5: SET I = 0
  • STEP 6: REPEAT STEP 5 AND 6 UNTIL I
  • STEP 7: TEMP = TEMP → NEXT
  • STEP 8: IF TEMP = NULL
               WRITE "DESIRED NODE NOT PRESENT"
                GOTO STEP 12
                END OF IF
                END OF LOOP
  • STEP 9: PTR → NEXT = TEMP → NEXT
  • STEP 10: TEMP → NEXT = PTR
  • STEP 11:  SET PTR = NEW_NODE
  • STEP 12:  EXIT
C program to insert data(after specific node) in singly linked list.
#include<stdio.h>  
#include<stdlib.h>  
void randominsert(int);  
void create(int);  
struct node  
{  
    int data;  
    struct node *next;  
};  
struct node *head;  
void main ()  
{  
    int choice,item,loc;  
    do   
    {  
        printf("\nEnter the item which you want to insert?\n");  
        scanf("%d",&item);  
        if(head == NULL)  
        {  
            create(item);  
        }  
        else  
        {  
            randominsert(item);  
        }  
        printf("\nPress 0 to insert more ?\n");  
        scanf("%d",&choice);  
    }while(choice == 0);  
}  
void create(int item)  
{  
      
        struct node *ptr = (struct node *)malloc(sizeof(struct node *));  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW\n");  
        }  
        else  
        {  
            ptr->data = item;  
            ptr->next = head;  
            head = ptr;  
            printf("\nNode inserted\n");  
        }  
}  
void randominsert(int item)  
    {  
        struct node *ptr = (struct node *) malloc (sizeof(struct node));  
        struct node *temp;  
        int i,loc;  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW");  
        }  
        else  
        {  
              
            printf("Enter the location");  
            scanf("%d",&loc);             
            ptr->data = item;  
            temp=head;  
            for(i=0;i<loc;i++)  
            {  
                temp = temp->next;  
                if(temp == NULL)  
                {  
                    printf("\ncan't insert\n");  
                    return;  
                }  
              
            }  
            ptr ->next = temp ->next;   
            temp ->next = ptr;   
            printf("\nNode inserted");  
        }  
          
    }  

Expected Output:

Enter the item which you want to insert?
12
Node inserted
Press 0 to insert more ?
2

Insertion in singly linked list 1

Insertion in singly linked list (at the end).

There are two cases that need to be described in order to put a node at the end.
  1. The node is being added to a list that is currently empty.
  2. The node is appended to the connected list's end.
The first case:
  • The condition (head == NULL) is satisfied in the first case. As a result, we only need to use the malloc statement in C to allocate space for the node. The following statements are used to build up the data and link parts of the node.
ptr->data = item;  
                ptr -> next = NULL; 
  • Because ptr is the only node that will be added into the list, it must be pointed to by the list's head pointer. The following statements will be used to do this.
Head = ptr     

The second case:

  • Because Head is not null, the condition Head = NULL would fail. In order to traverse the list, we must first declare a temporary pointer temp. temp is set to point to the list's initial node.
Temp = head  
  • Then, traverse through the entire linked list using the statements: 
while (temp→ next != NULL)  
            temp = temp → next; 
  • The temp will point to the last node of the list at the end of the loop. Now, set aside space for the new node and assign the item to its data section. 
  • Because the new node will be the last node in the list, the next component of this node must point to null. The next section of the temp node (which is presently the list's last node) must point to the new node (ptr).
temp = head;  
        while (temp -> next != NULL)  
        {  
            temp = temp -> next;  
        }  
        temp->next = ptr;  
        ptr->next = NULL;

Insertion in singly linked list


Algorithm

  • Step 1: IF PTR = NULL Write OVERFLOW
         Go to Step 1
         [END OF IF]
  • Step 2: SET NEW_NODE = PTR
  • Step 3: SET PTR = PTR - > NEXT
  • Step 4: SET NEW_NODE - > DATA = VAL
  • Step 5: SET NEW_NODE - > NEXT = NULL
  • Step 6: SET PTR = HEAD
  • Step 7: Repeat Step 8 while PTR - > NEXT != NULL
  • Step 8: SET PTR = PTR - > NEXT
         [END OF LOOP]
  • Step 9: SET PTR - > NEXT = NEW_NODE
  • Step 10: EXIT
C program to insert data(at the end) in singly linked list.

#include<stdio.h>  
#include<stdlib.h>  
void lastinsert(int);  
struct node  
{  
    int data;  
    struct node *next;  
};  
struct node *head;  
void main ()  
{  
    int choice,item;  
    do   
    {  
        printf("\nEnter the item which you want to insert?\n");  
        scanf("%d",&item);  
        lastinsert(item);  
        printf("\nPress 0 to insert more ?\n");  
        scanf("%d",&choice);  
    }while(choice == 0);  
}  
void lastinsert(int item)  
    {  
        struct node *ptr = (struct node*)malloc(sizeof(struct node));     
        struct node *temp;  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW");     
        }  
        else  
        {  
            ptr->data = item;  
            if(head == NULL)  
            {  
                ptr -> next = NULL;  
                head = ptr;  
                printf("\nNode inserted");  
            }  
            else  
            {  
                temp = head;  
                while (temp -> next != NULL)  
                {  
                    temp = temp -> next;  
                }  
                temp->next = ptr;  
                ptr->next = NULL;  
                printf("\nNode inserted");  
              
            }  
        }  
    }  

Expected Output:

Enter the item which you want to insert?
12
Node inserted
Press 0 to insert more ?
0
Enter the item which you want to insert?
23
Node inserted
Press 0 to insert more ?
2

Singly Linked List

One-way chain or single-linked list

  • A singly linked list is a collection of elements that are arranged in a specific order. 
  • The number of elements may vary depending on the program's requirements. In a single linked list, a node is made up of two parts: data and link. 
  • The data component of the node stores the actual data that the node will represent, while the link part stores the address of the node's immediate successor.
  • A one-way chain, also known as a singly linked list, can only be traversed in one direction. 
  • To put it another way, we may say that each node has only the next pointer, therefore we can't traverse the list backwards.

Consider the following scenario: the student's grades in three marks are maintained in a linked list, as indicated in the image.

Singly Linked List
Singly Linked List

  • The links are represented by the arrow in the diagram above. 
  • The marks earned by the student in each topic are stored in the data section of each node. 
  • The null pointer in the address part of the last node identifies it as the last node in the list. In the data section of the list, we can have as many elements as we like.

Operations on Singly Linked List

On a singly linked list, you can conduct a variety of actions. The following is a list of all such operations.
  1. Insertion in singly linked list at beginning.
  2. Insertion in singly linked list at end.
  3. Insertion in singly linked list after specific node.
    • Deletion and Traversing.
    1. Deletion at beginning.
    2. Deletion at the end of the list.
    3. Deletion after specified node.
    4. Traversing.
    5. Searching.

    Node Creation

    struct node   
    {  
        int data;   
        struct node *next;  
    };  
    struct node *head, *ptr;   
    ptr = (struct node *)malloc(sizeof(struct node *));  

    Insertion

    • Insertion into a singly linked list can take place at several points. The insertion is classified into the following types based on the position of the new node being inserted.

    Insertion in singly linked list at beginning

    • It's easy to add a new element to a single linked list at the start. Only a few changes to the node linkages are required. The steps below must be followed in order to add a new node to the list at the start.
    • Allocate space for the new node and place data in the node's data section. The following statements will accomplish this.
    ptr = (struct node *) malloc(sizeof(struct node *));  
                ptr → data = item   
    • Make the new node's link component point to the list's existing first node. The following expression will be used to do this.
    ptr->next = head;  
    • Finally, we must make the new node the first node in the list, which may be accomplished with the following statement.
    head= ptr ;

    Algorithm

    • Step 1: IF PTR = NULL
               Write OVERFLOW
               Go to Step 7
               [END OF IF]
    • Step 2: SET NEW_NODE = PTR
    • Step 3: SET PTR = PTR → NEXT
    • Step 4: SET NEW_NODE → DATA = VAL
    • Step 5: SET NEW_NODE → NEXT = HEAD
    • Step 6: SET HEAD = NEW_NODE
    • Step 7: EXIT
    Singly Linked List

    C program to insert new data (at the beginning) in singly linked list.
    #include<stdio.h>  
    #include<stdlib.h>  
    void beginsert(int);  
    struct node  
    {  
        int data;  
        struct node *next;  
    };  
    struct node *head;  
    void main ()  
    {  
        int choice,item;  
        do   
        {  
            printf("\nEnter the item which you want to insert?\n");  
            scanf("%d",&item);  
            beginsert(item);  
            printf("\nPress 0 to insert more ?\n");  
            scanf("%d",&choice);  
        }while(choice == 0);  
    }  
    void beginsert(int item)  
        {  
            struct node *ptr = (struct node *)malloc(sizeof(struct node *));  
            if(ptr == NULL)  
            {  
                printf("\nOVERFLOW\n");  
            }  
            else  
            {  
                ptr->data = item;  
                ptr->next = head;  
                head = ptr;  
                printf("\nNode inserted\n");  
            }    
        }  

    Expected Output:
    Enter the item which you want to insert?
    12
    Node inserted
    Press 0 to insert more ?
    0
    Enter the item which you want to insert?
    23
    Node inserted
    Press 0 to insert more ?
    2