×

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

Types of Linked list

Single linked list  A single linked list is a linked list in which all nodes are connected with each other in sequence. Each node of a singly linked list has two...

7 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 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.

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.

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 minutes read.

Understanding Data Processing

Introduction Data In our everyday lives, any task that we perform online is related to data. Millions of pieces of data are produced every second across the globe. Data production is largely...

4 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.

Bubble Sort vs Merge Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Merge Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

Tree vs Graph: Data Structure

Difference Between Tree and Graph What is Tree? A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in...

3 minutes read.

Convert binary tree to a doubly linked list

Implementation //creating a C++ program for the transition of a binary tree into a linked list. #include <iostream> using namespace std; /* Firstly, let’s create a binary tree that will help us in setting...

4 minutes read.

Advantages and Disadvantages of Linked List

Advantages of Linked List The linked list is a dynamic data structure.You can also decrease and increase the linked list at run-time. That is, you can allocate and deallocate memory at...

3 minutes read.

Spanning Tree

Spanning Tree: The spanning tree is a subset of the graph. It is a non-cyclic graph. If any node in the spanning tree is truncated, the entire graph fails. There are...

10 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.