Chapter Four
Linked Lists
Wachemo University(Durame Campus)
School of Computing and Informatics
Department of Computer Science
Mr. Abraham Wolde(2023)
1
OUTLINES
Introduction
Review on Pointers, Dynamic Memory
allocation and De-allocation
Types of Linked Lists(Singly Linked List and
Doubly Linked List)
2
Introduction
If the memory is allocated for the variable
during the compilation (i.e. before execution)
of a program, then it is fixed and cannot be
changed.
For example, an array A[100] is declared with
100 elements, then the allocated memory is
fixed and cannot be decreased and increased
the SIZE of the array if required.
3
Linked List
There is a special data structure called linked list that provides a
more flexible storage system and it does not require the use of
arrays.
A linked list is a linear collection of specially designed data
elements, called nodes, linked to one another by means of
pointers.
Each node is divided into two parts:
1. The first part contains the information of the elements, and
2. The second part contains the address of the next node in the
linked list.
Address part of the node is also called linked or next field.
4
5
Linked List
Each node is pictured with two parts.
The left part of each node contains the data items and the
right part represents the address of the next node; there is an
arrow drawn from it to the next node.
The next pointer of the last node contains a special value,
called the NULL pointer, which does not point to any
address of the node.
That is NULL pointer indicates the end of the linked list.
START pointer will hold the address of the 1st node in the
list START =NULL if there is no list (i.e.; NULL list or
empty list).
6
Linked list have many advantages and some of them are:
1. Linked list are dynamic data structure. That is, they can
grow or shrink during the execution of a program.
2. Efficient memory utilization: In linked list (or dynamic)
representation, memory is not pre-allocated. Memory is
allocated whenever it is required. And it is deallocated
(or removed) when it is not needed.
3. Insertion and deletion are easier and efficient. Linked list
provides flexibility in inserting a data item at a specified
position and deletion of a data item from the given
position.
4. Many complex applications can be easily carried out
with linked list.
7
Linked list has following disadvantages
1. More memory: to store an integer number, a
node with integer data and address field is
allocated. That is more memory space is needed.
2. Access to an arbitrary data item is little bit
cumbersome and also time consuming.
8
Operation on linked list
The primitive operations performed on the
linked list are as follows
Creation
Insertion
Deletion
Traversing
Searching
Concatenation
9
Operation on linked list
Creation operation is used to create a linked list.
Once a linked list is created with one node.
Insertion operation is used to insert a new node at
any specified location in the linked list. A new node
may be inserted.
a) At the beginning of the linked list
b) At the end of the linked list
c) At any specified position in between in a linked
list
10
Operation on linked list
Deletion operation is used to delete an item (or
node) from the linked list. A node may be
deleted from the
(a) Beginning of a linked list
(b) End of a linked list
(c) Specified location of the linked list
11
Operation on linked list
Traversing is the process of going through all the
nodes from one end to another end of a linked list.
In a singly linked list we can visit from left to
right, forward traversing, nodes only.
But in doubly linked list forward and backward
traversing is possible.
12
Review on Pointers, Dynamic Memory
allocation and De-allocation
Although C++ supports all the functions (i.e., malloc,
calloc, realloc and free) used in C, it also defines two
unary operators new and delete that performs the task
of allocating and freeing the memory in a better and
easier way.
An object (or variable) can be created by using new,
and destroyed by using delete, as and when required.
A data object created inside a block with new, will
remain in existence until it is explicitly destroyed by
using delete.
13
Cont…
The new operator can be used to create objects
of any type. It takes the following general form:
Pointer_Variable = new data_type;
14
Cont…
Here, Pointer_Variable is a pointer of type
data_type.
The new operator allocates sufficient memory to
hold a data object of type data_type and returns
the address of the object.
The Pointer_Variable holds the address of the
memory space allocated. For example:
int *Var1 = new int;
float *Var2 = new float;
15
Cont….
Where Var1 is a pointer of type int and Var2 is a pointer of
type float.
When a data object is no longer needed, it is destroyed to
release the memory space for reuse. The general form of
its use is:
delete Pointer_Variable;
The Pointer_Variable is the pointer that points to a data
object created with new.
delete Var1;
delete Var2;
16
Types of Linked Lists(Singly Linked List
and Doubly Linked List)
Basically we can divide the linked list into the
following three types in the order in which they
(or node) are arranged.
Singly linked list
Doubly linked list
Circular linked list
17
Singly Linked List
18
Algorithms for operations in Singly Linked List
Creating singly linked list
struct node
{
char name[20]; // Name of up to 20 letters
int age ;
node *nxt;// Pointer to next node
};
struct node *start_ptr = NULL;
19
Algorithm for inserting a node
Insert a Node at the beginning
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode → DATA = DATA
4. If (SATRT equal to NULL)
(a) NewNode → Link = NULL
5. Else
(a) NewNode → Link = START
6. START = NewNode
7. Exit
20
Insert a Node at any specified position
Insert a Node at the end 1. Input DATA and POS to be inserted
1. Input DATA to be inserted 2. Initialize TEMP = START; and j = 0
2. Create a NewNode 3. Repeat the step 3 while (k is less than
3. NewNode → DATA = DATA POS)
4. NewNode → Next = NULL (a) TEMP = TEMP Next
8. If (SATRT equal to NULL) (b) If (TEMP is equal to NULL)
(a) START = NewNode (i) Display “Node in the list less than the
9. Else position”
(a) TEMP = START (ii) Exit
(b) While (TEMP Next not equal to (c) k = k + 1
NULL) 4. Create a New Node
(i) TEMP = TEMP Next 5. NewNodeDATA = DATA
10. TEMPNext = NewNode 6. NewNodeNext = TEMPNext
11. Exit 7. TEMPNext = NewNode
8. Exit
21
Algorithm for Deleting a node
1. Input the DATA to be deleted
2. If ((STARTDATA) is equal to DATA)
(a) TEMP = START
(b) START = STARTNext
(c) Set free the node TEMP, which is deleted
(d) Exit
3. HOLD = START
4. While ((HOLDNextNext) not equal to NULL))
(a) if ((HOLDNEXTDATA) equal to DATA)
(i) TEMP = HOLDNext
(ii) HOLDNext = TEMPNext
(iii) Set free the node TEMP, which is deleted
(iv) Exit
(b) HOLD = HOLDNext
5. if ((HOLDnextDATA) == DATA)
(a) TEMP = HOLDNext
(b) Set free the node TEMP, which is deleted
(c) HOLDNext = NULL
(d) Exit
6. Disply “DATA not found”
7. Exit
22
Reading Assignment
Algorithm for Searching a Node
Algorithm for display all nodes
23
Doubly Linked List
A doubly linked list is one in which all nodes
are linked together by multiple links which
help in accessing both the successor (next) and
predecessor (previous) node for any arbitrary
node within the list.
Every node in the doubly linked list has three
fields: Left Pointer, Right Pointer and DATA.
Lpoint Data Rpoint
24
Cont.…
Lpoint will point to the node in the left side (or
previous node) that is Lpoint will hold the
address of the previous node.
Rpoint will point to the node in the right side (or
next node) that is Rpoint will hold the address of
the next node.
Data will store the information of the node.
25
26
Algorithm for inserting a node
1. Input the DATA and POS
2. Initialize TEMP = START; i = 0
3. Repeat the step 4 if (i less than POS)
and (TEMP is not equal to NULL)
4. TEMP = TEMPRPoint; i = i +1
5. If (TEMP not equal to NULL) and (i
equal to POS)
(a) Create a New Node
(b) NewNodeDATA = DATA
(c) NewNodeRPoint = TEMPRPoint
(d) NewNodeLPoint = TEMP
(e) (TEMPRPoint)LPoint = NewNode
(f ) TEMPRPoint = New Node
6. Else
(a) Display “Position NOT found”
7. Exit
27
Algorithm for deleting a node
1. Input the POS
2. Initialize TEMP = START; i = 0
3. Repeat the step 4 if (i less than POS)
and (TEMP is not equal to NULL)
4. TEMP = TEMPRPoint; i = i +1
5. If (TEMP not equal to NULL) and (i
equal to POS)
(a) Create a New Node
(b) NewNodeDATA = DATA
(c) NewNodeRPoint = TEMPRPoint
(d) NewNodeLPoint = TEMP
(e) (TEMPRPoint)LPoint = NewNode
(f ) TEMPRPoint = New Node
6. Else
(a) Display “Position NOT found”
28
Question & Answer
29
Thank You !!!
30