SlideShare a Scribd company logo
1
SINGLY LINKED LIST
SUBMITTED BY:- MD IMRAN ANSARI
SUBMITTED TO:- SOHIT SIR
•DSA LAB ROJECT 2ND
2
SINGLY LINKED LISTS
 What is a singly-linked list?
 Why linked lists?
 Representation
 Space Analysis
 Creation, Append and Prepend
 Traversal
 Search
 Insertion after and before an element
 Deletion
3
WHAT IS A SINGLY-LINKED LIST?
.
A singly linked list is a dynamic data structure consisting of a sequence of nodes, forming a
linear ordering
Each node stores:
 Element (data object)
 Reference (i.e., address) to the next node
Node:
Singly-linked list:
4
WHY LINKED LISTS?
Linked lists are used to implement many important data structures such as
stacks, queues, graphs, hash tables, etc.
Linked lists are used as components of some data structures. Examples:
B+ trees, skip lists, etc.
LISP An important programming language in artificial intelligence makes
extensive use of linked lists in performing symbolic processing.
Memory management: An important role of operating systems. An
operating system must decide how to allocate and reclaim storage for
processes running on the system. A linked list can be used to keep track
of portions of memory that are available for allocation.
Scrolled lists, components found in graphical user interfaces, can be
implemented using linked lists.
5
REPRESENTATION
We are using a representation in which a linked list has both head and tail references:
public class MyLinkedList{
protected Element head;
protected Element tail;
public final class Element{
Object data;
Element next;
Element(Object obj, Element element){
data = obj;
next = element;
}
public Object getData(){return data;}
public Element getNext(){return next;}
}
// . . .
}
6
TRAVERSAL
Begin at the first node, then follow each next reference until the
traversal condition is satisfied or until you come to the end.
To move an Element reference e from one node to the next use:
Example: Count the number of nodes in a linked list.
public int countNodes(){
int count = 0;
Element e = head;
while(e != null){
count++;
e = e.next;
}
return count;
}
e = e.next;
O(n(
7
SEARCHING
To search for an element, we traverse from head until we locate the object or we reach
the end of the list.
Example: Count the number of nodes with data field equal to a given object.
public int countNodes(Object obj){
int count = 0;
Element e = head;
while(e != null){
if(e.data.equals(obj))
count++;
e = e.next;
}
return count;
}
Complexity is….
• The following reference relationships are useful in searching:
e .n e x t e .n e x t .n e x t e .n e x t .n e x t .n e x t
e .n e x t .d a t a e .n e x t .n e x t .d a t a
e
e .d a t a
However, it is important to ensure that next is not null in such expressions
8
INSERTION AFTER AN ELEMENT
• To insert an object y after a node x:
• Move a reference e from the beginning of the list to node x:
Element e = head;
if(e == null) throw new IllegalArgumentException(“not found”);
while(e != null && !e.data.equals(x)){
e = e.next;
}
if(e == null) throw new IllegalArgumentException(“not found”);
• Create a new node containing y as data and let its next reference refer to the
node after node x:
Element element = new Element(y, e.next);
• Make the next reference of node x refer to node y:
e.next = element;
• If the new node was inserted at the end of the list, update the tail reference:
if(element.next == null) tail = element;
e
e .n e x t
x
e
e .n e x t
x
y
e l e m e n t
e
e .n e x t
x
y
e l e m e n t
9
INSERTION AFTER AN ELEMENT
public void insertAfter(Object obj) {
// create a new node for obj2 and make it refer to the node
// after obj1 node
Element element = new Element(obj, this.next);
// make obj1 node refer to the new node
this.next = element;
// update tail if the new node was inserted at the end
if(this == tail)
tail = next;
}
O(1(
O(n(
• The insertAfter method of the Element class is invoked as:
MyLinkedList.Element e = list.find(obj1);
if(e != null)
e.insertAfter(obj2); // insert obj2 after obj1
else
System.out.println("Element to insert before not found");
• Within the insertAfter method this refers to obj1 node:
• Note: The total complexity of the insert after operation is O(n) because find is O(n)
10
INSERTION BEFORE AN ELEMENT
• To insert an object y before a node x:
• Move a reference previous from the beginning of the list to the node before
node x:
Element e = head, previous;
if(e == null) throw new IllegalArgumentException(“not found”);
while(e != null && ! e.data.equals(x)){
previous = e;
e = e.next;
}
if(e == null) throw new IllegalArgumentException(“not found”);
• Create a new node containing y as data and let its next reference refer to the
node x:
Element element = new Element(y, e);
• Make the next reference of the node before node x refer to node y:
if(e == head)
head = element;
else
previous.next = element;
e
x
p r e v io u s
y
e l e m e n t
e
x
p r e v io u s
y
e l e m e n t
e
x
p r e v io u s
11
INSERTION BEFORE AN ELEMENT
public void insertBefore(Object obj) {
// create a new node for obj2, make this node point to obj1 node
Element element = new Element(obj, this);
if(this == head){
head = element;
return;
}
Element previous = head;
// move previous to node before obj1 node
while(previous.next != this) {
previous = previous.next;
}
previous.next = element; // insert
}
MyLinkedList.Element e = list.find(obj1);
if(e != null)
e.insertBefore(obj2); // insert obj2 before obj1
else
System.out.println("Element to insert before not found");
• The insertBefore method of the Element class is invoked as:
• Within the insertBefore method this refers to obj1 node:
O(n(
12
DELETION
• To delete a node x:
• Move a reference previous from the beginning of the list to the node before
node x:
Element e = head, previous;
if(e == null) throw new IllegalArgumentException(“not found”);
while(e != null && ! e.data.equals(x)){
previous = e;
e = e.next;
}
if(e == null) throw new IllegalArgumentException(“not found”);
• Bypass the node to be deleted:
if(e == head){
if(head.next == null)
head = tail = e = null;
else{
head = head.next;
}
else{
previous.next = e.next;
if(tail == e)
tail = previous;
}
e = null;
e
x
p r e v i o u s
e
x
p r e v io u s
13
DELETION – DELETING FIRST AND LAST ELEMENT
public void extractFirst() {
if(head == null)
throw new IllegalArgumentException("item not found");
head = head.next;
if(head == null)
tail = null;
}
public void extractLast() {
if(tail == null)
throw new IllegalArgumentException("item not found");
if (head == tail)
head = tail = null;
else {
Element previous = head;
while(previous.next != tail)
previous = previous.next;
previous.next = null;
tail = previous;
}
}
14
public void extract(Object obj) {
Element element = head;
Element previous = null;
while(element != null && ! element.data.equals(obj)) {
previous = element;
element = element.next;
}
if(element == null)
throw new IllegalArgumentException("item not found");
if(element == head)
head = element.next;
else
previous.next = element.next;
if(element == tail)
tail = previous;
}
DELETION OF AN ARBITRARY ELEMENT
To delete an element, we use either the extract method of
MyLinkedList or that of the Element inner class.
The MyLinkedList extract method (code similar to that in slide 16):
try{
list.extract(obj1);
} catch(IllegalArgumentException e){
System.out.println("Element not found");
}
• The method is invoked as:
15
public void extract() {
Element element = null;
if(this == head)
head = next;
else{
element = head;
while(element != null && element.next != this){
element = element.next;
}
if(element == null)
throw new InvalidOperationException(“Not found”);
element.next = next;
}
if(this == tail)
tail = element;
}
DELETION OF AN ARBITRARY ELEMENT
The Element extract method invocation and implementation:
MyLinkedList.Element e = list.find(obj1);
if(e != null)
e.extract();
else
System.out.println("Element not found");
16
TIME COMPLEXITY: SINGLY-LINKED LISTS VS. 1D-ARRAYS
Operation ID-Array Complexity Singly-linked list Complexity
Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference
Insert at middle* O(n) O(n)
Delete at beginning O(n) O(1)
Delete at end O(1) O(n)
Delete at middle* O(n):
O(1) access followed by O(n)
shift
O(n):
O(n) search, followed by O(1) delete
Search O(n) linear search
O(log n) Binary search
O(n)
Indexing: What is
the element at a
given position k?
O(1) O(n)
* middle: neither at the beginning nor at the end
17
EXERCISES
Using the Element extract method is less efficient than using the
MyLinkedList extract method. Why?
For the MyLinkedList class, Implement each of the following methods:
String toString()
Element find(Object obj)
void insertAt(int n) //counting the nodes from 1.
void deleteBefore(Object obj) // delete node before obj node
State the complexity of each method.
Which methods are affected if we do not use the tail reference in
MyLinkedList class.

More Related Content

What's hot

Java collections
Java collectionsJava collections
Java collections
Amar Kutwal
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handling
Hariz Mustafa
 
Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2
Hariz Mustafa
 

What's hot (20)

Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
07 java collection
07 java collection07 java collection
07 java collection
 
java collections
java collectionsjava collections
java collections
 
Array list
Array listArray list
Array list
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Java collections
Java collectionsJava collections
Java collections
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
L11 array list
L11 array listL11 array list
L11 array list
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handling
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2Lecture06 methods for-making_data_structures_v2
Lecture06 methods for-making_data_structures_v2
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Java ArrayList Video Tutorial
Java ArrayList Video TutorialJava ArrayList Video Tutorial
Java ArrayList Video Tutorial
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 

Similar to singly link list project in dsa.....by rohit malav

Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
ssuser7319f8
 
Objective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdfObjective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdf
advancethchnologies
 
Objective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdfObjective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdf
giriraj65
 
Linked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdfLinked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdf
adityacomputers001
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
Getachew Ganfur
 
Please and Thank youObjective The purpose of this exercise is to .pdf
Please and Thank youObjective The purpose of this exercise is to .pdfPlease and Thank youObjective The purpose of this exercise is to .pdf
Please and Thank youObjective The purpose of this exercise is to .pdf
alicesilverblr
 
Objective The purpose of this exercise is to create a Linked List d.pdf
Objective The purpose of this exercise is to create a Linked List d.pdfObjective The purpose of this exercise is to create a Linked List d.pdf
Objective The purpose of this exercise is to create a Linked List d.pdf
aliracreations
 

Similar to singly link list project in dsa.....by rohit malav (20)

03_LinkedLists_091.ppt
03_LinkedLists_091.ppt03_LinkedLists_091.ppt
03_LinkedLists_091.ppt
 
1.ppt
1.ppt1.ppt
1.ppt
 
Linked list
Linked list Linked list
Linked list
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Objective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdfObjective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdf
 
Objective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdfObjective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdf
 
Linked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdfLinked List Objective The purpose of this exercise is to cr.pdf
Linked List Objective The purpose of this exercise is to cr.pdf
 
Linked List
Linked ListLinked List
Linked List
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Linked list and its operations - Traversal
Linked list and its operations - TraversalLinked list and its operations - Traversal
Linked list and its operations - Traversal
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Ds notes
Ds notesDs notes
Ds notes
 
Data structure
Data  structureData  structure
Data structure
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02Fundamentalsofdatastructures 110501104205-phpapp02
Fundamentalsofdatastructures 110501104205-phpapp02
 
Please and Thank youObjective The purpose of this exercise is to .pdf
Please and Thank youObjective The purpose of this exercise is to .pdfPlease and Thank youObjective The purpose of this exercise is to .pdf
Please and Thank youObjective The purpose of this exercise is to .pdf
 
Objective The purpose of this exercise is to create a Linked List d.pdf
Objective The purpose of this exercise is to create a Linked List d.pdfObjective The purpose of this exercise is to create a Linked List d.pdf
Objective The purpose of this exercise is to create a Linked List d.pdf
 

More from Rohit malav

More from Rohit malav (20)

Aca lab project (rohit malav)
Aca lab project (rohit malav) Aca lab project (rohit malav)
Aca lab project (rohit malav)
 
operating system calls input and output by (rohit malav)
operating system calls input and output by (rohit malav)operating system calls input and output by (rohit malav)
operating system calls input and output by (rohit malav)
 
Python pandas liberary
Python pandas liberaryPython pandas liberary
Python pandas liberary
 
Presentation by purshotam verma
Presentation by purshotam vermaPresentation by purshotam verma
Presentation by purshotam verma
 
Deep learning in python by purshottam verma
Deep learning in python by purshottam vermaDeep learning in python by purshottam verma
Deep learning in python by purshottam verma
 
Atm Security System Using Steganography Nss ptt by (rohit malav)
Atm Security System Using  Steganography Nss ptt by (rohit malav)Atm Security System Using  Steganography Nss ptt by (rohit malav)
Atm Security System Using Steganography Nss ptt by (rohit malav)
 
Samba server Pts report pdf by Rohit malav
Samba server Pts report pdf by Rohit malavSamba server Pts report pdf by Rohit malav
Samba server Pts report pdf by Rohit malav
 
System calls operating system ppt by rohit malav
System calls operating system  ppt by rohit malavSystem calls operating system  ppt by rohit malav
System calls operating system ppt by rohit malav
 
A project on spring framework by rohit malav
A project on spring framework by rohit malavA project on spring framework by rohit malav
A project on spring framework by rohit malav
 
android text encryption Network security lab by rohit malav
android text encryption Network security lab by rohit malavandroid text encryption Network security lab by rohit malav
android text encryption Network security lab by rohit malav
 
samba server setup Pts ppt (rohit malav)
samba server setup Pts ppt (rohit malav)samba server setup Pts ppt (rohit malav)
samba server setup Pts ppt (rohit malav)
 
Spring frame work by rohit malav(detailed)
Spring frame work by rohit malav(detailed)Spring frame work by rohit malav(detailed)
Spring frame work by rohit malav(detailed)
 
spring framework ppt by Rohit malav
spring framework ppt by Rohit malavspring framework ppt by Rohit malav
spring framework ppt by Rohit malav
 
Samba server linux (SMB) BY ROHIT MALAV
Samba server linux (SMB) BY ROHIT MALAVSamba server linux (SMB) BY ROHIT MALAV
Samba server linux (SMB) BY ROHIT MALAV
 
Payroll system ppt1 (rohit malav)
Payroll system ppt1 (rohit malav)Payroll system ppt1 (rohit malav)
Payroll system ppt1 (rohit malav)
 
Payroll system ppt2 (rohit malav) version point 2
Payroll system ppt2 (rohit malav) version point 2Payroll system ppt2 (rohit malav) version point 2
Payroll system ppt2 (rohit malav) version point 2
 
ONLINE STUDENT MANAGEMENT SYSTEM
ONLINE STUDENT MANAGEMENT SYSTEMONLINE STUDENT MANAGEMENT SYSTEM
ONLINE STUDENT MANAGEMENT SYSTEM
 
digital unlock power point slide
digital unlock power point slidedigital unlock power point slide
digital unlock power point slide
 
Rohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan viharRohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan vihar
 
Snake report ROHIT MALAV
Snake report ROHIT MALAVSnake report ROHIT MALAV
Snake report ROHIT MALAV
 

Recently uploaded

LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 

Recently uploaded (20)

LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
Hall booking system project report .pdf
Hall booking system project report  .pdfHall booking system project report  .pdf
Hall booking system project report .pdf
 
Online resume builder management system project report.pdf
Online resume builder management system project report.pdfOnline resume builder management system project report.pdf
Online resume builder management system project report.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 

singly link list project in dsa.....by rohit malav

  • 1. 1 SINGLY LINKED LIST SUBMITTED BY:- MD IMRAN ANSARI SUBMITTED TO:- SOHIT SIR •DSA LAB ROJECT 2ND
  • 2. 2 SINGLY LINKED LISTS  What is a singly-linked list?  Why linked lists?  Representation  Space Analysis  Creation, Append and Prepend  Traversal  Search  Insertion after and before an element  Deletion
  • 3. 3 WHAT IS A SINGLY-LINKED LIST? . A singly linked list is a dynamic data structure consisting of a sequence of nodes, forming a linear ordering Each node stores:  Element (data object)  Reference (i.e., address) to the next node Node: Singly-linked list:
  • 4. 4 WHY LINKED LISTS? Linked lists are used to implement many important data structures such as stacks, queues, graphs, hash tables, etc. Linked lists are used as components of some data structures. Examples: B+ trees, skip lists, etc. LISP An important programming language in artificial intelligence makes extensive use of linked lists in performing symbolic processing. Memory management: An important role of operating systems. An operating system must decide how to allocate and reclaim storage for processes running on the system. A linked list can be used to keep track of portions of memory that are available for allocation. Scrolled lists, components found in graphical user interfaces, can be implemented using linked lists.
  • 5. 5 REPRESENTATION We are using a representation in which a linked list has both head and tail references: public class MyLinkedList{ protected Element head; protected Element tail; public final class Element{ Object data; Element next; Element(Object obj, Element element){ data = obj; next = element; } public Object getData(){return data;} public Element getNext(){return next;} } // . . . }
  • 6. 6 TRAVERSAL Begin at the first node, then follow each next reference until the traversal condition is satisfied or until you come to the end. To move an Element reference e from one node to the next use: Example: Count the number of nodes in a linked list. public int countNodes(){ int count = 0; Element e = head; while(e != null){ count++; e = e.next; } return count; } e = e.next; O(n(
  • 7. 7 SEARCHING To search for an element, we traverse from head until we locate the object or we reach the end of the list. Example: Count the number of nodes with data field equal to a given object. public int countNodes(Object obj){ int count = 0; Element e = head; while(e != null){ if(e.data.equals(obj)) count++; e = e.next; } return count; } Complexity is…. • The following reference relationships are useful in searching: e .n e x t e .n e x t .n e x t e .n e x t .n e x t .n e x t e .n e x t .d a t a e .n e x t .n e x t .d a t a e e .d a t a However, it is important to ensure that next is not null in such expressions
  • 8. 8 INSERTION AFTER AN ELEMENT • To insert an object y after a node x: • Move a reference e from the beginning of the list to node x: Element e = head; if(e == null) throw new IllegalArgumentException(“not found”); while(e != null && !e.data.equals(x)){ e = e.next; } if(e == null) throw new IllegalArgumentException(“not found”); • Create a new node containing y as data and let its next reference refer to the node after node x: Element element = new Element(y, e.next); • Make the next reference of node x refer to node y: e.next = element; • If the new node was inserted at the end of the list, update the tail reference: if(element.next == null) tail = element; e e .n e x t x e e .n e x t x y e l e m e n t e e .n e x t x y e l e m e n t
  • 9. 9 INSERTION AFTER AN ELEMENT public void insertAfter(Object obj) { // create a new node for obj2 and make it refer to the node // after obj1 node Element element = new Element(obj, this.next); // make obj1 node refer to the new node this.next = element; // update tail if the new node was inserted at the end if(this == tail) tail = next; } O(1( O(n( • The insertAfter method of the Element class is invoked as: MyLinkedList.Element e = list.find(obj1); if(e != null) e.insertAfter(obj2); // insert obj2 after obj1 else System.out.println("Element to insert before not found"); • Within the insertAfter method this refers to obj1 node: • Note: The total complexity of the insert after operation is O(n) because find is O(n)
  • 10. 10 INSERTION BEFORE AN ELEMENT • To insert an object y before a node x: • Move a reference previous from the beginning of the list to the node before node x: Element e = head, previous; if(e == null) throw new IllegalArgumentException(“not found”); while(e != null && ! e.data.equals(x)){ previous = e; e = e.next; } if(e == null) throw new IllegalArgumentException(“not found”); • Create a new node containing y as data and let its next reference refer to the node x: Element element = new Element(y, e); • Make the next reference of the node before node x refer to node y: if(e == head) head = element; else previous.next = element; e x p r e v io u s y e l e m e n t e x p r e v io u s y e l e m e n t e x p r e v io u s
  • 11. 11 INSERTION BEFORE AN ELEMENT public void insertBefore(Object obj) { // create a new node for obj2, make this node point to obj1 node Element element = new Element(obj, this); if(this == head){ head = element; return; } Element previous = head; // move previous to node before obj1 node while(previous.next != this) { previous = previous.next; } previous.next = element; // insert } MyLinkedList.Element e = list.find(obj1); if(e != null) e.insertBefore(obj2); // insert obj2 before obj1 else System.out.println("Element to insert before not found"); • The insertBefore method of the Element class is invoked as: • Within the insertBefore method this refers to obj1 node: O(n(
  • 12. 12 DELETION • To delete a node x: • Move a reference previous from the beginning of the list to the node before node x: Element e = head, previous; if(e == null) throw new IllegalArgumentException(“not found”); while(e != null && ! e.data.equals(x)){ previous = e; e = e.next; } if(e == null) throw new IllegalArgumentException(“not found”); • Bypass the node to be deleted: if(e == head){ if(head.next == null) head = tail = e = null; else{ head = head.next; } else{ previous.next = e.next; if(tail == e) tail = previous; } e = null; e x p r e v i o u s e x p r e v io u s
  • 13. 13 DELETION – DELETING FIRST AND LAST ELEMENT public void extractFirst() { if(head == null) throw new IllegalArgumentException("item not found"); head = head.next; if(head == null) tail = null; } public void extractLast() { if(tail == null) throw new IllegalArgumentException("item not found"); if (head == tail) head = tail = null; else { Element previous = head; while(previous.next != tail) previous = previous.next; previous.next = null; tail = previous; } }
  • 14. 14 public void extract(Object obj) { Element element = head; Element previous = null; while(element != null && ! element.data.equals(obj)) { previous = element; element = element.next; } if(element == null) throw new IllegalArgumentException("item not found"); if(element == head) head = element.next; else previous.next = element.next; if(element == tail) tail = previous; } DELETION OF AN ARBITRARY ELEMENT To delete an element, we use either the extract method of MyLinkedList or that of the Element inner class. The MyLinkedList extract method (code similar to that in slide 16): try{ list.extract(obj1); } catch(IllegalArgumentException e){ System.out.println("Element not found"); } • The method is invoked as:
  • 15. 15 public void extract() { Element element = null; if(this == head) head = next; else{ element = head; while(element != null && element.next != this){ element = element.next; } if(element == null) throw new InvalidOperationException(“Not found”); element.next = next; } if(this == tail) tail = element; } DELETION OF AN ARBITRARY ELEMENT The Element extract method invocation and implementation: MyLinkedList.Element e = list.find(obj1); if(e != null) e.extract(); else System.out.println("Element not found");
  • 16. 16 TIME COMPLEXITY: SINGLY-LINKED LISTS VS. 1D-ARRAYS Operation ID-Array Complexity Singly-linked list Complexity Insert at beginning O(n) O(1) Insert at end O(1) O(1) if the list has tail reference O(n) if the list has no tail reference Insert at middle* O(n) O(n) Delete at beginning O(n) O(1) Delete at end O(1) O(n) Delete at middle* O(n): O(1) access followed by O(n) shift O(n): O(n) search, followed by O(1) delete Search O(n) linear search O(log n) Binary search O(n) Indexing: What is the element at a given position k? O(1) O(n) * middle: neither at the beginning nor at the end
  • 17. 17 EXERCISES Using the Element extract method is less efficient than using the MyLinkedList extract method. Why? For the MyLinkedList class, Implement each of the following methods: String toString() Element find(Object obj) void insertAt(int n) //counting the nodes from 1. void deleteBefore(Object obj) // delete node before obj node State the complexity of each method. Which methods are affected if we do not use the tail reference in MyLinkedList class.

Editor's Notes

  1. O(n)
  2. extractFirst Complexity is O(1) extractLast Complexity is O(n)
  3. O(n)
  4. O(n)
  5. O(n)