Lecture 12 Linked List
Lecture 12 Linked List
Lecture #12
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 as shown in the below image:
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.
A linked list is made up of two items that are data and a reference to the next node. A reference to the next
node is given with the help of pointers and data is the value of a node. Each node contains data and links to
the other nodes. It is an ordered collection of data elements called a node and the linear order is maintained
by pointers.
Singly Linked List: It is the most basic linked list in which traversal is unidirectional i.e. from the head
node to the last node.
Doubly Linked List: In this linked list, traversal can be done in both ways, and hence it requires
an extra pointer.
Circular Linked List: This linked list is unidirectional but in this list, the last node points to the first i.e. the
head node and hence it becomes circular in nature.
Arrow operator (->) in C++ also known as Class Member Access Operator is a combination of two
different operators that is Minus operator (-) and greater than operator (>). It is used to access the public
members of a class, structure, or members of union with the help of a pointer variable.
There is a .(dot) operator in C++ that is also used to access the members of a class. But .(dot operator)
accesses the member or variable directly means without using the pointers whereas instead of accessing
members directly the arrow operator(->) in C++ uses a pointer to access them.
So the advantage of the -> operator is that it can deal with both pointer and non-pointer access but the
dot(.) operator can only be used to access the members of a class.
int main()
{
Linkedlist list;
// Inserting nodes
list.insertNode(11);
list.insertNode(22);
list.insertNode(33);
cout << "Elements of the list are: ";
// Print the list
list.printList();
cout << endl;
int x= list.deletebyValue(33);
cout<<x;
// Delete node at position 2.
list.deleteNode(x);
Advantages
it is very easier for the accessibility of a node in the forward direction.
the insertion and deletion of a node are very easy.
the Requirement will less memory when compared to doubly, circular or doubly circular linked list.
the Singly linked list is the very easy data structure to implement.
During the execution, we can allocate or deallocate memory easily.
Insertion and deletion of elements don’t need the movement of all the elements when compared to an array.
Disadvantages
Accessing the preceding node of a current node is not possible as there is no backward traversal.
the Accessing of a node is very time-consuming.
A doubly linked list (DLL) is a special type of linked list in which each node contains a pointer to the
previous node as well as the next node of the linked list.
Every node of DLL Requires extra space for a previous pointer. It is possible to implement DLL
with a single pointer though.
All operations require an extra pointer previous to be maintained. For example, in insertion, we
need to modify previous pointers together with the next pointers. For example in the following
functions for insertions at different positions, we need 1 or 2 extra steps to set the previous pointer.
It is used by web browsers for backward and forward navigation of web pages
LRU ( Least Recently Used ) / MRU ( Most Recently Used ) Cache are constructed using Doubly Linked
Lists.
Used by various applications to maintain undo and redo functionalities.
In Operating Systems, a doubly linked list is maintained by thread scheduler to keep track of processes that
are being executed at that time.