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

CACS201 Unit 5 - Linked Lists

Uploaded by

Mizash Kandel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CACS201 Unit 5 - Linked Lists

Uploaded by

Mizash Kandel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Unit 5

Linked Lists
Contents
• Introduction
• Linked List as an ADT
• Dynamic Implementation
• Insertion & Deletion of Node to and from a List
• Insertion and Deletion After and Before Nodes
• Linked Stacks and Queues
• Doubly Linked Lists and Its Advantages
Introduction
• A linked list is a linear collection of data elements.
• These data elements are called nodes, and they point to the next node by
means of pointers.
• A linked list is a data structure which can be used to implement other data
structures such as stacks, queues, trees, and so on.
• A linked list is a sequence of nodes in which each node contains one or
more than one data field and a pointer which points to the next node.
• Also, linked lists are dynamic in nature; that is, memory is allocated as and
when required.
• There is no need to know the exact size or exact number of elements as in
the case of arrays.
Introduction
• Each node is divided into two parts:
• The first part contains the information/data.
• The second part contains the address of the next node.
• The last node will not have any next node connected to it, so it will
store a special value called NULL. Usually NULL is defined by -1.
• Also, there is another special pointer START that stores the address of
the first node of the linked list. If START = NULL then it means that the
linked list is empty.
• Since each node points to another node which is of the same type, it
is known as a self-referential data type or a self-referential structure.
Linked List as an ADT
The List ADT Functions is given below:
• get() – Return an element from the list at any given position.
• insert() – Insert an element at any position of the list.
• remove() – Remove the first occurrence of any element from a non-empty
list.
• removeAt() – Remove the element at a specified location from a non-
empty list.
• replace() – Replace an element at any position by another element.
• size() – Return the number of elements in the list.
• isEmpty() – Return true if the list is empty, otherwise return false.
• isFull() – Return true if the list is full, otherwise return false.
Types of Linked Lists
There are different types of linked lists which we will be discussed in
this section. These include:
1. Singly Linked List
2. Circular Linked List
3. Doubly Linked List
4. Header Linked List
Dynamic Implementation
• The process or concept of linked lists supports dynamic memory allocation.
• To overcome the problem of wastage of memory space or in other words to
utilize the memory efficiently, dynamic memory allocation is used which allows us
to allocate/reserve the memory that is actually required.
• Hence, it will overcome the problem of wastage of memory space as in the case
of arrays.
• Dynamic memory allocation is best when we are not aware of the memory
requirements in advance.
Inserting a new node in a linked list
We will discuss three cases in the insertion process which include:
1. New node is inserted at beginning of the linked list.
2. New node is inserted at end of the linked list.
3. New node is inserted after the given node in a linked list.
Inserting a new node in the beginning of a linked list

• In the case of inserting a new node in the beginning of a linked list,


we will first check the overflow condition, which is whether the
memory is available for a new node.
• If the memory is not available, then an overflow message is displayed;
otherwise, the memory is allocated for the new node.
• Now, we will initialize the node with its info part, and its address part
will contain the address of the first node of the list which is the START
pointer.
• Hence, the new node is added as the first node in the list and the
START pointer will point to the first node of the list.
Inserting a new node in the beginning of a linked list
Algorithm for inserting a new node in the beginning of a linked list

Step 1: START
Step 2: IF PTR = NULL
Print OVERFLOW
Go to Step 8
[End of If]
Step 3: Set NEW NODE = PTR
Step 4: Set PTR = PTR -> NEXT
Step 5: Set NEW NODE -> INFO = VALUE
Step 6: Set NEW NODE -> NEXT = START
Step 7: Set START = NEW NODE
Step 8: EXIT
Inserting a new node at the end of a linked list
• To insert the new node at the end of the linked list, we will first check
the overflow condition, which is whether the memory is available for
a new node.
• If the memory is not available, then an overflow message is displayed;
otherwise, the memory is allocated for the new node.
• Then a PTR variable is made which will initially point to START and will
be used to traverse the linked list until it reaches the last node.
• When it reaches the last node, the NEXT part of the last node will
store the address of the new node and the NEXT part of the NEW
NODE will contain NULL which will denote the end of the linked list.
Inserting a new node at the end of a linked list
Algorithm for inserting a new node at the end of a linked list

Step 1: START
Step 2: IF PTR = NULL
Print OVERFLOW
Go to Step 10
[End of If]
Step 3: Set NEW NODE = PTR
Step 4: Set PTR = PTR -> NEXT
Step 5: Set NEW NODE -> INFO = VALUE
Step 6: Set NEW NODE -> NEXT = NULL
Step 7: Set PTR = START
Step 8: Repeat Step 8 while PTR -> NEXT != NULL
Set PTR = PTR -> NEXT
[End of Loop]
Step 9: Set PTR -> NEXT = NEW NODE
Step 10: EXIT
Inserting a new node after a node in a linked list
• In this case, a new node is inserted after a given node in a linked list.
As in the other cases, we will again check the overflow condition.
• If the memory for the new node is available, it will be allocated;
otherwise, an overflow message is printed.
• Then a PTR variable is made which will initially point to START, and the
PTR variable is used to traverse the linked list until it reaches the
value/node after which the new node is to be inserted.
• When it reaches that node/value, then the NEXT part of that node
will store the address of the new node and the NEXT part of the NEW
NODE will store the address of its next node in the linked list.
Inserting a new node after a node in a linked list
Algorithm for inserting a new node after a given node in a linked list

Step 1: START
Step 2: IF PTR = NULL
Print OVERFLOW
Go to Step 10
[End of If]
Step 3: Set NEW NODE = PTR
Step 4: Set PTR = PTR -> NEXT
Step 5: Set NEW NODE -> INFO = VALUE
Step 6: Set PTR = START
Step 7: Set PREV = PTR
Step 8: Repeat Step 8 while PREV -> INFO != GIVEN_VAL
Set PREV = PTR
Set PTR = PTR -> NEXT
[End of Loop]
Step 9: Set PREV -> NEXT = NEW NODE
Step 10: Set NEW NODE -> NEXT = PTR
Step 11: EXIT
Deleting a node from a linked list
We will discuss three cases in the deletion process which include:
1. Node is deleted from the beginning of the linked list.
2. Node is deleted from the end of the linked list.
3. Node is deleted after a given node from the linked list.
Deleting a node from the beginning of the linked list

• In the case of deleting a node from the beginning of a linked list, we


will first check the underflow condition, which occurs when we try to
delete a node from a linked list which is empty.
• This situation exists when the START pointer is equal to NULL.
• If the condition is true, then the underflow message is printed on the
screen; otherwise, the node is deleted from the linked list.
Deleting a node from the beginning of the linked list
Algorithm for deleting a node from the beginning of a linked list

Step 1: START
Step 2: IF START = NULL
Print UNDERFLOW
[End Of If]

Step 3: Set PTR = START


Step 4: Set START = START -> NEXT
Step 5: FREE PTR
Step 6: EXIT
Deleting a node from the end of the linked list
• In the case of deleting a node from the end of the linked list, we will
first check the underflow condition.
• This situation exists when the START pointer is equal to NULL.
• Hence, if the condition is true then the underflow message is printed
on the screen; otherwise, the node is deleted from the linked list.
Deleting a node from the end of the linked list
Algorithm for deleting a node from the end of a linked list

Step 1: START
Step 2: IF START = NULL
Print UNDERFLOW
[End Of If]
Step 3: Set PTR = START
Step 4: Repeat while PTR -> NEXT != NULL
Set PREV = PTR
Set PTR = PTR -> NEXT
[End of Loop]
Step 5: Set PREV -> NEXT = NULL
Step 6: FREE PTR
Step 7: EXIT
Deleting a node after a given node from the linked list

• In the case of deleting a node after a given node from the linked list,
we will again check the underflow condition as we checked in both
the other cases.
• This situation exists when the START pointer is equal to NULL.
• Hence, if the condition is true then the underflow message is printed;
otherwise, the node is deleted from the linked list.
Deleting a node after a given node from the linked list
Algorithm for deleting a node after a given node from the linked list

Step 1: START
Step 2: IF START = NULL
Print UNDERFLOW
[End Of If]
Step 3: Set PTR = START
Step 4: Set PREV = START
Step 5: Repeat while PREV -> INFO != GIVEN_VAL
Set PREV = PTR
Set PTR = PTR -> NEXT
[End of Loop]
Step 6: Set PREV -> NEXT = PTR -> NEXT
Step 7: FREE PTR
Step 8: EXIT
Write a menu-driven program for singly linked lists
performing insertion and deletion of all cases.
Doubly Linked List
• A doubly linked list is also called a two-way linked list; it is a special
type of linked list which can point to the next node as well as the
previous node in the sequence.
• In a doubly linked list each node is divided into three parts:
• The first part is called the previous pointer, which contains the address of the
previous node in the list.
• The second part is called the information part, which contains the information
of the node.
• The third part is called the next pointer, which contains the address of the
succeeding node in the list.
Operations on a Doubly Linked List
Various operations can be performed on a doubly linked list, which include:
• Inserting a New Node in a Doubly Linked List
• New Node is inserted at the beginning.
• New Node is inserted at the end.
• New Node is inserted after a given node.
• New Node is inserted before a given node.
• Deleting a Node from a Doubly Linked List
• Node is deleted from the beginning of the linked list.
• Node is deleted from the end of the linked list.
• Node is deleted after a given node from the linked list.
• Node is deleted before a given node from the linked list.
Inserting a new node in the beginning of a doubly linked list

• In this case of inserting a new node in the beginning of a doubly


linked list, we will first check with the overflow condition, that is,
whether the memory is available for a new node.
• If the memory is not available, then an overflow message is displayed;
otherwise, the memory is allocated for the new node.
• Now, we will initialize the node with its info part, and its address part
will contain the address of the first node of the list, which is the
START pointer.
• Hence, the new node is added as the first node in the list, and the
START pointer will point to the first node of the list.
Inserting a new node in the beginning of a doubly linked list
Algorithm for inserting a new node in the beginning of a doubly linked list

Step 1: START
Step 2: IF PTR = NULL
Print OVERFLOW
Go to Step 9
[End of If]
Step 3: Set NEW NODE = PTR
Step 4: Set NEW NODE -> INFO = VALUE
Step 5: Set NEW NODE -> PREV = NULL
Step 6: Set NEW NODE -> NEXT = START
Step 7: Set START -> PREV = NEW NODE
Step 8: Set START = NEW NODE
Step 9: EXIT
Inserting a new node at the end of a doubly linked list

• We will first check the overflow condition, which is whether the memory is
available for a new node.
• If the memory is not available then an overflow message is printed;
otherwise, the memory is allocated for the new node.
• Then a PTR variable is made which will initially point to START, and a PTR
variable will be used to traverse the list until it reaches the last node.
• When it reaches the last node, the NEXT part of the last node will store the
address of the new node, and the NEXT part of the NEW NODE will contain
NULL, which will denote the end of the linked list.
• The PREV part of the NEW NODE will store the address of the node pointed
to by PTR.
Inserting a new node at the end of a doubly linked list
Algorithm for inserting a new node at the end of a linked list

Step 1: START
Step 2: IF PTR = NULL
Print OVERFLOW
[End of If]
Step 3: Set NEW NODE = PTR
Step 4: Set PTR = PTR -> NEXT
Step 5: Set NEW NODE -> INFO = VALUE
Step 6: Set NEW NODE -> NEXT = NULL
Step 7: Set PTR = START
Step 8: Repeat while PTR -> NEXT != NULL
Set PTR = PTR -> NEXT
[End of Loop]
Step 9: Set PTR -> NEXT = NEW NODE
Step 10: Set NEW NODE -> PREV = PTR
Step 11: EXIT
Inserting a new node after a given node in a doubly linked list

• We will again check the overflow condition in it.


• If the memory for the new node is available, then it will be allocated;
otherwise, an overflow message is displayed.
• Then a PTR variable is made which will initially point to START, and the
PTR variable is used to traverse the linked list until its value becomes
equal to the value after which the new node is to be inserted.
• When it reaches that node/value, then the NEXT part of that node
will store the address of the new node, and the PREV part of the NEW
NODE will store the address of the preceding node.
Inserting a new node after a given node in a doubly linked list
Algorithm for inserting a new node after a given node in a linked list

Step 1: START
Step 2: IF PTR = NULL
Print OVERFLOW
Go to Step 10
[End of If]
Step 3: Set NEW NODE = PTR
Step 4: Set NEW NODE -> INFO = VALUE
Step 5: Set PTR = START
Step 6: Repeat while PTR -> INFO != GIVEN_VAL
Set PTR = PTR -> NEXT
[End of Loop]
Step 7: Set NEW NODE -> NEXT = PTR -> NEXT
Step 8: Set NEW NODE -> PREV = PTR
Step 9: Set PTR -> NEXT = NEW NODE
Step 10: EXIT
Inserting a new node before a given node in a doubly linked list

• We will again check the overflow condition in it.


• If the memory for the new node is available, then it will be allocated;
otherwise, an overflow message is displayed.
• Then a PTR variable is made which will initially point to START, and the
PTR variable is used to traverse the linked list until its value becomes
equal to the value before which the new node is to be inserted.
• When it reaches that node/value, then the PREV part of that node
will store the address of the NEW NODE, and the NEXT part of the
NEW NODE will store the address of the succeeding node.
Inserting a new node before a given node in a doubly linked list
Algorithm for inserting a new node before a given node in a doubly linked list

Step 1: START
Step 2: IF PTR = NULL
Print OVERFLOW
Go to Step 10
[End of If]
Step 3: Set NEW NODE = PTR
Step 4: Set NEW NODE -> INFO = VALUE
Step 5: Set PTR = START
Step 6: Repeat while PTR -> INFO != GIVEN_VAL
Set PTR = PTR -> NEXT
[End of Loop]
Step 7: Set NEW NODE -> NEXT = PTR
Step 8: Set NEW NODE -> PREV = PTR -> PREV
Step 9: Set PTR -> PREV = NEW NODE
Step 10: EXIT
Deleting a node from the beginning of the doubly linked list

• We will first check the underflow condition, which occurs when we


try to delete a node from the linked list which is empty.
• This situation exists when the START pointer is equal to NULL.
• Hence, if the condition is true, then the underflow message is
displayed; otherwise, the node is deleted from the linked list
Algorithm for deleting a node from the beginning of a doubly linked list

Step 1: START
Step 2: IF START = NULL
Print UNDERFLOW
[End Of If]
Step 3: Set PTR = START
Step 4: Set START = START -> NEXT
Step 5: Set START -> PREV = NULL
Step 6: FREE PTR
Step 7: EXIT
Deleting a node from the end of the doubly linked list
Algorithm for deleting a node from the end in a doubly linked list

Step 1: START
Step 2: IF START = NULL
Print UNDERFLOW
[End Of If]
Step 3: Set PTR = START
Step 4: Repeat while PTR -> NEXT != NULL
Set PTR = PTR -> NEXT
[End of Loop]
Step 5: Set PTR -> PREV -> NEXT= NULL
Step 6: FREE PTR
Step 7: EXIT
Deleting a node after a given node from the doubly linked list
Algorithm for deleting a node after a given node from the linked list

Step 1: START
Step 2: IF START = NULL
Print UNDERFLOW
[End Of If]
Step 3: Set PTR = START
Step 4: Repeat while PTR -> INFO != GIVEN_VAL
Set PTR = PTR -> NEXT
[End of Loop]
Step 5: Set TEMP = PTR -> NEXT
Step 6: Set PTR -> NEXT = TEMP -> NEXT
Step 7: Set TEMP-> NEXT -> PREV = PTR
Step 8: FREE TEMP
Step 9: EXIT
Deleting a node before a given node from the doubly linked list
Algorithm for deleting a node before a given node in a doubly linked list

Step 1: START
Step 2: IF START = NULL
Print UNDERFLOW
[End Of If]
Step 3: Set PTR = START
Step 4: Repeat while PTR -> INFO != GIVEN_VAL
Set PTR = PTR -> NEXT
[End of Loop]
Step 5: Set TEMP = PTR -> PREV
Step 6: Set TEMP -> PREV -> NEXT = PTR
Step 7: Set PTR -> PREV = TEMP -> PREV
Step 8: FREE TEMP
Step 9: EXIT
Doubly Linked Lists Advantages
• The doubly linked list allows traversing in both forward and backward
directions.
• Deletion of the nodes can be done easily.
• Reversing of linked list is easy.
• Insertion can be performed efficiently at any node.
Circular Linked Lists
• Circular linked lists are a type of singly linked list in which the address part
of the last node will store the address of the first node, unlike in singly
linked lists in which the address part of the last node stores a unique value,
NULL.
• While traversing a circular linked list we can begin from any node and we
traverse the list in only one direction, because a circular linked list does not
have a first or last node.
• The memory declarations for representing a circular linked list are the
same as for a linear linked list.
Header Linked Lists
• Header linked lists are a special type of linked list which always contain a special node, called the
header node, at the beginning.
• This header node usually contains vital information about the linked list, like the total number of
nodes in the list, whether the list is sorted or not, and so on.
• There are two types of header linked lists, which include:
• Grounded Header Linked List – This linked list stores a unique value NULL in the address field (next
part) of the last node of the list.

• Circular Header Linked List – This linked list stores the address of the header node in the address field
(next part) of the last node of the list.
Implementation of Stacks Using Linked Lists
• If we are aware of the maximum size of the stack in advance, then
implementation of stacks using arrays will be efficient.
• But if the size is not known in advance, then we will use the concept of a linked
list in which dynamic memory allocation takes place.
• As we all know a linked list has two parts; the first part contains the information
of the node, and the second part stores the address of the next element in the
linked list.
• Similarly, we can also implement a linked stack.
• Now, the START pointer in the linked list will become the TOP pointer in a linked
stack.
• All insertion and deletion operations will be done at the node pointed to by TOP
only.
Push Operation in Linked Stacks
• The new elements in the stack will always be inserted at the topmost
position of the stack.
• Initially, we will check whether TOP = NULL.
• If the condition is true, then the stack is empty; otherwise, the new
memory is allocated for the new node.
Algorithm for inserting a new element in a linked stack

Step 1: START
Step 2: Set NEW NODE -> INFO = VAL
IF TOP = NULL
Set NEW NODE -> NEXT = NULL
Set TOP = NEW NODE
ELSE
Set NEW NODE -> NEXT = TOP
Set TOP = NEW NODE
[End of If]
Step 3: EXIT
Pop Operation in Linked Stacks
• The elements from the stack will always be deleted from the node
pointed to by TOP.
• Initially, we will check whether TOP = NULL.
• If the condition is true, then the stack is empty, which means we
cannot delete any elements from it.
• Therefore, in that case an underflow error message is displayed on
the screen.
Implementation of Queues Using Linked Lists
• If we are aware of the maximum size of the queue in advance, then
implementation of a queue using arrays will be efficient.
• But if the size is not known in advance, then we will use the concept of a
linked list, in which dynamic memory allocation takes place.
• We can implement a linked queue.
• Now, the START pointer in the linked list will become the FRONT pointer in
a linked queue and the end of the queue will be denoted by REAR.
• All insertion operations will be done at the rear end only.
• Similarly, all deletion operations will be done at the front end only.
Insertion in Linked Queues
• The new elements in the queue will always be inserted from the rear
end.
• Initially, we will check whether FRONT = NULL.
• If the condition is true, then the queue is empty; otherwise, the new
memory is allocated for the new node.
Algorithm for inserting a new element in a linked queue

Step 1: START
Step 2: Set NEW NODE -> INFO = VAL
IF FRONT = NULL
Set FRONT = REAR = NEW NODE
Set FRONT -> NEXT = REAR -> NEXT = NEW NODE
ELSE
Set REAR -> NEXT = NEW NODE
Set NEW NODE -> NEXT = NULL
Set REAR = NEW NODE
[End of If]
Step 3: EXIT
Deletion in Linked Queues
• The elements from the queue will always be deleted from the front
end.
• Initially, we will check with the underflow condition, that is, whether
FRONT = NULL. If the condition is true, then the queue is empty,
which means we cannot delete any elements from it.
• Therefore, in that case an underflow error message is displayed on
the screen.
Algorithm for deleting an element from a queue
Step 1: START
Step 2: IF FRONT = NULL
Print UNDERFLOW ERROR
[End of If]
Step 3: Set TEMP = FRONT
Step 4: Set FRONT = FRONT -> NEXT
Step 5: FREE TEMP
Step 6: EXIT

You might also like