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!!!