Lecture 2: Linked Lists

Contents

1. Introduction to Linked Lists...... 2 2. Array VS Linked-List...... 2 3. Array-List VS Linked-List...... 3 4. Types of Linked Lists & Operations...... 4 5. Disadvantages of Linked-List...... 5

Compiled by: Prakash Gautam https://prakashgautam.com.np/dipit02 | [email protected] 1 1. Introduction to Linked Lists A is said to be linear if its elements form a sequence or a linear list. is a linear data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference to the next node in the sequence.

Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. 1.1) Node: Each node of a linked list can store a data called an element. 1.2) Next: Each node of a linked list contains a link to the next node called next. 1.3) Each node contains at least: A piece of data (Any Type) & Pointer to the next node in the list (Pointers maybe both next & previous if we consider DLL) 1.4) Head Pointer: It always points to the head node called head. 1.5) Null: Last node (tail) points to NULL.

2. Array VS Linked-List Both are linear data structures. Array ➢ Linear order determined by indices ➢ Pre-determined fixed size ➢ Easy access to any element a[i] in constant time () ➢ No space overhead ➢ Size = N x sizeOf (element)

Compiled by: Prakash Gautam https://prakashgautam.com.np/dipit02 | [email protected] 2 Linked List ➢ No fixed size; grow one element at a time ➢ Space overhead  Each element must store at least an additional reference  Size = n x sizeOf (element) + n x sizeOf(reference) ➢ No easy access to ith element wrt the head of the list

3. Array-List VS Linked-List ArrayList LinkedList

ArrayList internally uses dynamic array to LinkedList internally uses doubly linked list store the elements to store the elements.

Manipulation with ArrayList is slow Manipulation with LinkedList is faster than because it internally uses array. If any ArrayList because it uses doubly linked list so element is removed from the array, all the no bit shifting is required in memory. bits are shifted in memory.

LinkedList class can act as a list and queue ArrayList class can act as a list only both because it implements List and Deque because it implements List only. interfaces.

ArrayList is better for storing and LinkedList is better for manipulating data. accessing data.

Compiled by: Prakash Gautam https://prakashgautam.com.np/dipit02 | [email protected] 3 4. Types of Linked Lists & Operations 4.1) Singly Linked List In any singly linked list, the individual element is called as Node. Every Node contains two fields, data and next. The data field is used to store actual value of that node and next field is used to store the address of the next node in the sequence.

10 11 100 111 Head NULL

4.1.1) Operations on SLL In a single linked list we perform the following operations: ➢ INSERTION (At First, At End, At Specific Position) ➢ DELETION (At First, At End, At Specific Position)

4.2 Doubly Linked-List In doubly linked list, every node has link to its previous node and next node. So, we can traverse forward by using next field and can traverse backward by using previous field. Every node in a double linked list contains three fields: Previous, Data & Next.

NULL NULL 10 10 10 10

4.2.1) Operations on DLL In a single linked list we perform the following operations: ➢ INSERTION (At First, At End, At Specific Position) ➢ DELETION (At First, At End, At Specific Position)

Compiled by: Prakash Gautam https://prakashgautam.com.np/dipit02 | [email protected] 4 5. Disadvantages of Linked-List ➢They use more memory than arrays because of the storage used by their pointers. ➢Nodes in a linked list must be read in order from the beginning as linked lists are inherently sequential access. ➢Nodes are stored incontiguously, greatly increasing the time periods required to access individual elements within the list, especially with a CPU cache. ➢Difficulties arise in linked lists when it comes to reverse traversing. For instance, singly linked lists are cumbersome to navigate backwards and while doubly linked lists are somewhat easier to read, memory is consumed in allocating space for a back-pointer.

THANK YOU ! Happy Math, Happy Algorithm!

Compiled by: Prakash Gautam https://prakashgautam.com.np/dipit02 | [email protected] 5