Chapter 3 Linked List
Chapter 3 Linked List
Chapter 3 Linked List
CHAPTER 3
List
Data Structures
DATA STRUCTURE AND ALGORITHM 1
CONTENTS
List ADT
Array implementation of List ADT,
Operations
Time and space complexity of array implementation
Linked list:-
Singly linked lists,
doubly linked lists,
circular (singly and doubly) linked lists,
The elements of the array are stored respectively in successive memory locations.
Operations
Advantage:
Retrieval:
Insertion:
Deletion:
Solution:
Make MAX_LIST a variable
When array is full:
For variable-size collections, where dynamic operations such as insert/delete are common
Array is a poor choice of data structure
Limitation of Array
Changing the size of the array requires creating a new array
A linked list consists of a finite sequence of elements or nodes (not necessarily adjacent in memory) that contain
information plus (except possibly the last one) a link to another node.
Unlike an array, however, in which the linear order is determined by the array indices, the order in a linked list is
If node x points to node y, then x is called the predecessor of y and y the successor of x.
There is a link to the first element called the head of the list.
Therefore, the number of elements that may be added to a list is limited only by the amount of memory available.
The second implementation requires dynamic memory management where one can allocate memory at run-time, that is, during
Linked lists are generally implemented using dynamic memory management. Node *head = new Node;
A linked list is an ordered collection of data in which each element (node) contains a minimum of two values,
data and link(s) to its successor (and/or predecessor).
If a node has a link only to its successor in this sequence, the list is called a singly linked list.
Each node of the linked list has at least the following two elements:
Head
The last node in the list contains a null pointer (or a suitable value like -1) to indicate that it is the end or tail of the list
For handling such difficulties, we can use DLLs where each node contains two links,
one to its predecessor and other to its successor
Each node of a DLL has three fields in general but must have at least two link fields
In SLL, we cannot reach any of the nodes that precede the Current node
The above problem is not the case in DLL but the problem can be solved by….
Instead of that, store the address of the first node of the list in that link field.
the last node’s next link is set to the first node of the list and
the first node’s previous link is set to the last node of the list.
This gives access to the last node directly from the first node
The following are basic operations associated with the linked list as a data structure:
Inserting a node
Deleting a node
Some more operations, which are based on the basic operations, are as follows:
Searching a node, Updating a node, Printing the node or list, Counting the length of the list, Reversing the list, Sorting the list
using pointer manipulation, Concatenating two lists, Merging two sorted lists into a third sorted list
Head
56 12 70 8
Head
5 56 12 70 8
Head
5 56 12 70 8
Head
5 56 12 70 8
56 12 70 8
InsertAtEnd(Node head, x)
1. Create newNode
2. newNode.data <- x
3. cur <- head
4. while cur != Null
5. do
6. if cur.next = Null
7. cur.next <- newNode
8. return
9. cur ← cur.next
DATA STRUCTURE AND ALGORITHM 20
BASIC OPERATIONS – INSERTION AT THE END OF THE LIST
Head
56 12 70 8
Head
56 12 70 8 5
Head
56 12 70 8 5
Head
56 12 70 8 5
5
Head
56 12 70 8
5
Head
56 12 70 8
5
Head
DATA STRUCTURE AND ALGORITHM
56 12 70 8 22
BASIC OPERATIONS – DELETION AT THE BEGINNING OF THE
LIST
Head
56 12 70 8
Head
56 12 70 8
Head
56 12 70 8
Head
12 70 8
Head
56 12 70 8
Head
56 12 70 8
Head
56 12 70 8
Head
56 70 8
Head
56 12 70 8
Head
56 12 70 8
Head
56 12 70 8
Head
56 12 70
Advantages
1) Dynamic size
2) Ease of insertion/deletion
Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the first node.
2) Extra memory space for a pointer is required with each element of the list.
3) Not cache friendly. Since array elements are contiguous locations, there is locality of reference which is not
there in case of linked lists.
2. It provides static allocation, which means, the space allocation done by a compiler once cannot be changed during execution,
and the size has to be known in advance.
3. As individual objects are stored a fixed distance apart, we can access any element randomly.
4. Insertion and deletion of objects in between the list require a lot of data movement.
5. It is space inefficient for large objects with frequent insertions and deletions.
6. An element need not know/store and keep the address of its successive element.