×

Singly Linked list

Singly Linked list

A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed in only one direction from head of linked list to the tail of the linked list.

The basic building block of linked list is called node. A single node contains two things, first is data and second is a pointer of the next node which helps us to maintain the structure of the list.

The first node of the linked list is called head; it contains the starting address of the linked list and same as the last node is called as tail, points to NULL, which helps us to determine when the list ends.

Singly Linked list

Operations on Singly Linked List

  • Insert a node in the linked list:

We can insert a node into the linked list at the front, the end or anywhere in the linked list. The time complexity of these operations is as follows:

  • If we insert the node at the front of linked list; takes O(1)
  • If we insert the node at the end of linked list; takes O(n)
  • If we insert the node anywhere in the linked list; takes O(n)
  • Remove a node from the linked list:

We can remove a node from the front of the linked list, the end or anywhere of the linked list. The time complexity of these operations is as follows:

  • If we remove the node from the front of linked list; takes O(1)
  • If we remove the node from the end of linked list; takes O(n)
  • If we remove the node from anywhere in the linked list; takes O(n)
  • Search a node in the linked list:

Searching a node in the linked list takes O(n) time in the worst case.

Singly Linked List Program in C Language: -

 #include<stdio.h>
 #include<stdlib.h>
 struct node
 {
 int info;
 struct node *next;
 };
 struct node *start=NULL;
 void begin(int item)
 {
     struct node *p,*t;
     p=start;
     t=(struct node *)malloc(sizeof(struct node ));
     start=t;
     start -> info=item;
     start -> next=p;
     return;
 }
 void Specificposition(int item,int pos)
 {
     int i;
 struct node *t, *p, *temp;
 p=start;
 t=(struct node *)malloc(sizeof(struct node ));
 for(i=1; i<pos-1; i++)
 {
     p=p -> next;
 }
   temp = p -> next;
   t -> next = temp;
   t -> info = item;
   p -> next = t;
 }
 void End(int item)
 {
 struct node *t, *p;
 t=(struct node *)malloc(sizeof(struct node ));
 if(start == NULL)
 {
 start = t;
 start -> info = item;
 start -> next = NULL;
 return;
 }
 else
 {
 struct node *p = start;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 }
 }
 void delbegin()
 {
     struct node *t,*p;
     p = start;
     t = start -> next;
     start = t;
     free(p);
 }
 void delend()
 {
      struct node *t;
      t = start;
      while(t -> next -> next!=NULL)
      {
          t = t -> next;
      }
      t -> next = NULL;
      return;
 }
 void delspecificposition(int pos)
 {
     int i;
     struct node *t,*p;
     t=start;
    for(i=1;i<pos-1;i++)
    {
        t=t -> next;
    }
    t -> next=t -> next -> next;
     return;
 }
 void traverse(struct node * t)
 {
 if(t==NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     while(t -> next!=NULL)
                                     {
                         printf("%d -> ",t -> info);
                         t=t -> next;
                         }
                         printf("%d\n",t -> info);
 }
 void main()
 {
     int n,pos,item;
 while(1)
 {
 printf(" 1. insert at begin");
 printf("\n 2. insert at specific position");
 printf("\n 3. insert at End");
 printf("\n 4. delete at beginning");
 printf("\n 5. delete specific position");
 printf("\n 6. delete at End");
 printf("\n 7. Traverse the list");
 printf("\n 8.Exit\n");
 printf("Enter the choice:-\n");
 scanf("%d",&n);
 switch(n)
 {
 case 1:
        printf("enter the item do wanna insert:-\n");
        scanf("%d",&item);
        begin(item);
        break;
 case 2:
      printf("enter the position where do wanna insert:-\n");
      scanf("%d",&pos);
      printf("enter the item do wanna insert at position:-\n");
      scanf("%d",&item);
      Specificposition(item,pos);
      break;
 case 3:
        printf("enter the item do wanna insert at end:-\n");
        scanf("%d",&item);
        End(item);
        break;
 case 4:
        delbegin();
        break;
 case 5:
        printf("enter the position which do wanna delete :-\n");
        scanf("%d",&pos);
        delspecificposition(pos);
        break;
 case 6:
       delend();
       break;
 case 7:
        printf("your list is:");
        traverse(start);
        break;
 case 8:
         exit(0);
 }
 }
 } 

Output: -

Singly Linked list

Related Topics

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.

Post-order traversal in a binary tree

We all know that postorder is a form of tree traversal to visit the tree's nodes, and it helps us reach out to the tree's nodes. Postorder means visiting the...

4 minutes read.

Introduction to 2D-Arrays

Two Dimensional Array Technical Definitions An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table...

3 minutes read.

What is the B+ Tree in Data Structures?

We all know that the B+ tree in data structures is nothing but just an extended version of the B tree. It allows the smooth working of all the operations...

7 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

8 minutes read.

Program to calculate the area of the circumcircle of an equilateral triangle

You have given one value which represents the side of the equilateral triangle. You have to find out the area of the circumcircle. Let’s take an example - For the above...

3 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

3 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Merge Sort

Merge Sort is one of the most widely used sorting algorithms, and it is based on the Divide and Conquer principle. A problem is subdivided into multiple sub-problems in this method....

8 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Quick Sort

Quicksort is a sorting algorithm that uses a divide-and-conquer strategy. A pivot element is used to divide an array into subarrays (element selected from the array).  The pivot element should be...

4 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

3 minutes read.

Insertion in B+ Tree

We will learn how to insert a node in the B+ tree and what are the different properties we are going to follow. Except for the root node, every node should...

5 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

AVL tree in data structure c++

AVL tree is generally known as the self-sustained and most balanced tree in the field of a binary search tree. It was also widely known as the height-balanced binary tree....

6 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Stack Using Array

Stack – A Stack is a linear abstract data type used to store elements. It is also called last in first out or first in last out data structure because...

6 minutes read.