Linked List
A linked list is a linear data structure where each element is a separate object. Each element (we will call it a node) of a list is comprising of two items - the data and a reference to the next node. The last node has a reference to null. The entry point into a linked list is called the head of the list. It should be noted that the head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference.
Advantages of Linked Lists
- Dynamic size
- Ease of insertion/deletion
Disadvantages of Linked Lists
- Random access is not allowed. We have to access elements sequentially starting from the first node.
Types of Linked Lists
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Doubly Circular Linked List
Creating a Linked List
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
int main()
{
Node* newNode = new Node(4);
cout<< newNode->data << endl; // 4
cout<< newNode->next << endl; // 0
return 0;
}