0% found this document useful (0 votes)
20 views8 pages

Lab4 06042021 042043pm

solved lab journal of data structures and algorithm

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Lab4 06042021 042043pm

solved lab journal of data structures and algorithm

Uploaded by

ikhalil2718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab#04

Objectives:
 This lab is intended to introduce the different variants of Linked List and their working.

1 Linked List with Dummy Header Node:


To avoid the special case problems involved in inserting and deleting nodes,
programmers often use a dummy node in the list. A dummy head node is a
node that always points to the beginning of the list or has a NULL pointer
field if the list is empty. This node is called a dummy node since it does not
contain data included in the list. This node is permanent (is never deleted)
and always points to the first node in the list. To eliminate the need for
special structures, it is preferred that the dummy node be set up using the
same type of nodes as the data nodes in the list.

Let us see the building blocks of linked list with dummy head node:

class Node
{
int data;
Node *next;
};

Now the next step is to create an ADT class.

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

Data Structure and Algorithm Page 1


Lab#04
void insert_begin(int value);
// Inserts a new node at the end of the list
void insert_end(int value);
// Deletes a node from position ‘pos’ of the list
void deleteNode(int value);
// Deletes a node from the beginning of the list
void delete_begin();
// Deletes a node from the end of the list
void delete_end();
// Displays the values stored in the list
void traverse();
};

1.1 Creating a list with dummy head node:


When an object of this linked list variant is called, it creates a node ‘head’ with no data and next
field containing ‘NULL’.

Node *head = new Node;


head->data = 0;
head->next = NULL;

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 ?

1.2 Insertion in a list with dummy head node:


Insertion in this list variant is similar to any list i.e. insertion can take place at:

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.

Node *temp = new Node;


temp->data = value;

Data Structure and Algorithm Page 2


Lab#04
temp->next = head->next;
head->next = temp;

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.

1.3 Deletion of a node in a list with dummy head node:


Deletion in this list variant is similar to any list i.e. deletion can take place at:

a) Deletion from beginning


b) Deletion from end
c) Deletion from a particular position

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.

2 Circular Linked List:

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

Data Structure and Algorithm Page 3


Lab#04
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.

Figure 1: Circular Linked List

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.

Let us see the building blocks of circular list:

class Node
{
int data;
Node *next;
};

Now the next step is to create a circular linked list class.

// Creating a class Circular Linked List


class CList
{
private:
Node *head;
public:
CList();
// Checks if the list is empty or not
bool emptyList();
// Inserts a new node with value ‘value’ at position ‘pos’
in the list
void insert_at (int pos, int value);
// Inserts a new node at the start of the list
void insert_begin(int value);
// Inserts a new node at the end of the list
void insert_end(int value);
// Deletes a node from position ‘pos’ of the list
void deleteNode(int pos);
// Deletes a node from the beginning of the list

Data Structure and Algorithm Page 4


Lab#04
void delete_begin();
// Deletes a node from the end of the list
void delete_end();
// Displays the values stored in the list
void traverse();
};

Let us observe the working of each member function:

2.1 Constructing an empty list:

When a circular list object is called a pointer ‘Head’ is created which is pointing to NULL.

2.2 Checking for empty list:

If head is equal to NULL then the list is empty.

2.3 Insertion in a circular list:

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) Insertion at the beginning.


b) Insertion at the end.
c) Insertion at a position i.

2.4 Deletion from a circular list:

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.

d) Deletion at the beginning.


e) Deletion at the end.
f) Deletion at a position i.

Data Structure and Algorithm Page 5


Lab#04
2.4.1 Deletion at the beginning:

Case 1: List is empty.


 If the list is empty we will simply return.

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: List has only one node.


 If yes, set head = NULL and Delete curr.

Case 3: If the list has more than one 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.

Fig: Deleting first node from circular linked list

2.4.2 Deletion at the end:


Case 1: List is empty.
 If the list is empty we will simply return.

Data Structure and Algorithm Page 6


Lab#04
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: List has only one node.


 If yes, set head = NULL and Delete curr.

Case 3: If the list has more than one node.


 Set prev -> next = head and delete the node curr by Delete curr.

Fig: Deleting last node from circular linked list

2.4.3 Deleting at a particular position:


Case 1: List is empty.
 If the list is empty we will simply return.

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: List has less number of nodes.


 If yes, we simply return.

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.

+++++++++++++++++++++++++++++++++++++++++

Data Structure and Algorithm Page 8

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy