0% found this document useful (0 votes)
15 views22 pages

RAA DS Unit 3 Final Print

The document discusses the concept of linked organizations, specifically focusing on linked lists, which are collections of nodes that store data and pointers to the next node. It highlights the advantages of linked lists over arrays, such as dynamic memory allocation and non-contiguous storage, and outlines basic operations like insertion and deletion in singly linked lists. Additionally, it provides algorithms for initializing, inserting, and deleting nodes within a singly linked list.

Uploaded by

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

RAA DS Unit 3 Final Print

The document discusses the concept of linked organizations, specifically focusing on linked lists, which are collections of nodes that store data and pointers to the next node. It highlights the advantages of linked lists over arrays, such as dynamic memory allocation and non-contiguous storage, and outlines basic operations like insertion and deletion in singly linked lists. Additionally, it provides algorithms for initializing, inserting, and deleting nodes within a singly linked list.

Uploaded by

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

UNIT – III

CONCEPT OF LINKED ORGANIZATIONS


o Linked List can be defined as collection of objects called nodes that are randomly
stored in the memory.
o A node contains two fields i.e. data stored at that particular address and the
pointer which contains the address of the next node in the memory.
o The last node of the list contains pointer to the null.

Uses of Linked List


The list is not required to be contiguously present in the memory. The node can
reside anywhere in the memory and linked together to make a list. This achieves
optimized utilization of space.
List size is limited to the memory size and doesn't need to be declared in
advance.
Empty node cannot be present in the linked list.
We can store values of primitive types or objects in the singly linked list.
Linked list is useful because -

It allocates the memory dynamically. All the nodes of the linked list are non-
contiguously stored in the memory and linked together with the help of
pointers.
In linked list, size is no longer a problem since we do not need to define its size at
the time of declaration. List grows as per the program's demand and limited to
the available memory space.

Why use linked list over array?


Till now, we were using array data structure to organize the group of elements that are
to be stored individually in the memory. However, Array has several advantages and
disadvantages which must be known in order to decide the data structure which will be
used throughout the program.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 1
UNIT – III
Array contains following limitations:
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to
expand the size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory.
Inserting any element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array.
Using linked list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-
contiguously stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available
memory space.
DYNAMIC STORAGE MANAGEMENT
When memory is allocated during run/execution time, it is called ‘Dynamic Memory
Management’. This memory is not fixed and is allocated according to our requirements. Thus
in it there is no wastage of memory. So there is no need to know exact memory requirements
in advance. Linked lists use the concept of Dynamic storage management.

LINKED LIST
In this representation, the structure does not necessarily reflect the logical
organization. Items may appear in any order. The logical organization is provided
through pointers. Each item in the list is called as a node that consists of a data portion
containing the item information and a pointer which contains the location of the next item in
the logical sequence which is to be maintained. It is actually possible to have the advantages
of the linked list by using arrays, but in this case two kinds of information must be stored in
arrays, the information contained in the item and the pointer for that item. Suppose that F,
A, C, G are to be stored in an array which can hold a maximum of 5 items. They are to be
stored in the order in which they are read but they should logically be available in
alphabetical order. The array must be set up first. Then some counter to keep track of which
slot is available (i.e. empty) is required. A link field is required for two things. It can specify
the next index which is available after the current available space. It can also specify the
logical order of the items which currently occupy space.
It is also possible to delete and reuse space using this method. However, a
disadvantage is that an array which is large enough to hold the longest list must be declared
which may result in a large waste of memory in many cases.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 2
UNIT – III

SINGLY LINKED LISTS


We will examine the singly-linked list as an abstract data type. Remember that the
abstraction is an advantage to the user but the implementer must deal with all of the sub
processes which are abstracted for the user. Thus the user sees five processes but the
implementer must deal with all of the small processes which are used by the five major
processes which define the type. Whether the implementation uses arrays or dynamic
allocation is not relevant to the user but it is important for the implementer. The graphical
representation of a node in a single linked list is as follows...

Figure: Single Linked List Node Structure

For the implementer this means that the array must be defined and initialized and the
variable indicating the first open position set if it is an array implementation. If dynamic
allocation is used, the node structure must be defined. Obviously the user can request the
create process at any time. Then the old list is lost (or saved if the software allows) and the
initialization done.
Creation is an O(n) process for arrays, where n is the maximum length of the list (i.e.
array size) but an O(1) operation for dynamic allocation. Length is usually implemented by
having a variable which contains the length of the list. Note that this implementation requires
that insert and delete also modify the length of the list.
For array implementations this is usually stored as an integer variable. For dynamic
allocation, it may be stored as an integer variable as well and is stored in the list node for the
list. In order to find a specified item in a list the retrieve process is used. This is essentially a
search process. In a singly-linked list, all items are accessed by beginning at the front or head
of the list and going through until the required position is found.
Retrieve does not need to know how the list is ordered (i.e. ascending, frequency, etc.)
since the structure is built so that each item follows the next according to some ordering
relationship. The implementation may be diverse enough so that we can retrieve an item by
its key or as the nth element. In this case the retrieve has additional parameters. Retrieve is
O(n) in the worst case since the last item may be the one which is required or the item does

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 3
UNIT – III
not exist in the list. n in this case is the number of items in the list, not the maximum possible
number of items. We could make retrieve more efficient at indicating that an item does not
exist, but in doing so we also remove some of the generality of the data type.
Only one procedure should require modification when we change the ordering
relationship (see insertion). If we also incorporate the same code in retrieve so that the search
can stop when the item position is passed, we limit retrieve to that kind of ordered list. Note
also that retrieve from the user point of view is transparent of the data structure. Whether an
array or a series of nodes is accessed is not important to the user. Insert puts a new item in
the list.
Obviously, it is important to know the ordering relationship in order to implement this
process. Looking at the implementation more carefully, there are several steps. We have to
get an open space in the array implementation or create a new node for dynamically allocated
nodes. The information is then placed in the newly acquired position. Finding the correct
position for the new item in the list is dependent on the ordering relationship. Thus if we
change the type of linked list this procedure must be changed, or alternately, we can pass a
flag to the procedure which uses the portion of the code for that ordering relationship.
Having found the position, the insert procedure now sets the links so that the new item
is in the appropriate position with respect to the list ordering. The links determine the
appropriate position not the position physically occupied. Note that insertion at the head of
the list is a special case which must be dealt with. Most often the array implementation has
the link field (see previous) so that data need not be moved each time a new item is inserted.
The insertion itself is O(1) but the process of finding the position may be O(n) where n is the
number of items in the list. This makes the total process O(n) in the worst case, O(1) in the
best case and average of n/2. Delete also requires more than one step. The item must be
found, the links reset so that the integrity of the list is maintained, and the old item
destroyed. Finding the item can be done with the same procedure that finds node position in
insert. Retrieve cannot be used since the preceding node must also be known and this is not
returned with retrieve but must be returned with the find position procedure. Having found
the node, the links are reset to maintain the list, and finally the node destroyed. Once again,
deletion at the head of the list is a special case. In the array implementation, the position
must be placed back as available space, whereas it is returned to the memory pool for
dynamic allocation. Order for deletion is the same as insertion.

BASIC OPERATIONS ON A SINGLE LINKED LIST (SLL)


Before we implement actual operations, first we need to setup empty list. First perform
the following steps before implementing actual operations.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 4
UNIT – III
 Step 1: Include all the header files which are used in the program.
 Step 2: Declare all the user defined functions.
 Step 3: Define a Node structure with two members data and next
 Step 4: Define a Node pointer 'head' and set it to NULL.
 Step 5: Implement the main method by displaying operations menu and make
suitable function calls in the main method to perform user selected operation.
In a single linked list we perform the following operations...
1. Insertion
2. Deletion
3. Display

Figure: A Single Link-List (SLL).


Initialization of a Singly Link List:

void initializeList(List* listPtr)


{
listPtr->headPtr = NULL;
listPtr->count = 0;
}
SET POSITION
 Check if position is within range
 Start with address of head node
 Set count to 0
 While count is less than position
 follow link to next node
 increment count
 Return address of current node
Node* setPosition(const List* listPtr, int position)
{
int i;
Node* nodePtr = listPtr->headPtr;
if (position < 0 || position >= listPtr->count)
{
fprintf(stderr, “Invalid position\n”);
exit(1);
}
else
{
for (i = 0; i < position; i++)
{
nodePtr = nodePtr->nextPtr;
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 5
UNIT – III
}
}
return nodePtr;
}

Insertion Operation of SLL:


In a single linked list, the insertion operation can be performed in three ways. They are as
follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list

Inserting At Beginning of the list


We can use the following steps to insert a new node at beginning of the single linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set newNode→next = NULL and head = newNode.
 Step 4: If it is Not Empty then, set newNode→next = head and head = newNode.

Inserting At End of the list


We can use the following steps to insert a new node at end of the single linked list...
 Step 1: Create a newNode with given value and newNode → next as NULL.
 Step 2: Check whether list is Empty (head == NULL).
 Step 3: If it is Empty then, set head = newNode.
 Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5: Keep moving the temp to its next node until it reaches to the last node in the
list (until temp → next is equal to NULL).
 Step 6: Set temp → next = newNode.

Inserting At Specific location in the list (After a Node)

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 6
UNIT – III
We can use the following steps to insert a new node after a node in the single linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set newNode → next = NULL and head = newNode.
 Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5: Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → datais equal to location, here location
is the node value after which we want to insert the newNode).
 Step 6: Every time check whether temp is reached to last node or not. If it is reached
to last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp to next node.
 Step 7: Finally, Set 'newNode → next = temp → next' and 'temp →
next = newNode'

Algorithm:
void insertItem(List* listPtr, float item, int position)
{
Node* newNodePtr = makeNode(item);
Node* prevPtr = NULL;
if (position == 0)
{
newNodePtr->nextPtr = listPtr->headPtr;
listPtr->headPtr = newNodePtr;
}
else
{
prevPtr = setPosition(listPtr, position-1);
newNodePtr->nextPtr = prevPtr->nextPtr;
prevPtr->nextPtr = newNodePtr;
}
listPtr->count++;
}
1) INSERTING IN A NEW LIST

2) INSERTING – START OF LIST

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 7
UNIT – III

3) INSERTING – INSIDE THE LIST

Deletion Operation on a Single Link List (SLL)


In a single linked list, the deletion operation can be performed in three ways. They are as
follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the single linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize
with head.
 Step 4: Check whether list is having only one node (temp → next == NULL)
 Step 5: If it is TRUE then set head = NULL and delete temp (Setting Empty list
conditions)
 Step 6: If it is FALSE then set head = temp → next, and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the single linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
 Step 4: Check whether list has only one Node (temp1 → next == NULL)
 Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate the
function. (Setting Empty list condition)

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 8
UNIT – III
 Step 6: If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until it reaches to the last node in the list. (until temp1 →
next == NULL)
 Step 7: Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the single linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
 Step 4: Keep moving the temp1 until it reaches to the exact node to be deleted or to
the last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its
next node.
 Step 5: If it is reached to the last node then display 'Given node not found in the
list! Deletion not possible!!!'. And terminate the function.
 Step 6: If it is reached to the exact node which we want to delete, then check whether
list is having only one node or not
 Step 7: If list has only one node and that is the node to be deleted, then
set head = NULL and delete temp1 (free(temp1)).
 Step 8: If list contains multiple nodes, then check whether temp1 is the first node in
the list (temp1 == head).
 Step 9: If temp1 is the first node then move the head to the next node (head = head
→ next) and delete temp1.
 Step 10: If temp1 is not first node then check whether it is last node in the list (temp1
→ next == NULL).
 Step 11: If temp1 is last node then set temp2 → next = NULL and
delete temp1 (free(temp1)).
 Step 12: If temp1 is not first node and not last node then set temp2 → next = temp1
→ next and delete temp1 (free(temp1)).

void deleteNode(List* listPtr, int position)


{
Node* oldNodePtr = NULL;
Node* prevPtr = NULL;
if (listPtr->count > 0 && position < listPtr->count)
{
if (position == 0)
{
oldNodePtr = listPtr->headPtr;
listPtr->headPtr = oldNodePtr->nextPtr;
}
else
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 9
UNIT – III
{
prevPtr = setPosition(listPtr, position - 1);
oldNodePtr = prevPtr->nextPtr;
prevPtr->nextPtr = oldNodePtr->nextPtr;
}
listPtr->count--;
free(oldNodePtr);
}
else
{
fprintf(stderr, “List is empty or invalid position.\n”);
exit(1);
}
}
Deleting – 1st node

Deleting – Middle Node

Time Complexity:

DOUBLY-LINKED LISTS
One of the major disadvantages of a singly linked list is the inability to move
backward in the list. In double linked list, every node has link to its previous node and next
node. So, we can traverse forward by using next field and can traverse backward by using
previous field. Every node in a double linked list contains three fields and they are shown in
the following figure...

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 10
UNIT – III

OPERATIONS ON DLL: Operations


In a double linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the double linked list...
 Step 1: Create a newNode with given value and newNode → previous as NULL.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, assign NULL to newNode →
next and newNode to head.
 Step 4: If it is not Empty then, assign head to newNode →
next and newNode to head.
Inserting At End of the list
We can use the following steps to insert a new node at end of the double linked list...
 Step 1: Create a newNode with given value and newNode → next as NULL.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty, then assign NULL to newNode →
previous and newNode to head.
 Step 4: If it is not Empty, then, define a node pointer temp and initialize with head.
 Step 5: Keep moving the temp to its next node until it reaches to the last node in the
list (until temp → next is equal to NULL).
 Step 6: Assign newNode to temp → next and temp to newNode → previous.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 11
UNIT – III
Inserting At Specific location in the list (After a Node)
We can use the following steps to insert a new node after a node in the double linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, assign NULL to newNode → previous & newNode →
next and newNode to head.
 Step 4: If it is not Empty then, define two node pointers temp1 & temp2 and
initialize temp1 with head.
 Step 5: Keep moving the temp1 to its next node until it reaches to the node after
which we want to insert the newNode (until temp1 → datais equal to location, here
location is the node value after which we want to insert the newNode).
 Step 6: Every time check whether temp1 is reached to the last node. If it is reached to
the last node then display 'Given node is not found in the list!!! Insertion not
possible!!!' and terminate the function. Otherwise move the temp1 to next node.
 Step 7: Assign temp1 → next to temp2, newNode to temp1 →
next, temp1 to newNode → previous, temp2 to newNode →
next and newNode to temp2 → previous.

Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node

Deleting from Beginning of the list


We can use the following steps to delete a node from beginning of the double linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4: Check whether list is having only one node (temp → previous is equal
to temp → next)
 Step 5: If it is TRUE, then set head to NULL and delete temp (Setting Empty list
conditions)
 Step 6: If it is FALSE, then assign temp → next to head, NULL to head →
previous and delete temp.

Deleting from End of the list


We can use the following steps to delete a node from end of the double linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty, then display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is not Empty then, define a Node pointer 'temp' and initialize with head.
 Step 4: Check whether list has only one Node (temp → previous and temp →
next both are NULL)
 Step 5: If it is TRUE, then assign NULL to head and delete temp. And terminate
from the function. (Setting Empty list condition)

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 12
UNIT – III
 Step 6: If it is FALSE, then keep moving temp until it reaches to the last node in the
list. (until temp → next is equal to NULL)
 Step 7: Assign NULL to temp → previous → next and delete temp.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the double linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is not Empty, then define a Node pointer 'temp' and initialize with head.
 Step 4: Keep moving the temp until it reaches to the exact node to be deleted or to
the last node.
 Step 5: If it is reached to the last node, then display 'Given node not found in the
list! Deletion not possible!!!' and terminate the fuction.
 Step 6: If it is reached to the exact node which we want to delete, then check whether
list is having only one node or not
 Step 7: If list has only one node and that is the node which is to be deleted then
set head to NULL and delete temp (free(temp)).
 Step 8: If list contains multiple nodes, then check whether temp is the first node in
the list (temp == head).
 Step 9: If temp is the first node, then move the head to the next node (head = head
→ next), set head of previous to NULL (head → previous = NULL) and
delete temp.
 Step 10: If temp is not the first node, then check whether it is the last node in the list
(temp → next == NULL).
 Step 11: If temp is the last node then set temp of previous of next to NULL (temp
→ previous → next = NULL) and delete temp(free(temp)).
 Step 12: If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous =
temp → previous) and delete temp(free(temp)).
Following are advantages/disadvantages of doubly linked list over singly linked list.
Advantages of DLL over SLL
1. A DLL can be traversed in both forward and backward direction.
2. Delete operation in DLL is more efficient if pointer to the node to be deleted is given
3. We can quickly insert a new node before a given node.
In singly linked list, to delete a node, pointer to the previous node is needed. To get
this previous node, sometimes the list is traversed. In DLL, we can get the previous
node using previous pointer.
Disadvantages of DLL over SLL
1. Every node of DLL Require extra space for an previous pointer. It is possible to
implement DLL with single pointer.
2. All operations require an extra pointer previous to be maintained. For example, in
insertion, we need to modify previous pointers together with next pointers. For
example in following functions for insertions at different positions, we need 1 or 2
extra steps to set previous pointer.
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 13
UNIT – III
CIRCULAR LINKED LIST List?
In single linked list, every node points to its next node in the sequence and the last
node points NULL. But in circular linked list, every node points to its next node in the
sequence but the last node points to the first node in the list.
Circular linked list is a sequence of elements in which every element has link to its next
element in the sequence and the last element has a link to the first element in the
sequence.
That means circular linked list is similar to the single linked list except that the last node
points to the first node in the list
Example:

A Circular Linked List

Operations
In a circular linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Before we implement actual operations, first we need to setup empty list. First perform the
following steps before implementing actual operations.
 Step 1: Include all the header files which are used in the program.
 Step 2: Declare all the user defined functions.
 Step 3: Define a Node structure with two members data and next
 Step 4: Define a Node pointer 'head' and set it to NULL.
 Step 4: Implement the main method by displaying operations menu and make
suitable function calls in the main method to perform user selected operation.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 14
UNIT – III
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as
follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the circular linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set head = newNode and newNode→next = head .
 Step 4: If it is Not Empty then, define a Node pointer 'temp' and initialize with
'head'.
 Step 5: Keep moving the 'temp' to its next node until it reaches to the last node (until
'temp → next == head').
 Step 6: Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.

Inserting At End of the list


We can use the following steps to insert a new node at end of the circular linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL).
 Step 3: If it is Empty then, set head = newNode and newNode → next = head.
 Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5: Keep moving the temp to its next node until it reaches to the last node in the
list (until temp → next == head).
 Step 6: Set temp → next = newNode and newNode → next = head.

Inserting At Specific location in the list (After a Node)


We can use the following steps to insert a new node after a node in the circular linked list...
 Step 1: Create a newNode with given value.
 Step 2: Check whether list is Empty (head == NULL)
 Step 3: If it is Empty then, set head = newNode and newNode → next = head.
 Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
 Step 5: Keep moving the temp to its next node until it reaches to the node after which
we want to insert the newNode (until temp1 → datais equal to location, here location
is the node value after which we want to insert the newNode).
 Step 6: Every time check whether temp is reached to the last node or not. If it is
reached to last node then display 'Given node is not found in the list!!! Insertion
not possible!!!' and terminate the function. Otherwise move the temp to next node.
 Step 7: If temp is reached to the exact node after which we want to insert the
newNode then check whether it is last node (temp → next == head).
 Step 8: If temp is last node then set temp → next = newNode and newNode →
next = head.
 Step 8: If temp is not last node then set newNode → next = temp → next and temp
→ next = newNode.

Deletion
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 15
UNIT – III
In a circular linked list, the deletion operation can be performed in three ways those are as
follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the circular linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize both 'temp1' and 'temp2' with head.
 Step 4: Check whether list is having only one node (temp1 → next == head)
 Step 5: If it is TRUE then set head = NULL and delete temp1 (Setting Empty list
conditions)
 Step 6: If it is FALSE move the temp1 until it reaches to the last node. (until temp1
→ next == head )
 Step 7: Then set head = temp2 → next, temp1 → next = head and delete temp2.

Deleting from End of the list


We can use the following steps to delete a node from end of the circular linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
 Step 4: Check whether list has only one Node (temp1 → next == head)
 Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate
from the function. (Setting Empty list condition)
 Step 6: If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node.
Repeat the same until temp1 reaches to the last node in the list. (until temp1 →
next == head)
 Step 7: Set temp2 → next = head and delete temp1.

Deleting a Specific Node from the list


We can use the following steps to delete a specific node from the circular linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
 Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and
initialize 'temp1' with head.
 Step 4: Keep moving the temp1 until it reaches to the exact node to be deleted or to
the last node. And every time set 'temp2 = temp1' before moving the 'temp1' to its
next node.
 Step 5: If it is reached to the last node then display 'Given node not found in the
list! Deletion not possible!!!'. And terminate the function.
 Step 6: If it is reached to the exact node which we want to delete, then check whether
list is having only one node (temp1 → next == head)

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 16
UNIT – III
 Step 7: If list has only one node and that is the node to be deleted then
set head = NULL and delete temp1 (free(temp1)).
 Step 8: If list contains multiple nodes then check whether temp1 is the first node in
the list (temp1 == head).
 Step 9: If temp1 is the first node then set temp2 = head and keep moving temp2 to
its next node until temp2 reaches to the last node. Then set head = head →
next, temp2 → next = head and delete temp1.
 Step 10: If temp1 is not first node then check whether it is last node in the list (temp1
→ next == head).
 Step 11: If temp1 is last node then set temp2 → next = head and
delete temp1 (free(temp1)).
 Step 12: If temp1 is not first node and not last node then set temp2 → next = temp1
→ next and delete temp1 (free(temp1)).

Displaying a Circular Linked List


We can use the following steps to display the elements of a circular linked list...
 Step 1: Check whether list is Empty (head == NULL)
 Step 2: If it is Empty, then display 'List is Empty!!!' and terminate the function.
 Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize
with head.
 Step 4: Keep displaying temp → data with an arrow (--->) until temp reaches to the
last node
 Step 5: Finally display temp → data with arrow pointing to head → data.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 17
UNIT – III
LINKED STACKS AND QUEUES
We have already seen how to represent stacks and queues sequentially. Such a
representation proved efficient if we had only one stack or one queue. However, when several
stacks and queues co-exist, there was no efficient way to represent them sequentially. In this
section we present a good solution to this problem using linked lists. Figure 4.5 shows a
linked stack and a linked queue.

Notice that the direction of links for both the stack and queue are such as to facilitate
easy insertion and deletion of nodes. In the case of figure 4.5(a), one can easily add a node at
the top or delete one from the top. In figure 4.5(b), one can easily add a node at the rear and
both addition and deletion can be performed at the front, though for a queue we normally
would not wish to add nodes at the front. If we wish to represent n stacks and m queues
simultaneously, then the following set of algorithms and initial conditions will serve our
purpose:
T(i) = Top of ith stack 1 ≤ i ≤ n
F(i) = Front of ith queue 1≤ i≤ m
R(i) = Rear of ith queue 1 ≤ i≤ m
Initial conditions:
T(i) = 0 1≤i≤n
F(i) = 0 1 ≤ i≤ m
Boundary conditions:
T(i) = 0 iff stack i empty
F(i) = 0 iff queue i empty

procedure ADDS(i, Y) //add element Y onto stack i//


call GETNODE(X)
DATA(X) Y //store data value Y into new node//
LINK(X) T(i) //attach new node to top of i-th stack//
T(i) X //reset stack pointer//
end ADDS

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 18
UNIT – III
procedure DELETES(i, Y) //delete top node from stack i & set Y to be the DATA field of this node//

if T(i) = 0 then call STACK__EMPTY


X  T(i) //set X to top node of stack i//
Y  DATA(X) //Y gets new data//
T(i) LINK(X) //remove node from top of stack i//
call RET(X) //return node to storage pool//
end DELETES

procedure ADDQ(i,Y) //add Y to the ith queue//


call GETNODE(X)
DATA(X)  Y: LINK(X)  0
if F(i) = 0 then [F(i)  R(i)  X] //the queue was empty//
else [LINK(R(i))  X;R(i)  X] //the queue was not empty//
end ADDQ

procedure DELETEQ(i, Y)
//delete the first node in the i’th queue, set Y to its DATA field//
if F(i) = 0 then call QUEUE__EMPTY
else [X  F(i); F(i)  LINK(X) //set X to front node//
Y  DATA(X); call RET(X)] //remove data and return node//
end DELETEQ
 The solution presented above to the n-stack, m-queue problem is seen to be both
computationally and conceptually simple.
 There is no need to shift stacks or queues around to make space.
 Computation can proceed so long as there are free nodes. Though additional space is
needed for the link fields, the cost is no more than a factor of 2.
 Sometimes the DATA field does not use the whole word and it is possible to pack the
LINK and DATA fields into the same word.
 In such a case the storage requirements for sequential and linked representations would be
the same. For the use of linked lists to make sense, the overhead incurred by the storage
for the links must be overridden by:
i. The virtue of being able to represent complex lists all within the same array,
ii. The computing time for manipulating the lists is less than for sequential
representation.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 19
UNIT – III
STORAGE ALLOCATION & GARBAGE COLLECTION

The objective in the implementation of a storage pool is to make the running


times for Acquire and Release operations as small as possible. Ideally, both
operations run in constant time. In this section, we present a storage pool
implementation that uses a singly-linked list to keep track of the unused areas of
memory. The consequence of using an approach is that the running times are not
ideal. There are several requirements that the implementation of a storage pool
must satisfy: It must keep track somehow of the blocks of memory that have been
allocated as well as the areas of memory that remain unallocated. For example, in
order to implement the Acquire operation, we must have the means to locate an
unused area of memory of sufficient size in order to satisfy the request. The
approach taken in this section is to use a singly-linked list to keep track of the free
areas in the pool. In addition to keeping track of the free areas, it is necessary to
keep track of the size of each block that is allocated. This is necessary because
the Release operation takes only a pointer to the block of memory to be released.
i.e., the size of the block is not provided as an argument to the Release function.

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 20
UNIT – III

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 21
UNIT – III
Differences between SLL & DLL

Singly linked list (SLL) Doubly linked list (DLL)


A singly linked list is a linked list where A doubly linked list is complex type of linked
the node contains some data and a pointer list where the node contains some data and a
to the next node in the list pointer to the next as well as the previous node
in the list
It allows traversal only in one way It allows a two way traversal
It uses less memory per node (single It uses more memory per node(two pointers)
pointer)
Complexity of insertion and deletion at a Complexity of insertion and deletion at a known
known position is O(n) position is O(1)
If we need to save memory and searching If we need better performance while searching
is not required, we use singly linked list and memory is not a limitation, we go for
doubly linked list
If we know that an element is located If we know that an element is located towards
towards the end section, eg. ‘zebra’ still we the end section e.g. ’zebra’ we can start
need to begin from start and traverse the searching from the Back.
whole list
Singly linked list can mostly be used for They can be used to implement stacks, heaps,
stacks binary trees.

**** END OF UNIT-III NOTES ****

S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 22

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