SlideShare a Scribd company logo
1 of 32
Data Structure-I
SYBSc
Prof. Priyanka Bharate
Objectives
• Describe linked structures
• Compare linked structures to array-
based structures
• Explore the techniques for managing a
linked list
• Discuss the need for a separate node
class to form linked structures
Array Limitations
• What are the limitations of an array, as
a data structure?
• Fixed size
• Physically stored in consecutive memory
locations
• To insert or delete items, may need to shift
data
Linked Data Structures
• A linked data structure consists of
items that are linked to other items
• How? each item points to another item
• Singly linked list: each item points to
the next item
• Doubly linked list: each item points to
the next item and to the previous item
Conceptual Diagram of a Singly-
Linked List
front
Advantages of Linked Lists
• The items do not have to be stored in
consecutive memory locations: the
successor can be anywhere physically
• So, can insert and delete items without
shifting data
• Can increase the size of the data structure
easily
• Linked lists can grow dynamically (i.e.
at run time) – the amount of memory
space allocated can grow and shrink as
needed
Nodes
• A linked list is an ordered sequence of items
called nodes
• A node is the basic unit of representation in a
linked list
• A node in a singly linked list consists of two
fields:
• A data portion
• A link (pointer) to the next node in the structure
• The first item (node) in the linked list is
accessed via a front or head pointer
• The linked list is defined by its head (this is its
starting point)
Singly Linked List
datadatadata .
head
these are nodes
head pointer "defines" the linked list
(note that it is not a node)
Linked List
Note: we will hereafter refer to a singly linked list just as
a “linked list”
• Traversing the linked list
• How is the first item accessed?
• The second?
• The last?
• What does the last item point to?
• We call this the null link
Discussion
• How do we get to an item’s successor?
• How do we get to an item’s
predecessor?
• How do we access, say, the 3rd item in
the linked list?
• How does this differ from an array?
Linked List Operations
We will now examine linked list operations:
• Add an item to the linked list
• We have 3 situations to consider:
• insert a node at the front
• insert a node in the middle
• insert a node at the end
• Delete an item from the linked list
• We have 3 situations to consider:
• delete the node at the front
• delete an interior node
• delete the last node
Inserting a Node at the Front
front
node
node points to the new node to be
inserted, front points to the first node
of the linked list
front
node
1. Make the new node point to the
first node (i.e. the node that front
points to)
front
node
2. Make front point to the new node
(i.e the node that node points to)
Inserting a Node in the Middle
front
node
Let's insert the new node after the third
node in the linked list
front
node
1. Locate the node preceding the
insertion point , since it will have to be
modified (make current point to it)
current
insertion point
front
node
2. Make the new node point to the node
after the insertion point (i.e. the node
pointed to by the node that current
points to)
current
front
node
3. Make the node pointed to by current
point to the new node
current
X
Discussion
• Inserting a node at the front is a special
case; why?
• Is inserting a node at the end a special
case?
Deleting the First Node
front
front points to the first node in the linked list,
which points to the second node
front
Make front point to the second node (i.e. the node
pointed to by the first node)
X
Deleting an Interior Nodefront
1. Traverse the linked list so that current points to the node to be
deleted and previous points to the node prior to the one to be
deleted
previous current
front
2. We need to get at the node following the one to be
deleted (i.e. the node pointed to by the node that current
points to)
previous current
front
3. Make the node that previous points to, point to the
node following the one to be deleted
previous current
X
Discussion
• Deleting the node at the front is a
special case; why?
• Is deleting the last node a special case?
References As Links
• Recall that in Java, a reference variable
contains a reference or pointer to an
object
• We can show a reference variable obj
as pointing to an object:
obj
• A linked structure uses references to
link one object to another
Implementation of Linked List
• In Java, a linked list is a list of node
objects, each of which consists of two
references:
• A reference to the data object
• A reference to the next node object
• The head pointer is the reference to
the linked list, i.e. to the first node
object in the linked list
• The last node has the null value as its
reference to the “next” node object
Linked List of Node Objects
.
head
linked list object
these are node
objects
these are the data objects
Node Objects
• For our linked list implementations, we
will define a class called LinearNode to
represent a node
• It will be defined for the generic type T
• Why is it a good idea to have separate
node class?
• Note that it is called “LinearNode” to avoid
confusion with a different class that will define
nodes for non-linear structures later
The LinearNode Class
• Attributes (instance variables):
• element: a reference to the data object
• next : a reference to the next node
• so it will be of type LinearNode
element next
data object
The LinearNode Class
• Methods: we only need
• Getters
• Setters
public class LinearNode<T>
{
private LinearNode<T> next;
private T element;
public LinearNode( ){
next = null;
element = null;
}
public LinearNode (T elem){
next = null;
element = elem;
} // cont’d..
LinearNode.java
public LinearNode<T> getNext( ){
return next;
}
public void setNext (LinearNode<T> node){
next = node;
}
public T getElement( ){
return element;
}
public void setElement (T elem) {
element = elem;
}
}
LinearNode.java
(cont’d)
Example: Create a LinearNode
Object
• Example: create a node that contains the
integer 7
Integer intObj = new Integer(7);
LinearNode<Integer> inode =
new LinearNode<Integer> (intObj);
or
LinearNode<Integer> inode =
new LinearNode<Integer> (new Integer(7));
Exercise: Build a Linked List
• Exercise: create a linked list that
contains the integers 1, 2, 3, …, 10
Doubly Linked Lists
• In a doubly linked list, each node has
two links:
• A reference to the next node in the list
• A reference to the previous node in the
list
• What is the “previous” reference of the
first node in the list?
• What is the advantage of a doubly
linked list?
• What is a disadvantage?
Doubly Linked List
.
head
.
tail

More Related Content

Similar to Sy ds i

LinkedList Presentation.pptx
LinkedList Presentation.pptxLinkedList Presentation.pptx
LinkedList Presentation.pptxwahid431192
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxAhmedEldesoky24
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfKamranAli649587
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxSKUP1
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxLECO9
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked listLavanyaJ28
 
Remove Duplicates in an Unsorted Linked List in Python
Remove Duplicates in an Unsorted Linked List in PythonRemove Duplicates in an Unsorted Linked List in Python
Remove Duplicates in an Unsorted Linked List in PythonKal Bartal
 
1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptxBlueSwede
 
Data Structures(Part 1)
Data Structures(Part 1)Data Structures(Part 1)
Data Structures(Part 1)SURBHI SAROHA
 

Similar to Sy ds i (20)

unit 1.pptx
unit 1.pptxunit 1.pptx
unit 1.pptx
 
Linked list
Linked listLinked list
Linked list
 
lecture 02.2.ppt
lecture 02.2.pptlecture 02.2.ppt
lecture 02.2.ppt
 
LinkedList Presentation.pptx
LinkedList Presentation.pptxLinkedList Presentation.pptx
LinkedList Presentation.pptx
 
ds bridge.pptx
ds bridge.pptxds bridge.pptx
ds bridge.pptx
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
 
2- link-list.ppt
2- link-list.ppt2- link-list.ppt
2- link-list.ppt
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Data structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptxData structures and Algorithm analysis_Lecture 2.pptx
Data structures and Algorithm analysis_Lecture 2.pptx
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Linked list ppt
Linked list pptLinked list ppt
Linked list ppt
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
 
Remove Duplicates in an Unsorted Linked List in Python
Remove Duplicates in an Unsorted Linked List in PythonRemove Duplicates in an Unsorted Linked List in Python
Remove Duplicates in an Unsorted Linked List in Python
 
1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx1.Introduction to Data Structures and Algorithms.pptx
1.Introduction to Data Structures and Algorithms.pptx
 
Data Structures(Part 1)
Data Structures(Part 1)Data Structures(Part 1)
Data Structures(Part 1)
 

Recently uploaded

Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |aasikanpl
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPirithiRaju
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxpriyankatabhane
 
FREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naFREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naJASISJULIANOELYNV
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxNandakishor Bhaurao Deshmukh
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2John Carlo Rollon
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxBREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxPABOLU TEJASREE
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantadityabhardwaj282
 
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRlizamodels9
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024AyushiRastogi48
 

Recently uploaded (20)

Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
Call Us ≽ 9953322196 ≼ Call Girls In Lajpat Nagar (Delhi) |
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptxMicrophone- characteristics,carbon microphone, dynamic microphone.pptx
Microphone- characteristics,carbon microphone, dynamic microphone.pptx
 
FREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naFREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by na
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
 
Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2Evidences of Evolution General Biology 2
Evidences of Evolution General Biology 2
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort ServiceHot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxBREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are important
 
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024
 

Sy ds i

  • 2. Objectives • Describe linked structures • Compare linked structures to array- based structures • Explore the techniques for managing a linked list • Discuss the need for a separate node class to form linked structures
  • 3. Array Limitations • What are the limitations of an array, as a data structure? • Fixed size • Physically stored in consecutive memory locations • To insert or delete items, may need to shift data
  • 4. Linked Data Structures • A linked data structure consists of items that are linked to other items • How? each item points to another item • Singly linked list: each item points to the next item • Doubly linked list: each item points to the next item and to the previous item
  • 5. Conceptual Diagram of a Singly- Linked List front
  • 6. Advantages of Linked Lists • The items do not have to be stored in consecutive memory locations: the successor can be anywhere physically • So, can insert and delete items without shifting data • Can increase the size of the data structure easily • Linked lists can grow dynamically (i.e. at run time) – the amount of memory space allocated can grow and shrink as needed
  • 7. Nodes • A linked list is an ordered sequence of items called nodes • A node is the basic unit of representation in a linked list • A node in a singly linked list consists of two fields: • A data portion • A link (pointer) to the next node in the structure • The first item (node) in the linked list is accessed via a front or head pointer • The linked list is defined by its head (this is its starting point)
  • 8. Singly Linked List datadatadata . head these are nodes head pointer "defines" the linked list (note that it is not a node)
  • 9. Linked List Note: we will hereafter refer to a singly linked list just as a “linked list” • Traversing the linked list • How is the first item accessed? • The second? • The last? • What does the last item point to? • We call this the null link
  • 10. Discussion • How do we get to an item’s successor? • How do we get to an item’s predecessor? • How do we access, say, the 3rd item in the linked list? • How does this differ from an array?
  • 11. Linked List Operations We will now examine linked list operations: • Add an item to the linked list • We have 3 situations to consider: • insert a node at the front • insert a node in the middle • insert a node at the end • Delete an item from the linked list • We have 3 situations to consider: • delete the node at the front • delete an interior node • delete the last node
  • 12. Inserting a Node at the Front front node node points to the new node to be inserted, front points to the first node of the linked list front node 1. Make the new node point to the first node (i.e. the node that front points to)
  • 13. front node 2. Make front point to the new node (i.e the node that node points to)
  • 14. Inserting a Node in the Middle front node Let's insert the new node after the third node in the linked list front node 1. Locate the node preceding the insertion point , since it will have to be modified (make current point to it) current insertion point
  • 15. front node 2. Make the new node point to the node after the insertion point (i.e. the node pointed to by the node that current points to) current front node 3. Make the node pointed to by current point to the new node current X
  • 16. Discussion • Inserting a node at the front is a special case; why? • Is inserting a node at the end a special case?
  • 17. Deleting the First Node front front points to the first node in the linked list, which points to the second node front Make front point to the second node (i.e. the node pointed to by the first node) X
  • 18. Deleting an Interior Nodefront 1. Traverse the linked list so that current points to the node to be deleted and previous points to the node prior to the one to be deleted previous current front 2. We need to get at the node following the one to be deleted (i.e. the node pointed to by the node that current points to) previous current
  • 19. front 3. Make the node that previous points to, point to the node following the one to be deleted previous current X
  • 20. Discussion • Deleting the node at the front is a special case; why? • Is deleting the last node a special case?
  • 21. References As Links • Recall that in Java, a reference variable contains a reference or pointer to an object • We can show a reference variable obj as pointing to an object: obj • A linked structure uses references to link one object to another
  • 22. Implementation of Linked List • In Java, a linked list is a list of node objects, each of which consists of two references: • A reference to the data object • A reference to the next node object • The head pointer is the reference to the linked list, i.e. to the first node object in the linked list • The last node has the null value as its reference to the “next” node object
  • 23. Linked List of Node Objects . head linked list object these are node objects these are the data objects
  • 24. Node Objects • For our linked list implementations, we will define a class called LinearNode to represent a node • It will be defined for the generic type T • Why is it a good idea to have separate node class? • Note that it is called “LinearNode” to avoid confusion with a different class that will define nodes for non-linear structures later
  • 25. The LinearNode Class • Attributes (instance variables): • element: a reference to the data object • next : a reference to the next node • so it will be of type LinearNode element next data object
  • 26. The LinearNode Class • Methods: we only need • Getters • Setters
  • 27. public class LinearNode<T> { private LinearNode<T> next; private T element; public LinearNode( ){ next = null; element = null; } public LinearNode (T elem){ next = null; element = elem; } // cont’d.. LinearNode.java
  • 28. public LinearNode<T> getNext( ){ return next; } public void setNext (LinearNode<T> node){ next = node; } public T getElement( ){ return element; } public void setElement (T elem) { element = elem; } } LinearNode.java (cont’d)
  • 29. Example: Create a LinearNode Object • Example: create a node that contains the integer 7 Integer intObj = new Integer(7); LinearNode<Integer> inode = new LinearNode<Integer> (intObj); or LinearNode<Integer> inode = new LinearNode<Integer> (new Integer(7));
  • 30. Exercise: Build a Linked List • Exercise: create a linked list that contains the integers 1, 2, 3, …, 10
  • 31. Doubly Linked Lists • In a doubly linked list, each node has two links: • A reference to the next node in the list • A reference to the previous node in the list • What is the “previous” reference of the first node in the list? • What is the advantage of a doubly linked list? • What is a disadvantage?