0% found this document useful (0 votes)
15 views8 pages

Linked List

A linked list is a linear data structure consisting of nodes, each containing data and a pointer to the next node, allowing for dynamic memory allocation and efficient insertion and deletion. There are three main types of linked lists: singly linked lists, doubly linked lists, and circular linked lists, each with specific operations for insertion, deletion, and traversal. While linked lists offer advantages like dynamic sizing and ease of manipulation, they also have drawbacks such as higher memory usage and slower search times compared to arrays.

Uploaded by

Abdul Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Linked List

A linked list is a linear data structure consisting of nodes, each containing data and a pointer to the next node, allowing for dynamic memory allocation and efficient insertion and deletion. There are three main types of linked lists: singly linked lists, doubly linked lists, and circular linked lists, each with specific operations for insertion, deletion, and traversal. While linked lists offer advantages like dynamic sizing and ease of manipulation, they also have drawbacks such as higher memory usage and slower search times compared to arrays.

Uploaded by

Abdul Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to Linked List

What is a Linked List?


Linked List is a linear data structure which looks like a series of nodes,
where each node has two parts: data and next pointer. Unlike Arrays,
Linked List elements are not stored at a contiguous location. In the linked
list there is a head pointer, which points to the first element of the linked
list, and if the list is empty then it simply points to null or nothing.

Basic Terminologies of Linked List


• Head: The Head of a linked list is a pointer to the first node or
reference of the first node of linked list. This pointer marks the
beginning of the linked list.
• Node: Linked List consists of a series of nodes where each node
has two parts: data and next pointer.
• Data: Data is the part of node which stores the information in the
linked list.
• Next pointer: Next pointer is the part of the node which points to
the next node of the linked list.
Importance of Linked List
Here are a few advantages of a linked list that is listed below, it will help
you understand why it is necessary to know.
• Dynamic Data structure: The size of memory can be allocated or
de-allocated at run time based on the operation insertion or
deletion.
• Ease of Insertion/Deletion: The insertion and deletion of elements
are simpler than arrays since no elements need to be shifted after
insertion and deletion, Just the address needed to be updated.
• Efficient Memory Utilization: As we know Linked List is a dynamic
data structure the size increases or decreases as per the
requirement so this avoids the wastage of memory.
• Implementation: Various advanced data structures can be
implemented using a linked list like a stack, queue, graph, hash
maps, etc.
Types of Linked List
There are mainly three types of linked lists:
1. Singly linked list
2. Doubly linked list
3. Circular linked list
1. Singly Linked List
Singly Linked List is a type of linked list where each node has two
parts: data and next pointer. The data part stores the information and the
next pointer points to the next node of the linked list. The next pointer of
the last node stores null as it is the last node of the linked list and there is
no next node. Traversal of items can be done in the forward direction only
due to the linking of every node to its next node.
/ Singly linked list node in C++
class Node {
public:

// Data field
int data;

// Pointer to the next node


Node* next;

// Constructor to initialize a new node with data


Node(int new_data) {
this->data = new_data;
this->next = nullptr;
}
};
Basic Operations on Singly Linked List
The following are some basic operations performed on a Single Linked List:
• Insertion: The insertion operation can be performed in three ways.
They are as follows:
o Inserting At the Beginning of the list
o Inserting At End of the list
o Inserting At Specific location in the list
• Deletion: The deletion operation can be performed in three ways.
They are as follows:
o Deleting from the Beginning of the list
o Deleting from the End of the list
o Deleting a Specific Node
• Traverse: This process displays the elements of a Single-linked
list.
• Search: It is a process of determining and retrieving a specific node
either from the front, the end or anywhere in the list.
2. Doubly Linked List:
Doubly Linked List is a type of linked list where each node has three
parts: data, next pointer and previous pointer. The data part stores the
information, the next pointer points to the next node of the linked list and
the previous pointer points to the previous node of the linked list. The next
pointer of the last node and the previous pointer of the first node
stores null. Traversal of items can be done in the forward direction as well
as backward direction due to the linking of every node to its next node as
well as the previous node.

// Doubly linked list node in C++


class Node {
public:
// Data field
int data;

// Pointer to previous node


Node* prev;

// Pointer to the next node


Node* next;

// Constructor to initialize a new node with data


Node(int new_data) {
this->data = new_data;
this->next = nullptr;
this->prev = nullptr;
}
};

Operations on Doubly Linked List:


In a doubly linked list, we perform the following operations…
• Insertion: The insertion operation can be performed in three ways
as follows:
o Inserting At the Beginning of the list
oInserting after a given node.
o Inserting at the end.
o Inserting before a given node
• Deletion: The deletion operation can be performed in three ways
as follows…
o Deleting from the Beginning of the list
o Deleting from the End of the list
o Deleting a Specific Node
• Display: This process displays the elements of a double-linked list.
3. Circular Linked List:
A circular linked list is a type of linked list in which the first and the last
nodes are also connected to each other to form a circle, there is no NULL at
the end.
Commonly used operations on Circular Linked List:
The following operations are performed on a Circular Linked List
• Insertion: The insertion operation can be performed in three ways:
o Insertion in an empty list
o Insertion at the beginning of the list
o Insertion at the end of the list
o Insertion in between the nodes
• Deletion: The deletion operation can be performed in three ways:
o Deleting from the Beginning of the list
o Deleting from the End of the list
o Deleting a Specific Node
• Display: This process displays the elements of a Circular linked list.

Implementation of Linked List:


/ C++ program to show the implementation of singly linked
// list
#include <bits/stdc++.h>
using namespace std;

// A linked list node


class Node {
public:
int data;
Node* next;
// Constructor to initialize a new node with data
Node(int new_data) {
data = new_data;
next = nullptr;
}
};

// Function to print the linked list


void printList(Node* head) {

// Loop that runs till head is nullptr


while (head != nullptr) {

// Printing current node data


cout << " " << head->data;

// Moving to the next node in the list


head = head->next;
}
cout << "\n";
}

// Driver code
int main() {

// Create a hard-coded linked list:


// 2 -> 3 -> 4 -> 5 -> 6
Node* head = new Node(2);
head->next = new Node(3);
head->next->next = new Node(4);
head->next->next->next = new Node(5);
head->next->next->next->next = new Node(6);

// Printing the above list


cout << "Linked List:";
printList(head);

return 0;
}

Output
Linked List: 2 3 4 5 6
Linked List vs. Array:
Array Linked List

Arrays are stored in contiguous Linked Lists are not stored in contiguous
location. location.

Fixed size (Dynamic Sized Arrays


also internally use fixed sized Dynamic Size
arrays)

Only store elements no extra It stores both data and address of next
reference / pointer. node.

Elements can be access by traversing


Elements can be accessed easily in
through all the nodes till we reach the
O(1) time.
required node.

Insertion and deletion operation is Insertion and deletion operation is faster


slower than Linked List. than Array.

Advantages of Linked List:


• Dynamic nature: Linked lists are used for dynamic memory
allocation.
• Memory efficient: Memory consumption of a linked list is efficient
as its size can grow or shrink dynamically according to our
requirements, which means effective memory utilization hence, no
memory wastage.
• Ease of Insertion and Deletion: Insertion and deletion of nodes are
easily implemented in a linked list at any position.
• Implementation: For the implementation of stacks and queues and
for the representation of trees and graphs.
• The linked list can be expanded in constant time.
Disadvantages of Linked List:
• Memory usage: The use of pointers is more in linked lists hence,
complex and requires more memory.
• Accessing a node: Random access is not possible due to dynamic
memory allocation.
• Search operation costly: Searching for an element is costly and
requires O(n) time complexity.
• Traversing in reverse order: Traversing is more time-consuming
and reverse traversing is not possible in singly linked lists.
Applications of Linked List:
Here are some of the applications of a linked list:
• Linear data structures such as stack, queue, and non-linear data
structures such as hash maps, and graphs can be implemented
using linked lists.
• Dynamic memory allocation: We use a linked list of free blocks.
• Implementation of graphs: Adjacency list representation of graphs
is the most popular in that it uses linked lists to store adjacent
vertices.
• In web browsers and editors, doubly linked lists can be used to
build a forwards and backward navigation button.
• A circular doubly linked list can also be used for implementing data
structures like Fibonacci heaps.

You might also like