0% found this document useful (0 votes)
18 views24 pages

Unit 3

Uploaded by

Sudip Mali
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)
18 views24 pages

Unit 3

Uploaded by

Sudip Mali
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/ 24

Unit 3:

Linked List
1. Linked List:
A linked list is a sequence of data structures (node), which are connected together
via links.
Linked List is linear data structures where the elements are not stored in
contiguous locations and every element is a separate object with two fields.
Each element is known as a node. Every node in linked list has two fields:
1. Data
2. Next

Data part of the node stores actual information that is to be represented by the node
The next field of every node is contains address of the next node in list. The next
field of last node is always NULL.

Uses of Linked List (over array):


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.
3. Empty node can not be present in the linked list.
4. We can store values of primitive types or objects in the singly linked list.
5. Due to the dynamicity and ease of insertions and deletions, they are
preferred over the arrays. It also has few disadvantages like the nodes cannot
be accessed directly instead we need to start from the head and follow
through the link to reach to a node we wish to access.
6. We can store values of primitive types or objects in the singly linked list.
Types of linked list:

2. Singly Linked List:


 Singly linked list can be defined as the collection of ordered set of elements.
 A node in the singly linked list consists of two parts: Data part and Next
part.
 Data part of the node stores actual information that is to be represented by
the node
 Next part of the node stores the address of its immediate successor node.

- Singly linked list can be traversed only in one direction.


- In other words, we can say that each node contains only next pointer,
therefore we can not traverse the list in the reverse direction.

linked list can be shown in the figure as follow:


In the above figure, the arrow represents the links. The data part of every node
contains the value to be stored in node.

The last node in the list is identified by the null pointer which is present in the
address part of the last node.

3. Operations on Singly linked list:

1. Create A node
2. Insert at beginning
3. Insert at end
4. Insert after specific node
5. Delete first node
6. Delete last node
7. Delete specific node
8. Traverse a list
9. Searching in a list

Create a node:

Structure of node with two fields can be defined using class as follows:

Class node

int data;

node next;

public : node()
{

Next=null;

};

New node can be created as follows:

node n1=new node();

Insertion:

The insertion into a singly linked list can be performed at different positions.
Based on the position of the new node being inserted, the insertion is categorized
into the following categories.

1 Insertion at It involves inserting any element at the front of the list. We


the beginning just need to a few link adjustments to make the new node as
the head of the list.
2 Insertion at It involves insertion at the last of the linked list. The new
the end node can be inserted as the only node in the list or it can be
inserted as the last one. Different logics are implemented in
each scenario.
3 Insertion after It involves insertion after the specified node of the linked
specific node list. We need to skip the desired number of nodes in order to
in list reach the node after which the new node will be inserted.

Insertion at the beginning of list:

There are the following steps which need to be followed in order to inser a new
node in the list at beginning.

o Allocate the space for the new node and store data into the data part of the
node. This will be done by the following statements
o Make the link part of the new node pointing to the existing first node of the
list. This will be done by using the following statement.
Algorithm
o Step 1: IF NEW = NULL

Write OVERFLOW
Go to Step 5
[END OF IF]

o Step 2: ELSE SET NEW_NODE → DATA = VAL


o Step 3: SET NEW_NODE → NEXT = HEAD
o Step 4: SET HEAD = NEW_NODE
o Step 5: EXIT

Insertion in singly linked list at the end

In order to insert a new node at the last, there are two following scenarios which
need to be mentioned.

1. The node is being added to an empty list


2. The node is being added to the end of the linked list

In the first case:


o The condition (head == NULL) gets satisfied. Hence, we just need to
allocate the space for the node and make this new node as a head in list

In the second case:


o Since Head is not null. Now, we need to declare a temporary node n1 in
order to traverse through the list. n1 is made to point the first node of the list.
o Then, traverse through the entire linked list using n1 so that at last n1 will be
at last node in list
o Now create link between n1 and the new node.

Algorithm
o Step 1: IF NEW = NULL

Write OVERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET NEW.DATA = VAL


o Step 3: SET NEW.NEXT = NULL
o Step 4: SET PTR = HEAD
o Step 5: Repeat Step 6 while PTR - > NEXT != NULL
o Step 6: SET PTR = PTR - > NEXT

[END OF LOOP]

o Step 7: SET PTR - > NEXT = NEW


o Step 8: EXIT

Insertion in singly linked list after specific node:


o we need to declare a temporary node n1 in order to reach to the given
specific node list. At start n1 is made to point the first node of the list.
o Then, traverse through the entire linked list using n1 so that n1 will point to
the given specific node
o Now add the new node after this n1 node in list

Algorithm
o STEP 1: IF NEW = NULL

WRITE OVERFLOW
GOTO STEP 12
END OF IF

o STEP 2: SET NEW.NEXT=NULL


o STEP 3: NEW.DATA = VAL
o STEP 4: SET TEMP = HEAD
o STEP 5: SET I = 0
o STEP 6: REPEAT STEP 7 AND 8 WHILE I WILL BECOME THE GIVEN VALUE
o STEP 7: I=I+1
o STEP 8: TEMP = TEMP → NEXT
o STEP 9: IF TEMP = NULL

WRITE "DESIRED NODE NOT PRESENT"


GOTO STEP 12
END OF IF
END OF LOOP

o STEP 10: NEW.NEXT = TEMP.NEXT


o STEP 11: TEMP.NEXT = NEW
o STEP 12: EXIT

Deletion

Deletion in singly linked list at beginning

Since the first node of the list is to be deleted, therefore, we just need to make the
head, point to the next of the head. This will be done by using the following
statements.

Algorithm
o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 5
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: SET HEAD = HEAD . NEXT
o Step 4: FREE PTR
o Step 5: EXIT

Deletion in singly linked list at the end

There are two scenarios in which, a node is deleted from the end of the linked list.

1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be
deleted.

In the first scenario,

Assign head → next = NULL will survive and therefore, the only node head of the
list will be assigned to null.

In the second scenario:


 As list is not empty we need to traverse list from HEAD o the second last
and last node in list.
 Set new node say TEMP1 nad TEMP2 to HEAD and next to HEAD
respectively. Traverse list till TEMP1 is at Second last node and TEMP2 is
at last node
 Assign TEMP.next=NULL and free TEMP2

Algorithm
o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: Repeat Steps 4 and 5 while PTR . NEXT!= NULL
o Step 4: SET PREPTR = PTR
o Step 5: SET PTR = PTR . NEXT

[END OF LOOP]

o Step 6: SET PREPTR . NEXT = NULL


o Step 7: FREE PTR
o Step 8: EXIT

Deletion in singly linked list after the specified node :

In order to delete the node, which is present after the specified node, we need to
skip the desired number of nodes to reach the node after which the node will be
deleted. We need to keep track of the two nodes. The one which is to be deleted the
other one if the node which is present before that node. For this purpose, two
pointers are used: ptr and ptr1.

Algorithm
o STEP 1: IF HEAD = NULL

WRITE UNDERFLOW
GOTO STEP 10
END OF IF

o STEP 2: SET TEMP = HEAD


o STEP 3: SET I = 0
o STEP 4: REPEAT STEP 5 TO 8 UNTIL I<loc< li=""></loc<>
o STEP 5: TEMP1 = TEMP
o STEP 6: TEMP = TEMP → NEXT
o STEP 7: IF TEMP = NULL

WRITE "DESIRED NODE NOT PRESENT"


GOTO STEP 12
END OF IF

o STEP 8: I = I+1

END OF LOOP

o STEP 9: TEMP1 → NEXT = TEMP → NEXT


o STEP 10: FREE TEMP
o STEP 11: EXIT

Traversing :

Traversing is the most common operation that is performed in almost every


scenario of singly linked list. Traversing means visiting each node of the list once
in order to perform some operation on that. This will be done by using the
following statements.

Algorithm
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL

WRITE "EMPTY LIST"


GOTO STEP 7
END OF IF

o STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL


o STEP 5: PRINT PTR→ DATA
o STEP 6: PTR = PTR → NEXT

[END OF LOOP]

o STEP 7: EXIT
Searching :

Searching is performed in order to find the location of a particular element in the


list. Searching any element in the list needs traversing through the list and make
the comparison of every element of the list with the specified element. If the
element is matched with any of the list element then the location of the element is
returned from the function.

Algorithm
o Step 1: SET PTR = HEAD
o Step 2: Set I = 0
o STEP 3: IF PTR = NULL

WRITE "EMPTY LIST"


GOTO STEP 8
END OF IF

o STEP 4: REPEAT STEP 5 TO 7 UNTIL PTR != NULL


o STEP 5: if ptr → data = item

write i+1
End of IF

o STEP 6: I = I + 1
o STEP 7: PTR = PTR → NEXT

[END OF LOOP]

o STEP 8: EXIT

Analysis of singly linked list operation:


3. Doubly Linked List:

 Doubly linked list is a complex type of linked list in which a node contains a
link to the previous as well as the next node in the sequence.
 Therefore, in a doubly linked list, a node consists of three parts: node data,
link to the next node in sequence, link to the previous node.
 A sample node in a doubly linked list is shown in the figure.

 A doubly linked list containing three nodes having numbers from 1 to 3 in


their data part, is shown in the following image.
Memory Representation of a doubly linked list:
 Memory Representation of a doubly linked list is shown in the following
image.
 Generally, doubly linked list consumes more space for every node and
therefore, causes more expansive basic operations such as insertion and
deletion.
 However, we can easily manipulate the elements of the list since the list
maintains pointers in both the directions (forward and backward).
 In the following image, the first element of the list that is i.e. 13 stored at
address 1. The head pointer points to the starting address 1. Since this is the
first element being added to the list therefore the prev of the
list contains null. The next node of the list resides at address 4 therefore the
first node contains 4 in its next pointer.

We can traverse the list in this way until we find any node containing null or -1 in
its next part

 Operations on doubly linked list:


Creating a node::
Algorithm :

Insertion at the beginning:


Algorithm :
o Step 1: IF NEW_NODE = NULL
Write OVERFLOW
Go to Step 7

[END OF IF]

o Step 2: SET NEW_NODE . DATA = VAL


o Step 3: SET NEW_NODE . PREV = NULL
o Step 4: SET NEW_NODE . NEXT = START
o Step 5: SET head . PREV = NEW_NODE
o Step 6: SET head = NEW_NODE
o Step 7: EXIT

Insertion at end:
Algorithm
o Step 1: IF NEW_NODE = NULL

Write OVERFLOW
Go to Step 9
[END OF IF]

o Step 2: SET NEW_NODE . DATA = VAL


o Step 3: SET NEW_NODE . NEXT = NULL
o Step 4: SET TEMP = HEAD
o Step 5: Repeat Step 8 while TEMP . NEXT != NULL
o Step 6: SET TEMP = TEMP . NEXT

[END OF LOOP]

o Step 7: SET TEMP . NEXT = NEW_NODE


o Step 8: SET NEW_NODE . PREV = TEMP
o Step 9: EXIT

Insert after specific node:


Algorithm
o Step 1: IF NEW_NODE = NULL

Write OVERFLOW
Go to Step 12
[END OF IF]
o Step 2: SET NEW_NODE . DATA = VAL
o Step 3: SET TEMP = HEAD
o Step 4: SET I = 0
o Step 5: REPEAT 8 and 9 until I<="" Specified value "">
o Step 6: SET TEMP = TEMP . NEXT
o Step 7: I++
o Step 8: SET NEW_NODE . NEXT = TEMP . NEXT
o Step 9: SET NEW_NODE . PREV = TEMP
o Step 10 : SET TEMP . NEXT = NEW_NODE
o Step 11: SET TEMP . NEXT . PREV = NEW_NODE
o Step 12: EXIT

Deleting at Beginning:
Algorithm
o STEP 1: IF HEAD = NULL

WRITE EMPTY_LIST
GOTO STEP 6

o STEP 2: SET PTR = HEAD


o STEP 3: SET HEAD = HEAD → NEXT
o STEP 4: SET HEAD → PREV = NULL
o STEP 5: FREE PTR
o STEP 6: EXIT

Deleting at End:
Algorithm:
o Step 1: IF HEAD = NULL

Write EMPTY_LIST
Go to Step 7
[END OF IF]

o Step 2: SET TEMP = HEAD


o Step 3: REPEAT STEP 4 WHILE TEMP.NEXT != NULL
o Step 4: SET TEMP = TEMP.NEXT

[END OF LOOP]
o Step 5: SET TEMP .PREV. NEXT = NULL
o Step 6: FREE TEMP
o Step 7: EXIT

Deleting a specific Node:


Algorithm:
o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 9
[END OF IF]

o Step 2: SET TEMP = HEAD


o Step 3: Repeat Step 4 while TEMP . DATA != ITEM
o Step 4: SET TEMP = TEMP . NEXT

[END OF LOOP]

o Step 5: SET PTR = TEMP . NEXT


o Step 6: SET TEMP . NEXT = PTR . NEXT
o Step 7: SET PTR . NEXT . PREV = TEMP
o Step 8: FREE PTR
o Step 9: EXIT

Searching a specific data:


Algorithm:
o Step 1: IF HEAD == NULL

WRITE "UNDERFLOW"
GOTO STEP 8
[END OF IF]

o Step 2: Set PTR = HEAD


o Step 3: Set i = 0
o Step 4: Repeat step 5 to 7 while PTR != NULL
o Step 5: IF PTR → data = item

return i
[END OF IF]
o Step 6: i = i + 1
o Step 7: PTR = PTR → next
o Step 8: Exit

Traversing:
Algorithm:
o Step 1: IF HEAD == NULL

WRITE "UNDERFLOW"
GOTO STEP 6
[END OF IF]

o Step 2: Set PTR = HEAD


o Step 3: Repeat step 4 and 5 while PTR != NULL
o Step 4: Write PTR → data
o Step 5: PTR = PTR → next
o Step 6: Exit

4. Circular Singly Linked List


 In a circular Singly linked list, the last node of the list contains a pointer to
the first node of the list. We can have circular singly linked list as well as
circular doubly linked list.
 We traverse a circular singly linked list until we reach the same node where
we started. The circular singly liked list has no beginning and no ending.
There is no null value present in the next part of any of the nodes.
 The following image shows a circular singly linked list.
Memory Representation of circular linked list:

 In the following image, memory representation of a circular linked list


containing marks of a student in 4 subjects. However, the image shows a
glimpse of how the circular list is being stored in the memory. The start or
head of the list is pointing to the element with the index 1 and containing 13
marks in the data part and 4 in the next part. Which means that it is linked
with the node that is being stored at 4th index of the list.
 However, due to the fact that we are considering circular linked list in the
memory therefore the last node of the list contains the address of the first
node of the list.

Operations on Circular Linked list:

Insertion aafter specific node and Delting a specific node operations are
same as that of singly linked list. Refer given earlier notes for these
operation.

SN Operation Description

1 Insertion at Adding a node into circular singly linked list at the beginning.
beginning

2 Insertion at the end Adding a node into circular singly linked list at the end.
3 Deletion at beginning Removing the node from circular singly linked list at the beginning.

4 Deletion at the end Removing the node from circular singly linked list at the end.

5 Searching Compare each element of the node with the given item and return
the location at which the item is present in the list otherwise return null.

6 Traversing Visiting each element of the list at least once in order to perform some
specific operation.

Insertion at beginning:
Algorithm:
o Step 1: IF NEW_NODE = NULL

Write OVERFLOW
Go to Step 9
[END OF IF]

o Step 2: SET NEW_NODE . DATA = VAL


o Step 3: SET TEMP = HEAD
o Step 4: Repeat Step 5 while TEMP . NEXT != HEAD
o Step 5: SET TEMP = TEMP . NEXT

[END OF LOOP]

o Step 6: SET NEW_NODE . NEXT = HEAD


o Step 7: SET TEMP. NEXT = NEW_NODE
o Step 8: SET HEAD = NEW_NODE
o Step 9: EXIT

Insertion at end:
Algorithm:
o Step 1: IF NEW_NODE = NULL

Write OVERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET NEW_NODE . DATA = VAL


o Step 3: SET TEMP = HEAD
o Step 4: Repeat Step 8 while TEMP . NEXT != HEAD
o Step 5: SET TEMP = TEMP . NEXT

[END OF LOOP]

o Step 6: SET TEMP . NEXT = NEW_NODE


o Step 7: SET NEW_NODE . NEXT = HEAD
o Step 8: EXIT

Deleting from Beginning:


Algorithm:
o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: Repeat Step 4 while PTR.NEXT != HEAD
o Step 4: SET PTR = PTR.next

[END OF LOOP]

o Step 5: SET PTR. NEXT = HEAD.NEXT


o Step 6: FREE HEAD
o Step 7: SET HEAD = PTR.NEXT
o Step 8: EXIT

Deleting at end:
Algorithm:
o Step 1: IF HEAD = NULL

Write UNDERFLOW
Go to Step 8
[END OF IF]

o Step 2: SET PTR = HEAD


o Step 3: SET PTR1=HEAD.next
o Step 3: Repeat Steps 4 and 5 while PTR . NEXT != HEAD
o Step 4: SET PTR1.next=PTR1
o Step 5: SET PTR = PTR . NEXT

[END OF LOOP]

o Step 6: SET PTR . NEXT = HEAD


o Step 7: FREE PTR1
o Step 8: EXIT

Traversing:
Algorithm:
o STEP 1: SET PTR = HEAD
o STEP 2: IF PTR = NULL

WRITE "EMPTY LIST"


GOTO STEP 7
END OF IF

o STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR = HEAD


o STEP 5: PRINT PTR. DATA
o STEP 6: PTR = PTR . NEXT

[END OF LOOP]

o STEP 7: EXIT

5. Linked list representation of stack:


 Instead of using array to create stack we can use linked list.
 One of the advantages of creating stack using linked list that it allocate
memory dynamically.
 Here we are storing stack information in the form of nodes. And such nodes
are connected with each other through link to pretend the continuation.
 Though we are using linked list, insertion and deletion will takes place as
par the stack strategies.
 Hence insertion and deletion both operations will use insertion at end
algorithm as stack operations carried out in LIFO manners.
 Diagrammatic representation of stack using linked list is as follows:
 push( ) operation: push operation is same as that of insertion at end in singly
linked list.(Use notes and algorithm from above notes)
 pop( ) operation: pop operation is same as that of deletion at end in singly
linked list.(Use notes and algorithm from above notes)

6. Linked list representation of Queue:


 Instead of using array to create Queue we can use linked list.
One of the advantages of creating queue using linked list that it allocate
memory dynamically.
 Here we are storing queue information in the form of nodes. And such nodes
are connected with each other through link to pretend the continuation.
 Though we are using linked list, insertion and deletion will takes place as
par the queue strategies.
 Hence insertion operation will use insertion at end algorithm and deletion
operation will use deletion from beginning algorithm from linked list (as
queue operations carried out in FIFO manners.)
 Diagrammatic representation of queue using linked list is as follows:
 Insertion in queue operation: insertion operation is same as that of insertion
at end in singly linked list.(Use notes and algorithm from above notes)
 deletion operation: deletion operation is same as that of deletion at beginning
in singly linked list.(Use notes and algorithm from above notes)

7. Time complexity Analysis:


Singly Linked lists ( SLL)

A linked list can typically only be accessed via its head node. From there you can
only traverse from node to node until you reach the node you seek. Thus traverse
is O(n).

Searching for a given value in a linked list similarly requires traversing all the
elements until you find that value. Thus search is O(n).
Inserting into a linked list requires re-pointing the previous node (the node before
the insertion point) to the inserted node, and pointing the newly-inserted node to
the next node. Thus insertion is O(1).

Deleting from a linked list requires re-pointing the previous node (the node before
the deleted node) to the next node (the node after the deleted node). Thus deletion
is O(1).

Time Complexity (Singly linked List):

Operation Average Worst

Access O(n) O(n)

Search O(n) O(n)

Insertion O(1) O(1)


Operation Average Worst

Deletion O(1) O(1)

Time Complexity (Doubly Linked List):

Operation Average Worst

Access O(n) O(n)

Search O(n) O(n)

Insertion O(1) O(1)

Deletion O(n) O(n)

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