1. Data Structure Module 04
Linked Lists
A.B.Vartak
Department of Information Technology
Mumbai University
2. • Array is a linear collection of data elements in which the elements are stored
in consecutive memory locations
• While declaring arrays, we have to specify the size of the array, which will
restrict the number of elements that the array can store.
• What if we are not sure of the number of elements in advance?
• Moreover, to make efficient use of memory, the elements must be stored
randomly at any location rather than in consecutive locations.
• So, there must be a data structure that removes the restrictions on the
maximum number of elements and the storage condition to write efficient
programs.
• But like an array, insertions and deletions should be possible at any point in a
constant time. The one such data structure is linked list.
• Another advantage of a linked list over an array is that we can add any number
of elements in the list.
Need of Linked List (Array vs linked list)
3. • A linked list is a linear collection of data elements called nodes in which
linear representation is given by links from one node to the next node.
• Similar to array, it is a linear collection of data elements of the same type.
• Data elements of linked list are generally not lined in consecutive memory
space; instead they are dispersed in various locations.
• Elements in a linked list can be accessed only in a sequential manner (does
not allow random access )
• Linked list data structure can be used to implement other data structures.
Thus, it acts as building block to implement data structures like stacks,
queues and their variations.
• A linked list can be perceived as a train or a sequence of nodes in which
each node contains one or more data fields and a pointer to the next node.
Introduction to Linked List
4. Linked list element (node) is user defined structure data type, typically contains
two parts
data variables
pointers to next elements, hold the addresses of next elements
Since in a linked list, every node contains a pointer to another node which is of the
same type, it is also called a self-referential data type.
Example:
struct node {
int data; // data
struct node *next; // pointer to next element
};
Introduction to Linked List
6. Figure: Two Linked Lists Simultaneously maintained in Memory
By looking at the figure, we can
conclude that roll numbers of the
students who have opted for
Biology are S01, ?__, __, __, __,
and __.
Similarly, roll numbers of the
students who chose Computer
Science are S02, __, __, __, and
__.
7. Figure: Linked list with AVAIL and START pointers
Pointer variable START stores the
address of the first node of the
list and pointer variable AVAIL
stores the address of the first
free space for the free pool
(which is a linked list of all free
memory cells).
Memory allocation and
de-allocation for
a Linked List
8. • The computer maintains a list of all free memory cells. This list of
available space is called the free pool.
• When a new student’s record has to be added, the memory address
pointed by AVAIL will be taken and used to store the desired
information. After the insertion, the next available free space’s
address will be stored in AVAIL.
• When we delete a particular node from an existing linked list or delete
the entire linked list, the space occupied by it must be given back to
the free pool so that the memory can be reused by some other
program that needs memory space.
• The operating system does this task of adding the freed memory to
the free pool.
• This process is called garbage collection .
Memory allocation and de-allocation for a Linked List
9. • In the above linked list, every node contains two parts - one integer and the other
a pointer to the next node.
• The data part of the node which contains data may include a simple data type, an
array or a structure.
• The pointer part of the node contains a pointer to the next node (or address of
the next node in sequence).
• The last node will have no next node connected to it, so it will store a NULL value.
• A linked list is defined by the pointer pointing to the first node, e. g. START
• If START = NULL, then the linked list is empty and contains no nodes.
Singly Linked List
1 2 3 4 5 6 7 X
START
11. Linked List Operations
1. Create a node and linked list
2. Traversal, e.g. display all elements
3. Search node
4. Insert node
at beginning, at end, after/before a node
5. Delete node
at beginning, at end, after/before a node
6. Sort
12. A node is a structure data type, can be created in
two methods, statically and dynamically.
• Static method
– use array of structures
– declared as globally outside functions
– declared locally within a function
• Dynamic method (mostly used for linked list)
– use stdlib function malloc(size) get memory space
struct node *np = (struct node*) malloc(sizeof(struct node));
1. How to create nodes
13. How to create nodes continued...
struct node *np = (struct node*) malloc(sizeof(struct node));
At run time, OS allocates consecutive sizeof(struct node)
bytes in the heap region,
return the address of the address of the first memory
cell,
store the address to struct node type pointer np.
Need (struct node*) to cast the return address to struct
node pointer value.
Need use free(np) to release when deleting the node!!!
Otherwise cause memory leaking
14. 2. Traversing a Linked List
• We can traverse the entire linked list using a single pointer
variable called START.
• The START node contains the address of the first node; the next
part of the first node in turn stores the address of its
succeeding node.
• Using this technique the individual nodes of the list will form a
chain of nodes.
• If START = NULL, this means that the linked list is empty and
contains no nodes.
15. • For traversing the linked list, we also make use of another pointer variable PTR which
points to the node that is currently being accessed.
• Here processing an element may even be simply printing it.
17. 3. Searching for a value in a Linked List
• Searching means finding whether a given value is present in the
information part of the node or not.
• If it is present, the algorithm returns the address of the node
that contains the value.
Figure: Algorithm to
search a linked list
19. 4. Inserting a new node in a Linked List
• How a new node is added into an already existing
linked list?
• Four cases: -
Case 1: The new node is inserted at the beginning. (PushFront)
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.
20. Case1: The new node is inserted at the beginning.
hea
d
7 10 4 13
PushFro
nt
21. PushFro
nt
hea
d
7 10 4 13
26
Step1: Allocate memory for the new node and initialize its
DATA part (let’s assume 26).
22. PushFro
nt
hea
d
7 10 4 13
26
Step 2: Add the new node as the first node of the list by making the
NEXT part of the new node contain the address of START.
27. Case2: Inserting a Node at the End of a Linked List
PushBac
k
hea
d
7 10 4 13
28. hea
d
7 10 4 13
8
PushBac
k
Step1: Allocate memory for the new node and initialize its
DATA part (let’s assume 8) and NEXT part to NULL.
29. Head /
Start
7 10 4 13
8
PushBac
k
Step2: Take a pointer variable PTR which points to START.
Move PTR so that it points to the last node of the list.
PTR
30. PushBack time complexity
O(n)
Head /
Start
7 10 4 13
8
PTR
PushBac
k
Step3: Add the newly created node after the node pointed
by PTR. This is done by storing the address of the new node
in the NEXT part of PTR.
32. Case 3: The new node is inserted after a given
node.
PushAfter 3
34. PushAfter
Step2: Take two pointer variables PTR and PREPTR and initialize
them with START so that START, PTR, and PREPTR point to the
first node of the list.
35. PushAfter
Step3: Move PTR and PREPTR until the
DATA part of PREPTR= value of the node after which insertion
has to be done.
PREPTR will always point to the node just before PTR.
36. PushAfter
Step4: Add the new node in between the nodes pointed by
PREPTR and PTR.
PushAfter time complexity O(n)
41. Step 3: Move PTR and PREPTR until the
DATA part of PTR= value of the node
before which insertion has to be done. PREPTR will always point to
the node just before PTR
PushBefo
re
42. Step 4: Insert the new node in between the nodes pointed by
PREPTR and PTR
PushBefo
re
PushBefore time complexity O(n)
45. 5. Deleting a node from a Linked List
Three cases:
Case 1: The first node is deleted.
Case 2: The last node is deleted.
Case 3: The node after a given node is deleted.
46. • Underflow is a condition that occurs when we try to delete a
node from a linked list that is empty.
• This happens when START = NULL or when there are no more
nodes to delete.
• When a node is deleted from a linked list, the memory occupied
by that node will be freed up.
• The memory is returned to the free pool so that it can be used to
store other programs and data.
• Whatever be the case of deletion, the AVAIL pointer is always
changed so that it points to the address that has been recently
vacated.
UNDERFLOW IN LINKED LISTS
61. • Circular linked lists are widely used in
operating systems for task maintenance.
• When we are surfing the Internet, we
can use the Back button and the
forward button to move to the previous
pages that we have already visited.
• A circular linked list is used to maintain
the sequence of the Web pages visited.
• Traversing this circular linked list either
in forward or backward direction helps
to revisit the pages.
62. We can traverse the list until we find the NEXT entry that
contains the address of the start node of the list. This
denotes the end of the linked list, that is, the node that
contains the address of the first node is actually the last
node of the circular linked list.
72. • Doubly linked list calls for more space per node and more
expensive basic operations.
• However, a doubly linked list provides the ease to
manipulate the elements of the list as it maintains
pointers to nodes in both the directions (forward and
backward).
• The main advantage of using a doubly linked list is that it
makes searching twice as efficient.
DOUBLY LINKED LISTS continued…