Lab4 06042021 042043pm
Lab4 06042021 042043pm
Objectives:
This lab is intended to introduce the different variants of Linked List and their working.
Let us see the building blocks of linked list with dummy head node:
class Node
{
int data;
Node *next;
};
head ? 17 22 26 34
class HList
{
private:
Node * head;
public:
HList();
// Checks if the list is empty or not
bool emptyList();
// Inserts a new node with value ‘newV’ after node with
value ‘oldV’in the list
void insert_after(int oldV, int newV);
// Inserts a new node at the start of the list
It is also an indication of an empty list. To check if the list is empty use the condition (head-
>next==NULL) instead of (head==NULL).
head ?
a) Insertion at beginning
b) Insertion at end
c) Insertion at a particular position
Insertion at the beginning is a special case in a linear linked list as it requires the ‘Head’ pointer
to move to the new node inserted at the beginning. However in a list with dummy head node,
insertion at beginning will not require the ‘Head’ pointer to move as head node remains fixed.
The new node will be inserted after the head node.
For insertion at the end or any particular location will require traversal of the list starting from
the (Node *t= head->next) till the particular location and then insertion.
Deletion at the beginning is a special case in a linear linked list as it requires the ‘Head’ pointer
to move to the next node. However in a list with dummy head node, deletion at beginning will
not require the ‘Head’ pointer to move as head node remains fixed. The node to be deleted will
be the second node.
For deletion at the end or from any particular location will require traversal of the list starting
from the (Node *t= head->next) till the particular location. Another pointer will keep track of
the previous node to the current node.
Circular linked list is a linked list where all nodes are connected to form a circle. There is no
NULL at the end. A circular linked list can be a singly circular linked list or doubly circular linked
list.
Difference between circular and linear Linked List: 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
In circular linked list, any node can be a starting point. We can traverse the whole list by
starting from any point. We just need to stop when the first visited node is visited again.
class Node
{
int data;
Node *next;
};
When a circular list object is called a pointer ‘Head’ is created which is pointing to NULL.
A node can be inserted at any particular position in a circular list. However in general there can
be three particular scenarios for insertion of a node.
A node can be deleted from any particular position in a circular list. However in general there
can be three particular scenarios for deletion of a node.
If the list is not empty then we define two pointers curr and prev and
initialize the pointer curr with the head node
If the list is not empty then we define two pointers curr and prev and
initialize the pointer curr with the head node
Case 2: If the list has more nodes then check the location of the node.
Data Structure and Algorithm Page 7
Lab#04
If curr is the last node. Set prev -> next = head and delete the node
curr by Delete curr.
If curr is the first node. Move prev until it reaches the last node. After
prev reaches the last node, set head = head -> next and prev ->
next = head. Delete curr.
If the node to be deleted is neither the first node nor the last node, then
set prev -> next = temp -> next and delete curr.
2.4.4 Traversal
In a conventional linked list, we traverse the list from the head node and stop
the traversal when we reach NULL. In a circular linked list, we stop traversal
when we reach the first node again.
+++++++++++++++++++++++++++++++++++++++++