Follow

# HARDIK KAUSHIK's Blog

Follow # Data Structure and Algorithms- Linked List (Quick Revision Series PART- 3.1)

HARDIK KAUSHIK
·Jun 1, 2021·

In this blog, we will discuss LinkedList and its 3 types and then we will also discuss some basic operations that we can do in a linked list. So let's get started. 🥸

# So first thing first what is a Linked list? 🥸

Arrays can be used to store linear data of similar types, but arrays have the following limitations.

• The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.
• Inserting a new element in an array of elements is expensive because the room has to be created for the new elements and to create a room existing elements have to be shifted.

# Drawbacks:

• Random access is not allowed. We have to access elements sequentially starting from the first node.
• Extra memory space for a pointer is required with each element of the list.
• It is not cache-friendly. Since array elements are contiguous locations, there is a locality of reference which is not there in the case of linked lists.

## It consists of:

A linked list is represented by a pointer to the first node of the linked list. The first node is called the head. If the linked list is empty, then the value of the head is NULL.

Each node in a list consists of at least two parts:

• Data
• Pointer (Or Reference) to the next node

A linked list can be visualized as a chain of nodes, where every node points to the next node. See below: # Types of Linked List 🧐

• Doubly Linked List− Items can be navigated forward and backward.

• Circular Linked List− The last item (or node) contains a link of the first element (or node) as next and the first element has a link to the last element as previous.

# Basic Operations on Simple Linked List ⚡️ ☄️

Following are the basic operations supported by a list.

• Insertion− Adds an element at the beginning of the list.

• Deletion− Deletes an element at the beginning of the list.

• Display− Displays the complete list.

• Search− Searches an element using the given key.

• Delete− Deletes an element using the given key.

### Creating Node constructor to allocate value on every declaration of a Node.

``````class Node
{
public:
int data;
Node* next;
Node(int val)
{
data = val;
next = NULL;
}
};
``````

## 1. insert Node at Trail: 🤘

``````void insert_at_trail(Node* &head, int val)      // head starting of list to which
// value that we want to add
{
Node* n = new Node(val);
// Make new node "n" through constructor
{
return;
}

while (temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = n;
}
``````

## 2. Insert node as a Header node: 🤙

``````void insert_at_first(Node* &head, int val)
{
Node* n = new Node(val);
}
``````

## 3. Check if the given element is present or not: 👈

``````bool search_in_linkedlist(Node* head, int element_to_search)
{
while (temp != NULL)
{
if (temp->data == element_to_search) return true;

temp = temp -> next;
}
return false;
}
``````

## 4. Traverse and display: 👉

``````void display_linked_list(Node* head)
{
while (temp != NULL)
{
cout << temp->data << " --> ";
temp = temp -> next;
}
cout << "null" << endl;
}
``````

# 🤓

``````#include <bits/stdc++.h>
using namespace std;

class Node
{
public:
int data;
Node* next;
Node(int val)
{
data = val;
next = NULL;
}
};

void insert_at_trail(Node* &head, int val)      // head starting of list to which
// value that we want to add
{
Node* n = new Node(val);
// Make new node "n" through constructor
{
return;
}

while (temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = n;
}

{
Node* n = new Node(val);
}

{
while (temp != NULL)
{
if (temp->data == element_to_search) return true;

temp = temp -> next;
}
return false;
}

{
while (temp != NULL)
{
cout << temp->data << " --> ";
temp = temp -> next;
}
cout << "null" << endl;
}

int main()
{

}
``````

Remember these operations are only for normal LinkedList. So in the next blog, we will discuss these operations on a circular as well as on a double-headed linked list.

So till then, goodbye and have a great day ahead.