0% found this document useful (0 votes)
22 views

Week 8

A linked list is a data structure where each element points to the next element, allowing for dynamic sizes. It consists of nodes where each node has a data field and pointer to the next node. New nodes can be added or removed from the linked list at runtime, avoiding size limitations of arrays. Common linked list operations include traversing the list, searching for elements, and deleting nodes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Week 8

A linked list is a data structure where each element points to the next element, allowing for dynamic sizes. It consists of nodes where each node has a data field and pointer to the next node. New nodes can be added or removed from the linked list at runtime, avoiding size limitations of arrays. Common linked list operations include traversing the list, searching for elements, and deleting nodes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Linked List

• One of the limitations of storing the data elements using an array is that the size
of the array must be specified at the time of creating it.

• Also, the size of the array once decided at the time of creation cannot be later
changed in the program.

• Hence, an array is a data structure of a fixed size which cannot ‘grow’ or ‘shrink’
at run time after its creation.

• This problem with arrays can be solved by creating a new data structure called
linked list
Linked List

• It is a data structure which is created as a collection of nodes such that the new
nodes can be added to the linked list or existing nodes can be removed from the
linked list at runtime of the program.

• This means that the number of nodes in the linked list can be changed at runtime
as per the requirement of the incoming data.

• As the linked list is a chain of nodes which can ‘grow’ or ‘shrink’ at runtime of the
program, it avoids the drawback with arrays because no maximum bound with
linked list unlike arrays
Structure of a node in a linked list

• Every node of a linked list consists of two parts in it.


• The first part of the node stores the data value to be stored in the list and the
second part of the node is a pointer that points to the next node thereby creating a
chain of nodes.

Structure of a node in a linked list


• The data element to be stored in a node can be a primitive or of an object type

• Along with the data to be stored, each node also has a pointer that points to
the next Node object

Basic representation of a linked list


1. data: A integer data value
2. next: A pointer to a next node present in the linked list.

The pointer next will contain NULL for a last node of the linked list.
Since storing the members data and next is a common requirement of each of the
node objects to be created in a linked list, we can create a structure Node which
defines the properties of each of the node objects as follows:
Creating a first node in a linked list
Note that the data type of the pointer next is Node itself, because the pointer next
should point to the next node in the linked list which will also be the object of the
structure Node. Hence a structure Node is called as a self-referential structure

Every linked list must have a header pointer that points to the first node of the linked
list. Let us create this pointer with a name as head

Note that the linked list will initially be empty without any nodes in it
Let us assume that we now need to store the first value (say 10) inside the linked list

So as to store this data element we need to create a node in a linked list at


runtime by using a keyword new as follows.

Hence, the pointer head now points to a newly created node


• when the node is created, we can now initialize the member variables of that
node object.

• As the data to be stored in the node is 10, we initialize the member data by using
the pointer head to store 10 as follows.

NULL indicating that there are no further


nodes in the linked list
Creation of subsequent nodes in a linked list
To understand the process to insert subsequent nodes in the linked list, let us
assume that the following linked list is already created with three nodes in it.

Existing linked list


let us now try to insert a fourth node at the end of the existing linked list

Note that the new node is now pointed by the pointer d and is not yet linked with
the existing linked list which is pointed by head
Unlinked node
• To attach the new node pointed by d at the end of the linked list pointed by
head, we need to obviously reach the last node of the linked list that is
pointer by head.

• Once we reach the last node we can make the next pointer of the last node to
point to the node object pointed by d, thereby attaching the new node at the
end of the linked list.

• Also, it is important to understand that this is a singly linked list, which means
that we can move only in one direction from header node to the last node.

• This also means that, we will not be able to directly reach any node without
visiting all the nodes previous to it. Therefore, if we need to search for a last
node in an existing linked list, we must start the searching process from the
header node and end the search once we find a node whose next is NULL.
So as to start the searching process to reach the last node, let us create a pointer
temp which will initially point to the head node
As discussed before, we now need to take the pointer temp to point to the last node

The pointer temp currently points to a node with a data value as 10 and as seen the
value of temp->next (next of the node pointed by temp) is not NULL, we move the
pointer temp ahead in the linked list by using the statement as follows
We check if the current node is a last node by looking at the next value of the current
node

As seen from the Figure the pointer temp now points to the last node,
then the next value of temp (temp->next) is now NULL
Following while loop can be used to make the pointer temp to point to the last
node after its execution

We can now make the next of the last node to point to the unlinked node d by using
the following statement
Hence, the next value of the last node will now point to the object which is
pointed by the pointer d ,attaching a new node at the end of the linked list

New node attached


The following is the complete code for the insert() function that is used to insert a
new node at the end of the linked list.
Traversing a linked list

• The process of visiting each and every node of the linked list is called as
traversing the linked list.
• We will print the data value of each node in the linked list staring from the
header node.
• Let us again assume a linked list with three nodes created
To start the traversing process, let us create a pointer temp that will initially
point to the head node by using the following statement:
As the temp points to the first node, we can now print the data value stored
inside the first node using the following cout statement

This statement will print the value 10

to now print the value of the next node, we can take the pointer temp ahead
using the following statement
As the pointer temp now points to a next node we can again execute the statement:

will print the value 20

Hence the following while loop can be used to traverse through a complete linked list
Searching an element in a linked list

• Let us now write a function find() that searches the presence of a particular
data value in a linked list.
• The function will take an integer argument(x), and search the presence of
value x starting from the first node in the linked list.

• The pointer temp is initialized to the first node of the linked list and the
traversal loop terminates if any one of the following condition is satisfied:

1. Element x is found in the linked list: We need not traverse ahead in the
list if the data element x we are searching for is found in the list. We
should continue with the search loop until the data value of the node
pointed by temp is not equal to x. This means that, the while loop should
continue traversing until the time following condition is true:
temp->data!=x
2. End of the linked list reached: If this is the case then the value of the pointer temp
will become NULL and the while loop will terminate as the condition temp!=NULL is
now false.

If the while loop terminates because of this condition, it indicates that the pointer
temp has crossed the last node of the linked list but has still not found the data
element x

This means that the while loop should also continue traversing until the time
following condition is true:

temp!=NULL
Deleting an element from the linked list
To understand the algorithm for deleting a node from the existing linked list, let
us consider a following linked list created with four nodes

Now lets delete a node with a data value 30 from the linked list
If x is the node to be deleted then we can search the node with a data value
x using the following while loop which is discussed when writing the find()

In this case the data value of node to be deleted x is 30. Therefore, the above while
loop will make the pointer temp to point to a node be deleted after the loop
completes its execution
The pointer temp points to the node to be deleted
• Even if we have found the node to be deleted and the pointer temp points to it, we
will not be able to delete this node from the linked list using the pointer temp alone.
• This is because the requirement of deletion is that the next pointer of the ‘previous
node’ of the ‘node to be deleted’ must point to the ‘next node’ of the ‘node to be
deleted’
To set the next pointer of the ‘previous node’ of the ‘node to be deleted’ we
would need one more pointer that always points to the ‘previous node’ of the
‘node to be deleted’. Let us name this pointer as prev. The job of pointer prev is
to always point to the previous node of the node to which temp points to

Also, each time we make the pointer temp to move to the next node we will store
the old value of the pointer temp inside the pointer prev. It ensures that the
pointer prev is always pointing to one node previous to pointer temp
Pointers prev and temp
To make the next of 20 to point to 40, we need to set the next pointer of the node
pointed by prev to point to the ‘next node’ of temp.
• Also, note that the first node has no node previous to it and hence if the node to
be deleted is the first node then we will need to modify the head pointer so that
the head points to the second node in the linked list

• Therefore, following block must be executed to modify the header of the linked
list if the node to be deleted is the first node of the list:
The following is the complete algorithm to delete a node with a value x from the linked
list
Creation and manipulation of the linked list
The complete source code to create a linked list is as given below. The main program is
menu driven where a user will be given the following options to perform operations
on the linked list.

1. Insertion of a node in a linked list


2. Traversing a linked list
3. Searching an element from the linked list
4. Deletion of a node from a linked list
5. Exit The user can enter an appropriate menu option to perform a specific operation
on the linked list at each iteration of the do-while loop which implements a menu
driven program
delete Keyword in C++

The keyword delete is used to free the memory that is allocated dynamically. This
helps in optimum utilization of the available heap space. The following is the syntax of
using delete keyword:

When a keyword delete is followed by the name of the pointer, system will free up the
memory pointed by the pointer.
For example, let us create an integer memory of 2 bytes dynamically and store
its address in a pointer p, which can be done by using the following statement:

After performing the operations with the memory, we can free the
memory pointed by the pointer p by using the following statement:

If the pointer p points to an array that is created dynamically then the delete
statement to free the complete array space pointed by the pointer

You might also like