6-2-Types of Linked List
6-2-Types of Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory
locations. The elements in a linked list are linked using pointers. In simple words, a linked list consists of
nodes where each node contains a data field and a reference(link) to the next node in the list.
Singly linked list is the simplest type of linked list in which every node contains some data and a
pointer to the next node of the same data type.
The node contains a pointer to the next node means that the node stores the address of the next node in
the sequence. A single linked list allows the traversal of data only in one way. Below is the image for the
same:
2
// and traversal of Singly Linked List
4
#include <bits/stdc++.h>
5
using namespace std;
7
class Node {
8
public:
9
int data;
10
Node* next;
11
Node(int data) {
12
this->data = data;
13
this->next = nullptr;
14
}
15
};
16
Output
1 2 3
Therefore, it contains three parts of data, a pointer to the next node, and a pointer to the previous node.
This would enable us to traverse the list in the backward direction as well.
1
// Define the Node structure
2
struct Node {
3
int data;
4
Node* next;
5
Node* prev;
1
// C++ program to illustrate creation
2
// and traversal of doubly Linked List
3
#include <iostream>
4
using namespace std;
6
struct Node {
7
int data;
8
Node* next;
9
Node* prev;
10
11
Node(int x) {
12
data = x;
13
next = nullptr;
14
prev = nullptr;
15
}
16
};
17
18
void forwardTraversal(Node* head) {
19
Node* curr = head;
20
while (curr != nullptr) {
21
cout << curr->data << " ";
22
Output
Forward Traversal:
1 2 3
Backward Traversal:
3 2 1
While traversing a circular linked list, we can begin at any node and traverse the list in any direction
forward and backward until we reach the same node we started. Thus, a circular linked list has no
beginning and no end. Below is the image for the same:
Node(int value) {
data = value;
next = nullptr;
}
};
1
// C++ program to illustrate creation
2
// and traversal of Circular Linked List
3
#include <iostream>
4
using namespace std;
6
struct Node {
7
int data;
8
Node* next;
10
Node(int value) {
11
data = value;
12
next = nullptr;
13
}
14
};
15
16
17
void printList(Node* last){
Output
2 3 4
Doubly Circular linked list or a circular two-way linked list is a complex type of linked list that contains a
pointer to the next as well as the previous node in the sequence. The difference between the doubly
linked and circular doubly list is the same as that between a singly linked list and a circular linked
list. The circular doubly linked list does not contain null in the previous field of the first node.
// Structure of a Node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
1
// C++ program to illustrate creation
2
// and traversal of doubly circular Linked List
3
#include <iostream>
4
using namespace std;
6
struct Node {
7
int data;
8
Node* next;
9
Node* prev;
10
11
Node(int x) {
12
data = x;
13
next = nullptr;
14
prev = nullptr;
15
16
}
17
};
18
19
void forwardTraversal(Node* head) {
20
Node* curr = head;
21
do {
22
cout << curr->data << " ";
23
curr = curr->next;
24
} while (curr != head);
Output
Forward Traversal:
1 2 3
Backward Traversal:
3 2 1
6. Skip-List :
A skip list is a data structure that allows for efficient search, insertion and deletion of elements
in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is
determined through a probabilistic analysis. Skip lists are implemented using a technique called
“coin flipping.” In this technique, a random number is generated for each insertion to determine
the number of layers the new element will occupy. This means that, on average, each element will
be in log(n) layers, where n is the number of elements in the bottom layer. Please refer to
Introduction of Skip List.
Note: Header Linked Lists are not a distinct type of linked list but rather a technique used within
existing linked list structures, such as singly or doubly linked lists. This technique involves using
a dummy node, also known as a header node, to simplify operations like insertion, deletion, and
traversal. Please refer to Header Linked Lists for implementation.