Chapter 3 Linked ListNEW
Chapter 3 Linked ListNEW
Chapter 3 Linked ListNEW
Arrays store multiple data of similar types with the same name.
It allows random access to elements.
As the array is of fixed size and stored in contiguous memory locations there is no memory shortage or overflow.
It is helpful to store any type of data with a fixed size.
Since the elements in the array are stored at contiguous memory locations it is easy to iterate in this data structure
and unit time is required to access an element if the index is known.
The structure can store different types of data whereas an array can only store similar data types.
Structure does not have limited size like an array.
Structure elements may or may not be stored in contiguous locations but array elements are stored in contiguous
locations.
In structures, object instantiation is possible whereas in arrays objects are not possible.
List
Data Structures
DATA STRUCTURE AND ALGORITHM 6
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.
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:
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
In SLL, each node provides information about where the next node is.
It has no knowledge about where the previous node is.
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:
Creating an empty list
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
DATA STRUCTURE AND ALGORITHM 21
INSERTION ALGORITHM VS C++ IMPLEMENTATION
Head 56
12 70 8
Head 56
5 12 70 8
Head 56
5 12 70 8
Head 56
5 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 25
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 56
DATA STRUCTURE AND ALGORITHM
12 70 8 27
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.
DATA STRUCTURE AND ALGORITHM 35
COMPARISON OF SEQUENTIAL AND LINKED ORGANIZATIONS
Linked organization The features of this organization include the following:
1. Elements can be placed anywhere in the memory.
2. Dynamic allocation (size need not be known in advance), that is, space allocation as per need can be done during
execution.
3. As objects are not placed in consecutive locations at a fixed distance apart, random access to elements is not possible.
4. Insertion and deletion of objects do not require any data shifting.
5. It is space efficient for large objects with frequent insertions and deletions.
6. Each element in general is a collection of data and a link. At least one link field is a must.
7. Every element keeps the address of its successor element in a link field.
8. The only burden is that we need additional space for the link field for each element. However, additional space is not a
severe penalty when large objects are to be stored.
9. Linked organization needs the use of pointers and dynamic memory allocation.
DATA STRUCTURE AND ALGORITHM 36
QUIZ 1