SlideShare a Scribd company logo
1 of 45
Download to read offline
Data Structures
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423603
(An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Information Technology
(NBAAccredited)
Ms. K. D. Patil
Assistant Professor
Linked Organization
• Introduction, References and Basic Types, Linked list ADT,
Implementing Singly linked list, Doubly linked lists, Implementing
Doubly linked list, Implementation of stack using Linked organization,
Applications: well formed-ness of parenthesis, Implementation of
queue using linked organization, cloning Data Structures, Linked list
efficiency
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
Learning Outcomes
• At the end of this unit, student will able to-
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
Recap!!!
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Data Structures: It is a particular way of organizing data in a computer.
Reference https://www.geeksforgeeks.org/difference-between-linear-and-non-linear-data-structures/
Introduction to Linked Organization
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Drawbacks of Sequential Organization
• The size of array must be known in advance before using it in the program.
• It is almost impossible to expand the size of the array at run time.
• 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.
Introduction to Linked Organization
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• The linked list is suitable for use in many kinds of general-purpose databases.
• 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.
• 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.
• You can use a linked list in many cases in which you use an array, unless you need frequent
random access to individual items using an index.
• Simpler than some other popular structures such as trees.
Introduction to Linked Organization
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• A linked list is an ordered collection of finite, homogeneous data elements called
nodes where the linear order is maintained by means of links or pointers
• Node – This part stores the data.
• Link – This part stores the address of the memory location, where the next data of the list is
stored.
Linked List ADT
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• // shows how to define a (single) linked list
class LinkedList {
Node head;
class Node
{
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
// Method to maintain the collection to be defined
}
Linked List ADT
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Methods
• Insertion operations
• Insertion at front
• Insertion at end
• Insertion at any position
• Traversal
• Printing the collection
• Reversing the ordering of elements
Linked List ADT
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Methods
• Merging operation
• Merging two list into a single list
• Deletion operation
• Deletion from front
• Deletion from end
• Deletion from any position
Types of Linked List
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Single Linked List
• Double Linked List
• Circular Linked List
Insert Operation : Insertion at front (SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public Node insertFront(int data) {
if(head == NULL) // check if linked list is empty
return newNode;
else{ // Create a new node with given data
Node newNode = new Node(data);
newNode.next = head;
// Make the new node as the first node
head = newNode;
Return head;
}
}
Insert Operation : Insertion at End (SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void insertEnd(int data){
if( head == NULL ){
newNode = new Node(data);
head = newNode;
return head; }
else {
Node newNode = new Node(data);
newNode.next = null;
Node temp = head;
while(temp.next != null) {
// traversing the list to get the last node
temp = temp.next;
}
temp.next = newNode;
} }
Insert Operation : Insertion at any position
(SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void insertKey(int data , int key) {
Node newNode = new Node(data);
newNode.next = null;
Node temp = head;
boolean status = false;
while(temp != null) {
if(temp.data == key) {
status = true;
break;
}
temp = temp.next;
}
if(status)
{
newNode.next = temp.next;
temp.next = newNode;
}
}
Insert Operation : Insertion at any position
(SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void insertKey(int data , int key) {
Node newNode = new Node(data);
newNode.next = null;
Node temp = head;
boolean status = false;
while(temp != null) {
if(temp.data == key) {
status = true;
break;
}
temp = temp.next;
}
if(status)
{
newNode.next = temp.next;
temp.next = newNode;
}
}
Delete Operation : Deletion at front (SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• To delete a node from the linked list, we need to do the following steps.
• Find the previous node of the node to be deleted.
• Change the next of the previous node.
• Free memory for the node to be deleted.
Delete Operation : Deletion at front (SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void deleteFront (int data) {
Node temp = head, prev = null; // Store head node
if (temp != null && temp.data == key) // If head node itself holds the key to be deleted
{
head = temp.next; // Changed head
return;
}
Delete Operation : Deletion at end (SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void deleteEnd (int data) {
Node temp = head, prev = null; // Store head node
if (temp != null) // If the list is not empty
{
while(temp != null) {
// Move to the end node
prev = temp;
temp = temp.next;
}
x = temp.data;
prev.next = null;
}
return x;
}
Delete Operation : Deletion at any Position
(SLL)
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void deleteAnyPosition (int data, int key) {
Node temp = head, prev = null;
while(temp != null) {
if(temp.data == key) {
prev.next = temp.next; // Display the message
System.out.println(key+ " position element deleted");
break;
}
else {
prev = temp;
temp = temp.next;
}
} }
temp
temp (else part)
Time Complexity of SLL
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Accessing the data in linked list nodes takes linear time because of the need to search
through the entire list via pointers.
• It's also important to note that there is no way of optimizing search in linked lists.
• In the array, we could at least keep the array sorted. However, since we don't know how long
the linked list is, there is no way of performing a binary search:
• Time Complexity is as follows:
• Insertion & Deletion at the beginning and end - O(1)
• Insertion & Deletion at specific position - O(n)
• Indexing - O(n)
• Search - O(n)
Time and Space Complexity of SLL
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Linked lists hold two main pieces of information (the value and pointer) per node.
This means that the amount of data stored increases linearly with the number of
nodes in the list. Therefore, the space complexity of the linked list is O(n)
Doubly Linked List
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Doubly linked list is a data structure that has reference to both the previous and next
nodes in the list. It provides simplicity to traverse, insert and delete the nodes in both
directions in a list.
• It consists of three parts: node data, link to the next node in sequence, link to the
previous node.
Doubly Linked List
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• In a doubly linked list, each node contains three data members:
data: The data stored in the node
next: It refers to the reference to the next node
prev: It refers to the reference to the previous node
Doubly Linked List : Define Node, class
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
Node Creation
public class Node {
int data;
Node prev;
Node next;
public Node(int data)
{
this.data = data;
this.prev = null;
this.next = null;
}
}
class Creation
public class DoublyLinkedList {
Node head;
Node tail;
public DoublyLinkedList()
{
this.head = null;
this.tail = null;
}
}
Doubly Linked List : Traversing
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Start from the head node and follow
the next reference until we reach
the end of the list
• Can also start from the tail node
and follow the prev until we reach
the head of the node
// Traversing from head to the end of the list
public void traverseForward() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
// Traversing from tail to the head
public void traverseBackward()
{
Node current = tail;
while (current != null) {
System.out.print(current.data + " ");
current = current.prev;
}
}
Doubly Linked List : Insertion
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• To insert a node in a doubly linked list
• Create a new node that is going to be inserted
• Update the references of the adjacent nodes to add the new node
• Update the head or tail of the list if the new node is being inserted at the
beginning or end of the list.
• A node can be added to a Doubly Linked List in three ways:
• Insertion at the beginning of the list
• Insertion at a specific position in the list
• Insertion at the end of the list
Doubly Linked List : Insertion at front
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Create a new node to be inserted.
• Set the next pointer of the new node to the current head of the doubly linked list.
• Set the previous pointer of the new node to null, as it will be the new head.
• If the doubly linked list is not empty (i.e., the current head is not null), set the
previous pointer of the current head to the new node.
Doubly Linked List : Insertion at front
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void insertAtBeginning(int data)
{
Node temp = new Node(data);
if (head == null) {
head = temp;
tail = temp;
}
else {
temp.next = head;
head.prev = temp;
head = temp;
}
}
temp
Doubly Linked List : Insertion at end
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Create a new node to be inserted.
• Check if the list is empty or not. If it is empty then we will set both the head and tail
to the new node temp.
• If the list is not empty, traverse to the last node of the doubly linked list.
• Set the next pointer of the last node to the new node.
• Set the previous pointer of the new node to the current last node.
• Set the next pointer of the new node to null, as it will be the last node in the list
Doubly Linked List : Insertion at end
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void insertAtEnd(int data)
{
Node temp = new Node(data);
if (tail == null) {
head = temp;
tail = temp;
}
else {
tail.next = temp;
temp.prev = tail;
tail = temp;
}
}
temp
Doubly Linked List : Insertion at specific
position
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Create a new node to be inserted.
• Set the next pointer of the new node to the next node of the node at given position.
• Set the previous pointer of the new node to the node at given position.
• Set the next pointer of the node at the given position to the new node.
• If the next node of the new node is not null, set its previous pointer to the new
node.
Doubly Linked List : Insertion at specific
position
public void insertAtPosition(int data, int position)
{
Node temp = new Node(data);
if (position == 1) {
insertAtBeginning(data);
}
temp
else {
Node current = head;
int currPosition = 1;
while (current != null && currPosition < position)
{
current = current.next;
currPosition++;
}
Doubly Linked List : Insertion at specific
position
if (current == null) { insertAtEnd(data); }
else {
temp.next = current;
temp.prev = current.prev;
current.prev.next = temp;
current.prev = temp;
} } }
temp
Doubly Linked List : Deletion
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• To delete a node in a doubly linked list
• first, we need to update the references of the adjacent nodes to delete the node
• Update the head or tail of the list if the node is being deleted at the beginning or
end of the list.
• The deletion of a node in a doubly-linked list can be divided into three main
categories:
• Deletion of the first node in the list.
• Deletion of a node at a specific position in the list.
• Deletion of the last node in the list.
Doubly Linked List : Deletion at front
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• First, we will check if the list is empty if it is then we return
• if there is only one node in the list then we will set both the head and tail to null
• Otherwise we will simply set the head node to the next node of the current head in
the list, and set the previous reference of the new head node to null.
Doubly Linked List : Deletion at front
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void deleteAtBeginning() {
if (head == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
return;
}
Node temp = head;
head = head.next;
head.prev = null;
temp.next = null;
}
Before Deletion
After Deletion
Doubly Linked List : Deletion at position
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• If the given position is 1 (i.e., the first node), update the head of the doubly linked
list to be the next node of the current head.
• Traverse to the node at the given position, keeping track of the previous node.
• Set the next pointer of the previous node to the next node of the node to be
deleted.
• If the next node of the node to be deleted is not null, set its previous pointer to the
previous node.
• Delete the node.
Doubly Linked List : Deletion at position
public void delete(int pos)
{
if (head == null) {
return;
}
if (pos == 1) {
deleteAtBeginning();
return;
}
Node current = head;
int count = 1;
while (current != null && count != pos) {
current = current.next;
count++;
}
Before Deletion
After Deletion of second node
Doubly Linked List : Deletion at position
if (current == null) {
System.out.println("Position wrong");
return;
}
if (current == tail) {
deleteAtEnd();
return;
}
current.prev.next = current.next;
current.next.prev = current.prev;
current.prev = null;
current.next = null;
}
Before Deletion
After Deletion of second node
Doubly Linked List : Deletion of last node
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• First, we will check if the list is empty if it is then we return
• if there is only one node in the list then we will set both the head and tail to null
• otherwise we will simply set the tail node to the previous node of the current tail
node in the list, set the prev of the current tail node as null and set the next
reference of the new tail node to null.
Doubly Linked List : Deletion of last node
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
public void deleteAtEnd() {
if (tail == null) {
return;
}
if (head == tail) {
head = null;
tail = null;
return;
}
Node temp = tail;
tail = tail.prev;
tail.next = null;
temp.prev = null;
}
Before Deletion
After Deletion
Time Complexity of DLL
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Time Complexity is as follows:
• Insertion - O(1)
• Deletion - O(1)
• Indexing - O(n)
• Search - O(n)
• Space complexity of the linked list is O(n)
Applications of Linked Organization
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• Sparse matrix manipulation
• Polynomial manipulation
• Memory management Applications of linked lists N
References
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
• R. Lafore, “Data structures and Algorithms in Java”, Pearson education, ISBN: 9788
131718124.
• Michael Goodrich, Roberto Tamassia, Michael H. Goldwasser, “Data Structures and
Algorithms in Java”, 6th edition, wiley publication, ISBN: 978-1-118-77133-4
• R. Gilberg, B. Forouzan, “Data Structure: A Pseudo code approach with C++”, Cengage
Learning.
• https://www.geeksforgeeks.org/linked-list-in-java/
• https://prepinsta.com/java-program/linked-list/
• Animation Video:
https://www.youtube.com/watch?v=iNUS9iZwrVA&ab_channel=TechnologyPremiere
Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
Thank You!!!
Happy Learning!!!

More Related Content

Similar to Unit 1_SLL and DLL.pdf

Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1Amar Rawat
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Getachew Ganfur
 
SIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxSIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxsagabo1
 
Unit.1 Introduction to Data Structuresres
Unit.1 Introduction to Data StructuresresUnit.1 Introduction to Data Structuresres
Unit.1 Introduction to Data Structuresresamplopsurat
 
Data and File Structure Lecture Notes
Data and File Structure Lecture NotesData and File Structure Lecture Notes
Data and File Structure Lecture NotesFellowBuddy.com
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.pptclassall
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURESUsha Mahalingam
 

Similar to Unit 1_SLL and DLL.pdf (20)

Data structure unitfirst part1
Data structure unitfirst part1Data structure unitfirst part1
Data structure unitfirst part1
 
Linked Lists
Linked ListsLinked Lists
Linked Lists
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
DSA - Copy.pptx
DSA - Copy.pptxDSA - Copy.pptx
DSA - Copy.pptx
 
SIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptxSIX WEEKS SUMMER TRAINING REPORT.pptx
SIX WEEKS SUMMER TRAINING REPORT.pptx
 
Unit.1 Introduction to Data Structuresres
Unit.1 Introduction to Data StructuresresUnit.1 Introduction to Data Structuresres
Unit.1 Introduction to Data Structuresres
 
Data and File Structure Lecture Notes
Data and File Structure Lecture NotesData and File Structure Lecture Notes
Data and File Structure Lecture Notes
 
Link list
Link listLink list
Link list
 
Linked list
Linked listLinked list
Linked list
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
Data structure
 Data structure Data structure
Data structure
 
Unit 1 LINEAR DATA STRUCTURES
Unit 1  LINEAR DATA STRUCTURESUnit 1  LINEAR DATA STRUCTURES
Unit 1 LINEAR DATA STRUCTURES
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
 

More from KanchanPatil34

Unit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdfUnit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdfKanchanPatil34
 
Unit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfUnit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfKanchanPatil34
 
PAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 MicroporcessorPAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 MicroporcessorKanchanPatil34
 
PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386KanchanPatil34
 
PAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessorPAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessorKanchanPatil34
 
PAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentationPAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentationKanchanPatil34
 
SE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentationSE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentationKanchanPatil34
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1KanchanPatil34
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2KanchanPatil34
 
SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3
SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3
SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3KanchanPatil34
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2KanchanPatil34
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1KanchanPatil34
 
SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051KanchanPatil34
 
Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2KanchanPatil34
 
Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1KanchanPatil34
 
Unit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idtUnit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idtKanchanPatil34
 
Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386KanchanPatil34
 
Unit i se pai_dos function calls
Unit i se pai_dos function callsUnit i se pai_dos function calls
Unit i se pai_dos function callsKanchanPatil34
 

More from KanchanPatil34 (20)

Unit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdfUnit 2_2 Binary Tree as ADT_General Tree.pdf
Unit 2_2 Binary Tree as ADT_General Tree.pdf
 
Unit 2_1 Tree.pdf
Unit 2_1 Tree.pdfUnit 2_1 Tree.pdf
Unit 2_1 Tree.pdf
 
Unit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfUnit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdf
 
PAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 MicroporcessorPAI Unit 3 Paging in 80386 Microporcessor
PAI Unit 3 Paging in 80386 Microporcessor
 
PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386PAI Unit 3 Multitasking in 80386
PAI Unit 3 Multitasking in 80386
 
PAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessorPAI Unit 2 Segmentation in 80386 microprocessor
PAI Unit 2 Segmentation in 80386 microprocessor
 
PAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentationPAI Unit 2 Protection in 80386 segmentation
PAI Unit 2 Protection in 80386 segmentation
 
SE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentationSE PAI Unit 2_Data Structures in 80386 segmentation
SE PAI Unit 2_Data Structures in 80386 segmentation
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 1
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
 
SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3
SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3
SE PAI Unit 5_Serial Port Programming in 8051 micro controller_Part 3
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 2
 
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
SE PAI Unit 5_Serial Port Programming in 8051 microcontroller_Part 1
 
SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051SE PAI Unit 5_IO programming in 8051
SE PAI Unit 5_IO programming in 8051
 
Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2Unit 5_Interrupt programming in 8051 micro controller - part 2
Unit 5_Interrupt programming in 8051 micro controller - part 2
 
Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1Unit 5_interrupt programming_Part 1
Unit 5_interrupt programming_Part 1
 
8051 interfacing
8051 interfacing8051 interfacing
8051 interfacing
 
Unit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idtUnit 3 se pai_ivt and idt
Unit 3 se pai_ivt and idt
 
Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386Unit 2 se pai_registers in 80386
Unit 2 se pai_registers in 80386
 
Unit i se pai_dos function calls
Unit i se pai_dos function callsUnit i se pai_dos function calls
Unit i se pai_dos function calls
 

Recently uploaded

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Unit 1_SLL and DLL.pdf

  • 1. Data Structures Sanjivani Rural Education Society’s Sanjivani College of Engineering, Kopargaon-423603 (An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune) NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified Department of Information Technology (NBAAccredited) Ms. K. D. Patil Assistant Professor
  • 2. Linked Organization • Introduction, References and Basic Types, Linked list ADT, Implementing Singly linked list, Doubly linked lists, Implementing Doubly linked list, Implementation of stack using Linked organization, Applications: well formed-ness of parenthesis, Implementation of queue using linked organization, cloning Data Structures, Linked list efficiency Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
  • 3. Learning Outcomes • At the end of this unit, student will able to- Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology
  • 4. Recap!!! Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Data Structures: It is a particular way of organizing data in a computer. Reference https://www.geeksforgeeks.org/difference-between-linear-and-non-linear-data-structures/
  • 5. Introduction to Linked Organization Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Drawbacks of Sequential Organization • The size of array must be known in advance before using it in the program. • It is almost impossible to expand the size of the array at run time. • 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.
  • 6. Introduction to Linked Organization Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • The linked list is suitable for use in many kinds of general-purpose databases. • 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. • 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. • You can use a linked list in many cases in which you use an array, unless you need frequent random access to individual items using an index. • Simpler than some other popular structures such as trees.
  • 7. Introduction to Linked Organization Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • A linked list is an ordered collection of finite, homogeneous data elements called nodes where the linear order is maintained by means of links or pointers • Node – This part stores the data. • Link – This part stores the address of the memory location, where the next data of the list is stored.
  • 8. Linked List ADT Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • // shows how to define a (single) linked list class LinkedList { Node head; class Node { int data; Node next; Node(int x) // parameterized constructor { data = x; next = null; } } // Method to maintain the collection to be defined }
  • 9. Linked List ADT Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Methods • Insertion operations • Insertion at front • Insertion at end • Insertion at any position • Traversal • Printing the collection • Reversing the ordering of elements
  • 10. Linked List ADT Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Methods • Merging operation • Merging two list into a single list • Deletion operation • Deletion from front • Deletion from end • Deletion from any position
  • 11. Types of Linked List Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Single Linked List • Double Linked List • Circular Linked List
  • 12. Insert Operation : Insertion at front (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public Node insertFront(int data) { if(head == NULL) // check if linked list is empty return newNode; else{ // Create a new node with given data Node newNode = new Node(data); newNode.next = head; // Make the new node as the first node head = newNode; Return head; } }
  • 13. Insert Operation : Insertion at End (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void insertEnd(int data){ if( head == NULL ){ newNode = new Node(data); head = newNode; return head; } else { Node newNode = new Node(data); newNode.next = null; Node temp = head; while(temp.next != null) { // traversing the list to get the last node temp = temp.next; } temp.next = newNode; } }
  • 14. Insert Operation : Insertion at any position (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void insertKey(int data , int key) { Node newNode = new Node(data); newNode.next = null; Node temp = head; boolean status = false; while(temp != null) { if(temp.data == key) { status = true; break; } temp = temp.next; } if(status) { newNode.next = temp.next; temp.next = newNode; } }
  • 15. Insert Operation : Insertion at any position (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void insertKey(int data , int key) { Node newNode = new Node(data); newNode.next = null; Node temp = head; boolean status = false; while(temp != null) { if(temp.data == key) { status = true; break; } temp = temp.next; } if(status) { newNode.next = temp.next; temp.next = newNode; } }
  • 16. Delete Operation : Deletion at front (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • To delete a node from the linked list, we need to do the following steps. • Find the previous node of the node to be deleted. • Change the next of the previous node. • Free memory for the node to be deleted.
  • 17. Delete Operation : Deletion at front (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void deleteFront (int data) { Node temp = head, prev = null; // Store head node if (temp != null && temp.data == key) // If head node itself holds the key to be deleted { head = temp.next; // Changed head return; }
  • 18. Delete Operation : Deletion at end (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void deleteEnd (int data) { Node temp = head, prev = null; // Store head node if (temp != null) // If the list is not empty { while(temp != null) { // Move to the end node prev = temp; temp = temp.next; } x = temp.data; prev.next = null; } return x; }
  • 19. Delete Operation : Deletion at any Position (SLL) Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void deleteAnyPosition (int data, int key) { Node temp = head, prev = null; while(temp != null) { if(temp.data == key) { prev.next = temp.next; // Display the message System.out.println(key+ " position element deleted"); break; } else { prev = temp; temp = temp.next; } } } temp temp (else part)
  • 20. Time Complexity of SLL Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Accessing the data in linked list nodes takes linear time because of the need to search through the entire list via pointers. • It's also important to note that there is no way of optimizing search in linked lists. • In the array, we could at least keep the array sorted. However, since we don't know how long the linked list is, there is no way of performing a binary search: • Time Complexity is as follows: • Insertion & Deletion at the beginning and end - O(1) • Insertion & Deletion at specific position - O(n) • Indexing - O(n) • Search - O(n)
  • 21. Time and Space Complexity of SLL Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Linked lists hold two main pieces of information (the value and pointer) per node. This means that the amount of data stored increases linearly with the number of nodes in the list. Therefore, the space complexity of the linked list is O(n)
  • 22. Doubly Linked List Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Doubly linked list is a data structure that has reference to both the previous and next nodes in the list. It provides simplicity to traverse, insert and delete the nodes in both directions in a list. • It consists of three parts: node data, link to the next node in sequence, link to the previous node.
  • 23. Doubly Linked List Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • In a doubly linked list, each node contains three data members: data: The data stored in the node next: It refers to the reference to the next node prev: It refers to the reference to the previous node
  • 24. Doubly Linked List : Define Node, class Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology Node Creation public class Node { int data; Node prev; Node next; public Node(int data) { this.data = data; this.prev = null; this.next = null; } } class Creation public class DoublyLinkedList { Node head; Node tail; public DoublyLinkedList() { this.head = null; this.tail = null; } }
  • 25. Doubly Linked List : Traversing Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Start from the head node and follow the next reference until we reach the end of the list • Can also start from the tail node and follow the prev until we reach the head of the node // Traversing from head to the end of the list public void traverseForward() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } } // Traversing from tail to the head public void traverseBackward() { Node current = tail; while (current != null) { System.out.print(current.data + " "); current = current.prev; } }
  • 26. Doubly Linked List : Insertion Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • To insert a node in a doubly linked list • Create a new node that is going to be inserted • Update the references of the adjacent nodes to add the new node • Update the head or tail of the list if the new node is being inserted at the beginning or end of the list. • A node can be added to a Doubly Linked List in three ways: • Insertion at the beginning of the list • Insertion at a specific position in the list • Insertion at the end of the list
  • 27. Doubly Linked List : Insertion at front Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Create a new node to be inserted. • Set the next pointer of the new node to the current head of the doubly linked list. • Set the previous pointer of the new node to null, as it will be the new head. • If the doubly linked list is not empty (i.e., the current head is not null), set the previous pointer of the current head to the new node.
  • 28. Doubly Linked List : Insertion at front Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void insertAtBeginning(int data) { Node temp = new Node(data); if (head == null) { head = temp; tail = temp; } else { temp.next = head; head.prev = temp; head = temp; } } temp
  • 29. Doubly Linked List : Insertion at end Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Create a new node to be inserted. • Check if the list is empty or not. If it is empty then we will set both the head and tail to the new node temp. • If the list is not empty, traverse to the last node of the doubly linked list. • Set the next pointer of the last node to the new node. • Set the previous pointer of the new node to the current last node. • Set the next pointer of the new node to null, as it will be the last node in the list
  • 30. Doubly Linked List : Insertion at end Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void insertAtEnd(int data) { Node temp = new Node(data); if (tail == null) { head = temp; tail = temp; } else { tail.next = temp; temp.prev = tail; tail = temp; } } temp
  • 31. Doubly Linked List : Insertion at specific position Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Create a new node to be inserted. • Set the next pointer of the new node to the next node of the node at given position. • Set the previous pointer of the new node to the node at given position. • Set the next pointer of the node at the given position to the new node. • If the next node of the new node is not null, set its previous pointer to the new node.
  • 32. Doubly Linked List : Insertion at specific position public void insertAtPosition(int data, int position) { Node temp = new Node(data); if (position == 1) { insertAtBeginning(data); } temp else { Node current = head; int currPosition = 1; while (current != null && currPosition < position) { current = current.next; currPosition++; }
  • 33. Doubly Linked List : Insertion at specific position if (current == null) { insertAtEnd(data); } else { temp.next = current; temp.prev = current.prev; current.prev.next = temp; current.prev = temp; } } } temp
  • 34. Doubly Linked List : Deletion Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • To delete a node in a doubly linked list • first, we need to update the references of the adjacent nodes to delete the node • Update the head or tail of the list if the node is being deleted at the beginning or end of the list. • The deletion of a node in a doubly-linked list can be divided into three main categories: • Deletion of the first node in the list. • Deletion of a node at a specific position in the list. • Deletion of the last node in the list.
  • 35. Doubly Linked List : Deletion at front Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • First, we will check if the list is empty if it is then we return • if there is only one node in the list then we will set both the head and tail to null • Otherwise we will simply set the head node to the next node of the current head in the list, and set the previous reference of the new head node to null.
  • 36. Doubly Linked List : Deletion at front Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void deleteAtBeginning() { if (head == null) { return; } if (head == tail) { head = null; tail = null; return; } Node temp = head; head = head.next; head.prev = null; temp.next = null; } Before Deletion After Deletion
  • 37. Doubly Linked List : Deletion at position Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • If the given position is 1 (i.e., the first node), update the head of the doubly linked list to be the next node of the current head. • Traverse to the node at the given position, keeping track of the previous node. • Set the next pointer of the previous node to the next node of the node to be deleted. • If the next node of the node to be deleted is not null, set its previous pointer to the previous node. • Delete the node.
  • 38. Doubly Linked List : Deletion at position public void delete(int pos) { if (head == null) { return; } if (pos == 1) { deleteAtBeginning(); return; } Node current = head; int count = 1; while (current != null && count != pos) { current = current.next; count++; } Before Deletion After Deletion of second node
  • 39. Doubly Linked List : Deletion at position if (current == null) { System.out.println("Position wrong"); return; } if (current == tail) { deleteAtEnd(); return; } current.prev.next = current.next; current.next.prev = current.prev; current.prev = null; current.next = null; } Before Deletion After Deletion of second node
  • 40. Doubly Linked List : Deletion of last node Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • First, we will check if the list is empty if it is then we return • if there is only one node in the list then we will set both the head and tail to null • otherwise we will simply set the tail node to the previous node of the current tail node in the list, set the prev of the current tail node as null and set the next reference of the new tail node to null.
  • 41. Doubly Linked List : Deletion of last node Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology public void deleteAtEnd() { if (tail == null) { return; } if (head == tail) { head = null; tail = null; return; } Node temp = tail; tail = tail.prev; tail.next = null; temp.prev = null; } Before Deletion After Deletion
  • 42. Time Complexity of DLL Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Time Complexity is as follows: • Insertion - O(1) • Deletion - O(1) • Indexing - O(n) • Search - O(n) • Space complexity of the linked list is O(n)
  • 43. Applications of Linked Organization Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • Sparse matrix manipulation • Polynomial manipulation • Memory management Applications of linked lists N
  • 44. References Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology • R. Lafore, “Data structures and Algorithms in Java”, Pearson education, ISBN: 9788 131718124. • Michael Goodrich, Roberto Tamassia, Michael H. Goldwasser, “Data Structures and Algorithms in Java”, 6th edition, wiley publication, ISBN: 978-1-118-77133-4 • R. Gilberg, B. Forouzan, “Data Structure: A Pseudo code approach with C++”, Cengage Learning. • https://www.geeksforgeeks.org/linked-list-in-java/ • https://prepinsta.com/java-program/linked-list/ • Animation Video: https://www.youtube.com/watch?v=iNUS9iZwrVA&ab_channel=TechnologyPremiere
  • 45. Data Structures – Unit 1 Ms. K. D. Patil Department of Information Technology Thank You!!! Happy Learning!!!