2. Learning Objectives
âť–Linear and Non-linear DS
âť–Introduction
âť–Linked list representation
âť–Memory allocation for a Linked List
âť–Memory de-allocation for a Linked List
âť–Array vs Linked list
âť–Types of Linked Lists
âť–Singly Linked list
âť–Doubly Linked list
2PREPAREDBY NURJAHANNIPA
3. Continue…
âť–Circular Linked Lists
âť–Difference between Linear Linked list VS Circular Linked List
âť–Circular Doubly Linked List
âť–Insertion in a linked list
âť–Deletion from a linked list
âť–Garbage Collection
âť–Overflow and Underflow
âť–Applications of linked list in computer science.
âť–Applications of linked list in real world.
âť–Application of Circular Linked Lists
3PREPAREDBY NURJAHANNIPA
4. Introduction
âť–Data structure where data elements are arranged sequentially or linearly where the elements
are attached to its previous and next adjacent in what is called a linear data structure.
âť–Data structures where data elements are not arranged sequentially or linearly are called non-
linear data structures.
4PREPAREDBY NURJAHANNIPA
5. Introduction
âť‘We have studied that an 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. For example, if we
declare an array as int marks[10], then the array can store a maximum of 10 data elements but
not more than that. But 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.
âť‘Linked list is a data structure that is free from the aforementioned restrictions. A linked list
does not store its elements in consecutive memory locations and the user can add any number
of elements to it.
PREPAREDBY NURJAHANNIPA 5
6. Introduction
âť‘In Fig. 6.1, we can see a linked list in which every node contains two parts, an integer and a
pointer to the next node. Linked lists contain a pointer variable START that stores the address of
the first node in the list.
âť‘ The left part of the node which contains data may include a simple data type, an array, or a
structure.
âť‘The right 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 special value
called NULL. In Fig. 6.1, the NULL pointer is represented by X.
❑While programming, we usually define NULL as –1. Hence, a NULL pointer denotes the end of
the list.
6PREPAREDBY NURJAHANNIPA
7. Basic Terminologies
You have to start somewhere, so we give the address of the first node a special name
called HEAD.
Also, the last node in the LinkedList can be identified because its next portion points to NULL.
It acts as a building block to implement data structures such as stacks, queues.
7PREPAREDBY NURJAHANNIPA
8. Basic Terminologies (Continue..)
In the figure, we can see that the variable START is used to store
the address of the first node. Here, in this example, START = 1, so
the first data is stored at address 1, which is H. The corresponding
NEXT stores the address of the next node, which is 4. So, we will
look at address 4 to fetch the next data item.
The second data element obtained from address 4 is E. Again, we
see the corresponding NEXT to go to the next node. From the
entry in the NEXT, we get the next address, that is 7, and fetch L
as the data. We repeat this procedure until we reach a position
where the NEXT entry contains –1 or NULL, as this would denote
the end of the linked list. When we traverse DATA and NEXT in
this manner, we finally see that the linked list in the above
example stores characters that when put together form the word
HELLO.
PREPAREDBY NURJAHANNIPA 8
9. Basic Terminologies (Continue..)
Let us take another example to see how two linked lists are
maintained together in the computer’s memory. For
example, the students of Class XI of Science group are asked
to choose between Biology and Computer Science.
Now, we will maintain two linked lists, one for each subject.
That is, the first linked list will contain the roll numbers of all
the students who have opted for Biology and the second list
will contain the roll numbers of students who have chosen
Computer Science.
By looking at the figure, we can conclude that roll numbers
of the students who have opted for Biology are S01, S03,
S06, S08, S10, and S11.
Similarly, roll numbers of the students who chose Computer
Science are S02, S04, S05, S07, and S09.
PREPAREDBY NURJAHANNIPA 9
10. Basic Terminologies (Continue..)
Consider a scenario in which the roll
number, name, aggregate, and grade of
students are stored using linked lists.
Now, we will see how the NEXT pointer is
used to store the data alphabetically.
This is shown in Fig. 6.4.
PREPAREDBY NURJAHANNIPA 10
12. Memory allocation for a Linked List(Cont..)
âť‘We have seen how a linked list is represented in the memory. If we want to add a node to an
already existing linked list in the memory, we first find free space in the memory and then use it
to store the information.
âť‘For example, consider the linked list shown in Fig. 6.5. The linked list contains the roll number
of students, marks obtained by them in Biology, and finally a NEXT field which stores the address
of the next node in sequence.
âť‘ Now, if a new student joins the class and is asked to appear for the same test that the other
students had taken, then the new student’s marks should also be recorded in the linked list. For
this purpose, we find a free space and store the information there.
âť‘In Fig. 6.5 the grey shaded portion shows free space, and thus we have 4 memory locations
available. We can use any one of them to store our data. This is illustrated in Figs 6.5(a) and (b)
PREPAREDBY NURJAHANNIPA 12
13. Memory allocation for a Linked List(Cont..)
âť‘We have seen that every linked list has a pointer variable START which stores the address of
the first node of the list.
âť‘Likewise, for the free pool (which is a linked list of all free memory cells), we have a pointer
variable AVAIL which stores the address of the first free space. Let us revisit the memory
representation of the linked list storing all the students’ marks in Biology.
❑Now, 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. For example, in Fig. 6.6, when the first free memory
space is utilized for inserting the new node, AVAIL will be set to contain address 6.
PREPAREDBY NURJAHANNIPA 13
14. Memory de-allocation for a Linked List
14PREPAREDBY NURJAHANNIPA
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. The operating system will perform
this operation whenever it finds the CPU idle or whenever the
programs are falling short of memory space.
The operating system scans through all the memory cells and
marks those cells that are being used by some program. Then it
collects all the cells which are not being used and adds their
address to the free pool, so that these cells can be reused by
other programs. This process is called garbage collection.
15. Types of Linked List
There are 3 different implementations of Linked List available, they are:
âť–Singly Linked List
âť–Doubly Linked List
âť–Circular Linked List
15PREPAREDBY NURJAHANNIPA
16. Singly Linked List
A singly linked list is the simplest type of linked list in which every node contains some data and
a pointer to the next node of the same data type. By saying that the node contains a pointer to
the next node, we mean that the node stores the address of the next node in sequence.
A singly linked list allows traversal of data only in one way.
16PREPAREDBY NURJAHANNIPA
18. Doubly Linked List
In a doubly linked list, each node contains a data part and two addresses, one for
the previous node and one for the next node.
18PREPAREDBY NURJAHANNIPA
20. Continue…
â–Ş Each node points to not only successor but the predecessor.
â–Ş There are two NULL: at the first and last nodes in the list
â–Ş Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists
backwards.
The primary disadvantage of doubly linked lists are that
â–Ş Each node requires an extra pointer, requiring more space, and
â–Ş The insertion or deletion of a node takes a bit longer (more pointer operations).
20PREPAREDBY NURJAHANNIPA
21. Circular Linked List
âť‘In a circular linked list, the last node contains a pointer to the first node of the list. We can have
a circular singly linked list as well as a circular doubly linked list. While traversing a circular linked
list, we can begin at any node and traverse the list in any direction, forward or backward, until
we reach the same node where we started. Thus, a circular linked list has no beginning and no
ending.
21PREPAREDBY NURJAHANNIPA
24. Linear linked list vs Circular Linked Lists
In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list
must be preserved in order to be able to reference the list again.
Circular linked lists can be used to help the traverse the same list again and again if needed. A
circular list is very similar to the linear list where in the circular list the pointer of the last node
points not NULL but the first node.
PREPAREDBY NURJAHANNIPA 24
25. Basic Operations on linked list
âť–Creation
âť–Insertion
âť–Deletion
âť–Traversal
âť–Search
âť–Concatenation
âť–Display
PREPAREDBY NURJAHANNIPA 25
26. Creation of a node
Create
Create operation is used to create constituent node when required.
In create operation, memory must be allocated for one node and assigned to head as follows.
Creating first node
head = (node*) malloc (sizeof(node));
head -> data = 20;
head -> next = NULL;
PREPAREDBY NURJAHANNIPA 26
27. Inserting a new node in a linked list
Case 1: The new node is inserted at the beginning.
Case 2: The new node is inserted at the end.
Case 3: The new node is inserted in the middle.
PREPAREDBY NURJAHANNIPA 27
28. Case 1: At the beginning of the list
âť‘we first check whether memory is available for the new node. If the free memory has exhausted,
then an OVERFLOW message is printed.
âť‘Otherwise, if a free memory cell is available, then we allocate space for the new node. Set its DATA
part with the given VAL and the NEXT part is initialized with the address of the first node of the list,
which is stored in START.
âť‘Now, since the new node is added as the first node of the list, it will now be known as the START
node, that is, the START pointer variable will now hold the address of the NEW_NODE.
PREPAREDBY NURJAHANNIPA 28
29. Case 2: Insertion in the middle of a
linked list (Cont…)
Adding a new node in linked list is a more than one step activity. We shall learn this with
diagrams here. First, create a node using the same structure and find the location where it has
to be inserted. Imagine that we are inserting a node E (NewNode), between B (LeftNode)
and C (RightNode).
PREPAREDBY NURJAHANNIPA 29
30. Case 3: The new node is inserted at the end
âť‘Consider the linked list shown in Fig. 6.14. Suppose we want to add a new node with data 9 as
the last node of the list. Then the following changes will be done in the linked list.
âť‘Figure 6.15 shows the algorithm to insert a new node at the end of a linked list. In Step 6, we
take a pointer variable PTR and initialize it with START. That is, PTR now points to the first node
of the linked list. In the while loop, we traverse through the linked list to reach the last node.
Once we reach the last node, in Step 9, we change the NEXT pointer of the last node to store the
address of the new node. Remember that the NEXT field of the new node contains NULL, which
signifies the end of the linked list.
PREPAREDBY NURJAHANNIPA 30
31. Deleting node from a linked list
âť‘Case 1: Delete a node from the beginning.
âť‘Case 2: Delete the end node.
âť‘Case 3: Delete a node from middle.
PREPAREDBY NURJAHANNIPA 31
32. Case 1: Delete a node from the beginning.
âť‘When removing the node at the beginning of
the list, there is no relinking of nodes to be
performed, since the first node has no preceding
node. For example, removing node with a:
âť‘However, we must fix the pointer to the
beginning of the list:
PREPAREDBY NURJAHANNIPA 32
33. Case 2: Delete the end node.
âť‘Removing a node from the end requires that the preceding node becomes the new end of the
list (i.e., points to nothing after it). For example, removing the node with c:
âť‘Note that the last two cases (middle and end) can be combined by saying that "the node
preceding the one to be removed must point where the one to be removed does."
PREPAREDBY NURJAHANNIPA 33
34. Case 3: Delete a node from middle.
âť‘Removing a node from the middle requires that
the preceding node skips over the node being
removed. For example, removing the node with b:
âť‘This means that we need some way to refer
to the node before the one we want to remove.
PREPAREDBY NURJAHANNIPA 34
36. Header linked list
Grounded header list- It is a list whose last node contains the NULL pointer. In the header linked
list the start pointer always points to the header node. start -> next = NULL indicates that the
grounded header linked list is empty. The operations that are possible on this type of linked list
are Insertion, Deletion, and Traversing.
PREPAREDBY NURJAHANNIPA 36
37. Circular Header Linked List
A list in which last node points back to the header node is called circular linked list. The chains
do not indicate first or last nodes. In this case, external pointers provide a frame of reference
because last node of a circular linked list does not contain the NULL pointer. The possible
operations on this type of linked list are Insertion, Deletion and Traversing.
PREPAREDBY NURJAHANNIPA 37
38. Adding two polynomials using Linked List
Input: 1st number = 5x^2 + 4x^1 + 2x^0 2nd number = 5x^1 + 5x^0 Output: 5x^2 + 9x^1 + 7x^0
PREPAREDBY NURJAHANNIPA 38
39. Array vs Linked List
ARRAY LINKED LIST
Array is a collection of elements of similar data type. Linked List is an ordered collection of elements of
same type, which are connected to each other using
pointers.
Array supports Random Access, which means
elements can be accessed directly using their index,
like arr[0] for 1st element, arr[6] for 7th element
etc.
Hence, accessing elements in an array is fast with a
constant time complexity of O(1).
Linked List supports Sequential Access, which
means to access any element/node in a linked list,
we have to sequentially traverse the complete
linked list, upto that element.
To access nth element of a linked list, time
complexity is O(n)
39PREPAREDBY NURJAHANNIPA
40. Array vs Linked List
Array Linked List
In array, Insertion and Deletion operation takes
more time, as the memory locations are consecutive
and fixed.
In case of linked list, a new element is stored at the
first free and available memory location, with only a
single overhead step of storing the address of
memory location in the previous node of linked list.
Insertion and Deletion operations are fast in linked
list.
In an array, elements are stored in contiguous
memory location or consecutive manner in the
memory.
In a linked list, new elements can be stored
anywhere in the memory.
Address of the memory location allocated to the
new element is stored in the previous node of
linked list, hence forming a link between the two
nodes/elements.
40PREPAREDBY NURJAHANNIPA
41. Array vs Linked List
Array Linked List
In array, each element is independent and can be
accessed using it's index value.
In case of a linked list, each node/element points to
the next, previous, or maybe both nodes.
Array can single dimensional, two
dimensional or multidimensional
Linked list can
be Linear(Singly), Doubly or Circular linked list.
Size of the array must be specified at time of array
decalaration.
Size of a Linked list is variable. It grows at runtime,
as more nodes are added to it.
Array gets memory allocated in the Stack section. Whereas, linked list gets memory allocated
in Heap section.
Memory is allocated as soon as the array is
declared, at compile time. It's also known as Static
Memory Allocation.
Memory is allocated at runtime, as and when a new
node is added. It's also known as Dynamic Memory
Allocation.
41PREPAREDBY NURJAHANNIPA
44. Applications of linked list in real world-
❑Image viewer – Previous and next images are linked, hence can be accessed by next and
previous button.
❑Previous and next page in web browser – We can access previous and next url searched in web
browser by pressing back and next button since, they are linked as linked list.
❑Music Player – Songs in music player are linked to previous and next song. you can play songs
either from starting or ending of the list.
44PREPAREDBY NURJAHANNIPA
45. Applications of linked list in computer
science
âť–Implementation of stacks and queues
âť–Implementation of graphs : Adjacency list representation of graphs is most popular which uses linked list
to store adjacent vertices.
âť–Dynamic memory allocation : We use linked list of free blocks.
âť–Maintaining directory of names
âť–Performing arithmetic operations on long integers
âť–Manipulation of polynomials by storing constants in the node of linked list
âť–representing sparse matrices
45PREPAREDBY NURJAHANNIPA
46. Applications of Circular Linked Lists:
❖Useful for implementation of queue. Unlike this implementation, we don’t need to maintain
two pointers for front and rear if we use circular linked list. We can maintain a pointer to the last
inserted node and front can always be obtained as next of last.
âť–Circular lists are useful in applications to repeatedly go around the list. For example, when
multiple applications are running on a PC, it is common for the operating system to put the
running applications on a list and then to cycle through them, giving each of them a slice of time
to execute, and then making them wait while the CPU is given to another application. It is
convenient for the operating system to use a circular list so that when it reaches the end of the
list it can cycle around to the front of the list.
âť–Circular Doubly Linked Lists are used for implementation of advanced data structures
like Fibonacci Heap.
46PREPAREDBY NURJAHANNIPA
47. References
1. Schaum’s Outline Series of Data Structures by Seymour Lipschutz
2. Data Structures Using C by Reema Thareja
3. https://www.codesdope.com/course/data-structures-linked-lists/
PREPAREDBY NURJAHANNIPA 47