SlideShare a Scribd company logo
1 of 38
Download to read offline
Lesson 5 – Linked List
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
A Simple Linked List
Our first example program, linkList.java,
demonstrates a simple linked list. The only
operations allowed in this version of a list
are:
 Inserting an item at the beginning of the list
 Deleting the item at the beginning of the list
 Iterating through the list to display its contents
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2
The Link Class
You’ve already seen the data part of the Link
class. Here’s the complete class definition:
The linkList.java Program
Listing 5.1 shows the complete linkList.java program. You’ve already seen all the components
except the main() routine.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
The linkList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 4
Output:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 5
In main() we create a new list, insert four new links into it with insertFirst(), and display it. Then,
in the while loop, we remove the items one by one with deleteFirst() until the list is empty. The
empty list is then displayed. Here’s the output from linkList.java:
List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99}
Deleted {88, 8.99}
Deleted {66, 6.99}
Deleted {44, 4.99}
Deleted {22, 2.99}
List (first-->last):
Finding and Deleting Specified Links
Our next example program adds methods to
search a linked list for a data item with a specified
key value and to delete an item with a specified
key value. These, along with insertion at the start
of the list, are the same operations carried out by
the LinkList Workshop applet. The complete
linkList2.java program is shown in Listing 5.2.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
Finding and Deleting Specified Links
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
Finding and Deleting Specified Links
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
Output:
The main() routine makes a list, inserts four items, and displays the resulting list. It then
searches for the item with key 44, deletes the item with key 66, and displays the list again.
Here’s the output:
List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99}
Found link with key 44
Deleted link with key 66
List (first-->last): {88, 8.99} {44, 4.99} {22, 2.99}
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
LISTING 5.3 The firstLastList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
LISTING 5.3 The firstLastList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
LISTING 5.3 The firstLastList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12
For simplicity, in this program we’ve reduced the number of
data items in each link from two to one. This makes it easier to
display the link contents. (Remember that in a serious program
there would be many more data items, or a reference to
another object containing many data items.) This program
inserts three items at the front of the list, inserts three more at
the end, and displays the resulting list. It then deletes the first
two items and displays the list again.
Here’s the output:
List (first-->last): 66 44 22 11 33 55
List (first-->last): 22 11 33 55
LISTING 5.4 The linkStack.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
LISTING 5.4 The linkStack.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
LISTING 5.4 The linkStack.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
Output:
The main() routine creates a stack object, pushes two items on it, displays the stack, pushes two
more items, and displays the stack again. Finally, it pops two items and displays the stack a third
time.
Here’s the output:
Stack (top-->bottom): 40 20
Stack (top-->bottom): 80 60 40 20 Stack (top-->bottom): 40 20
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
The sortedList.java Program
The sortedList.java example shown in Listing 5.6
presents a SortedList class with insert(),
remove(), and displayList() methods. Only the
insert() routine is different from its counterpart
in non-sorted lists.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
The sortedList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 18
The sortedList.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 19
Output:
In main() we insert two items with key values 20 and 40. Then we insert three more
items, with values 10, 30, and 50. These values are inserted at the beginning of the
list, in the middle, and at the end, showing that the insert() routine correctly
handles these special cases. Finally, we remove one item, to show removal is always
from the front of the list. After each change, the list is displayed.
Here’s the output from sortedList.java:
List (first-->last): 20 40
List (first-->last): 10 20 30 40 50
List (first-->last): 20 30 40 50
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
LISTING 5.7 The listInsertionSort.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
LISTING 5.7 The listInsertionSort.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
LISTING 5.7 The listInsertionSort.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23
This program displays the values in the array
before the sorting operation and again
afterward.
Here’s some sample output:
Unsorted array: 59 69 41 56 84 15 86 81 37 35
Sorted array: 15 35 37 41 56 59 69 81 84 86
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 24
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 25
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 26
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 27
The doublyLinked.java Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 28
In main() we insert some items at the beginning of the list
and at the end, display
the items going both forward and backward, delete the
first and last items and the
item with key 11, display the list again (forward only),
insert two items using the
insertAfter() method, and display the list again.
Here’s the output:
List (first-->last): 66 44 22 11 33 55
List (last-->first): 55 33 11 22 44 66
List (first-->last): 44 22 33
List (first-->last): 44 22 77 33 88
Iterator Methods
Additional methods can make the iterator a flexible and powerful class. All operations previously performed by
the class that involve iterating through the list, such as insertAfter(), are more naturally performed by the iterator.
In our example the iterator includes the following methods:
• reset()—Sets the iterator to the start of the list
• nextLink()—Moves the iterator to the next link
• getCurrent()—Returns the link at the iterator
• atEnd()—Returns true if the iterator is at the end of the list
234 CHAPTER 5 Linked Lists
• insertAfter()—Inserts a new link after the iterator
• insertBefore()—Inserts a new link before the iterator
• deleteCurrent()—Deletes the link at the iterator
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 29
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 30
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 31
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 32
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 33
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 34
LISTING 5.9 The interIterator.java
Program
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 35
Output:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 36
The main() routine inserts four items into the list, using an iterator and its insertAfter() method.
Then it waits for the user to interact with it. In the following sample interaction, the user
displays the list, resets the iterator to the beginning, goes forward two links, gets the current
link’s key value (which is 60), inserts 100 before this, inserts 7 after the 100, and displays the list
again:
Summary
A linked list consists of one linkedList object and a number of Link objects.
• The linkedList object contains a reference, often called first, to the first link
in the list.
• Each Link object contains data and a reference, often called next, to the next
link in the list.
• A next value of null signals the end of the list.
• Inserting an item at the beginning of a linked list involves changing the new
link’s next field to point to the old first link and changing first to point to the
new item.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 37
Summary
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 38
Stacks and queues are ADTs. They can be implemented using either arrays or linked lists.
• In a sorted linked list, the links are arranged in order of ascending (or sometimes descending) key
value.
• Insertion in a sorted list takes O(N) time because the correct insertion point must be found.
Deletion of the smallest link takes O(1) time.
• In a doubly linked list, each link contains a reference to the previous link as well as the next link.
• A doubly linked list permits backward traversal and deletion from the end of the list.
• An iterator is a reference, encapsulated in a class object, that points to a link in an associated list.
• Iterator methods allow the user to move the iterator along the list and access the link currently
pointed to.
• An iterator can be used to traverse through a list, performing some operation
on selected links (or all links).

More Related Content

What's hot

C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKushaal Singla
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)Durga Devi
 
Database Management System-session 3-4-5
Database Management System-session 3-4-5Database Management System-session 3-4-5
Database Management System-session 3-4-5Infinity Tech Solutions
 
DocumentationofchangesondocumentparsingofBlueHOUND
DocumentationofchangesondocumentparsingofBlueHOUNDDocumentationofchangesondocumentparsingofBlueHOUND
DocumentationofchangesondocumentparsingofBlueHOUNDXulang Wan
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its FundamentalsHitesh Mohapatra
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3Mahmoud Ouf
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
GSP 125 Final Exam Guide
GSP 125 Final Exam GuideGSP 125 Final Exam Guide
GSP 125 Final Exam Guidecritter13
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structureMahmoud Alfarra
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURESUsha Mahalingam
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#Shahzad
 

What's hot (20)

C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
Database Management System-session 3-4-5
Database Management System-session 3-4-5Database Management System-session 3-4-5
Database Management System-session 3-4-5
 
DocumentationofchangesondocumentparsingofBlueHOUND
DocumentationofchangesondocumentparsingofBlueHOUNDDocumentationofchangesondocumentparsingofBlueHOUND
DocumentationofchangesondocumentparsingofBlueHOUND
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its Fundamentals
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
Java execise
Java execiseJava execise
Java execise
 
Intake 38 data access 3
Intake 38 data access 3Intake 38 data access 3
Intake 38 data access 3
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
GSP 125 Final Exam Guide
GSP 125 Final Exam GuideGSP 125 Final Exam Guide
GSP 125 Final Exam Guide
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structure
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Lab12 dsa bsee20075
Lab12 dsa bsee20075Lab12 dsa bsee20075
Lab12 dsa bsee20075
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
Technical
TechnicalTechnical
Technical
 

Similar to Lesson 5 link list

project2.classpathproject2.project project2 .docx
project2.classpathproject2.project  project2 .docxproject2.classpathproject2.project  project2 .docx
project2.classpathproject2.project project2 .docxbriancrawford30935
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuningAnil Pandey
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
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.pdfadityacomputers001
 
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.pdfadvancethchnologies
 
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.pdfgiriraj65
 
adjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfadjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfakankshasorate1
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Getachew Ganfur
 
Data structures Lecture no. 2
Data structures Lecture no. 2Data structures Lecture no. 2
Data structures Lecture no. 2AzharIqbal710687
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringRAJASEKHARV8
 
Mcq 15-20Q15Which of the following trees are binary search tr
Mcq 15-20Q15Which of the following trees are binary search trMcq 15-20Q15Which of the following trees are binary search tr
Mcq 15-20Q15Which of the following trees are binary search trAbramMartino96
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfarpitaeron555
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructuresNguync91368
 

Similar to Lesson 5 link list (20)

project2.classpathproject2.project project2 .docx
project2.classpathproject2.project  project2 .docxproject2.classpathproject2.project  project2 .docx
project2.classpathproject2.project project2 .docx
 
Application sql issues_and_tuning
Application sql issues_and_tuningApplication sql issues_and_tuning
Application sql issues_and_tuning
 
2 b queues
2 b queues2 b queues
2 b queues
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
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
 
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
 
adjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdfadjava_23_bcs_vision_academy_sachinsir.pdf
adjava_23_bcs_vision_academy_sachinsir.pdf
 
2 a stacks
2 a stacks2 a stacks
2 a stacks
 
Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9Stacks queues-1220971554378778-9
Stacks queues-1220971554378778-9
 
Rana Junaid Rasheed
Rana Junaid RasheedRana Junaid Rasheed
Rana Junaid Rasheed
 
Data structures Lecture no. 2
Data structures Lecture no. 2Data structures Lecture no. 2
Data structures Lecture no. 2
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
Singly & Circular Linked list
Singly & Circular Linked listSingly & Circular Linked list
Singly & Circular Linked list
 
Mcq 15-20Q15Which of the following trees are binary search tr
Mcq 15-20Q15Which of the following trees are binary search trMcq 15-20Q15Which of the following trees are binary search tr
Mcq 15-20Q15Which of the following trees are binary search tr
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
“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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
“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...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

Lesson 5 link list

  • 1. Lesson 5 – Linked List 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
  • 2. A Simple Linked List Our first example program, linkList.java, demonstrates a simple linked list. The only operations allowed in this version of a list are:  Inserting an item at the beginning of the list  Deleting the item at the beginning of the list  Iterating through the list to display its contents 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2 The Link Class You’ve already seen the data part of the Link class. Here’s the complete class definition:
  • 3. The linkList.java Program Listing 5.1 shows the complete linkList.java program. You’ve already seen all the components except the main() routine. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
  • 4. The linkList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 4
  • 5. Output: 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 5 In main() we create a new list, insert four new links into it with insertFirst(), and display it. Then, in the while loop, we remove the items one by one with deleteFirst() until the list is empty. The empty list is then displayed. Here’s the output from linkList.java: List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99} Deleted {88, 8.99} Deleted {66, 6.99} Deleted {44, 4.99} Deleted {22, 2.99} List (first-->last):
  • 6. Finding and Deleting Specified Links Our next example program adds methods to search a linked list for a data item with a specified key value and to delete an item with a specified key value. These, along with insertion at the start of the list, are the same operations carried out by the LinkList Workshop applet. The complete linkList2.java program is shown in Listing 5.2. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
  • 7. Finding and Deleting Specified Links 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
  • 8. Finding and Deleting Specified Links 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
  • 9. Output: The main() routine makes a list, inserts four items, and displays the resulting list. It then searches for the item with key 44, deletes the item with key 66, and displays the list again. Here’s the output: List (first-->last): {88, 8.99} {66, 6.99} {44, 4.99} {22, 2.99} Found link with key 44 Deleted link with key 66 List (first-->last): {88, 8.99} {44, 4.99} {22, 2.99} 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
  • 10. LISTING 5.3 The firstLastList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
  • 11. LISTING 5.3 The firstLastList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
  • 12. LISTING 5.3 The firstLastList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12 For simplicity, in this program we’ve reduced the number of data items in each link from two to one. This makes it easier to display the link contents. (Remember that in a serious program there would be many more data items, or a reference to another object containing many data items.) This program inserts three items at the front of the list, inserts three more at the end, and displays the resulting list. It then deletes the first two items and displays the list again. Here’s the output: List (first-->last): 66 44 22 11 33 55 List (first-->last): 22 11 33 55
  • 13. LISTING 5.4 The linkStack.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
  • 14. LISTING 5.4 The linkStack.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
  • 15. LISTING 5.4 The linkStack.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
  • 16. Output: The main() routine creates a stack object, pushes two items on it, displays the stack, pushes two more items, and displays the stack again. Finally, it pops two items and displays the stack a third time. Here’s the output: Stack (top-->bottom): 40 20 Stack (top-->bottom): 80 60 40 20 Stack (top-->bottom): 40 20 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
  • 17. The sortedList.java Program The sortedList.java example shown in Listing 5.6 presents a SortedList class with insert(), remove(), and displayList() methods. Only the insert() routine is different from its counterpart in non-sorted lists. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
  • 18. The sortedList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 18
  • 19. The sortedList.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 19
  • 20. Output: In main() we insert two items with key values 20 and 40. Then we insert three more items, with values 10, 30, and 50. These values are inserted at the beginning of the list, in the middle, and at the end, showing that the insert() routine correctly handles these special cases. Finally, we remove one item, to show removal is always from the front of the list. After each change, the list is displayed. Here’s the output from sortedList.java: List (first-->last): 20 40 List (first-->last): 10 20 30 40 50 List (first-->last): 20 30 40 50 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
  • 21. LISTING 5.7 The listInsertionSort.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
  • 22. LISTING 5.7 The listInsertionSort.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
  • 23. LISTING 5.7 The listInsertionSort.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23 This program displays the values in the array before the sorting operation and again afterward. Here’s some sample output: Unsorted array: 59 69 41 56 84 15 86 81 37 35 Sorted array: 15 35 37 41 56 59 69 81 84 86
  • 24. The doublyLinked.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 24
  • 25. The doublyLinked.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 25
  • 26. The doublyLinked.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 26
  • 27. The doublyLinked.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 27
  • 28. The doublyLinked.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 28 In main() we insert some items at the beginning of the list and at the end, display the items going both forward and backward, delete the first and last items and the item with key 11, display the list again (forward only), insert two items using the insertAfter() method, and display the list again. Here’s the output: List (first-->last): 66 44 22 11 33 55 List (last-->first): 55 33 11 22 44 66 List (first-->last): 44 22 33 List (first-->last): 44 22 77 33 88
  • 29. Iterator Methods Additional methods can make the iterator a flexible and powerful class. All operations previously performed by the class that involve iterating through the list, such as insertAfter(), are more naturally performed by the iterator. In our example the iterator includes the following methods: • reset()—Sets the iterator to the start of the list • nextLink()—Moves the iterator to the next link • getCurrent()—Returns the link at the iterator • atEnd()—Returns true if the iterator is at the end of the list 234 CHAPTER 5 Linked Lists • insertAfter()—Inserts a new link after the iterator • insertBefore()—Inserts a new link before the iterator • deleteCurrent()—Deletes the link at the iterator 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 29
  • 30. LISTING 5.9 The interIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 30
  • 31. LISTING 5.9 The interIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 31
  • 32. LISTING 5.9 The interIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 32
  • 33. LISTING 5.9 The interIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 33
  • 34. LISTING 5.9 The interIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 34
  • 35. LISTING 5.9 The interIterator.java Program 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 35
  • 36. Output: 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 36 The main() routine inserts four items into the list, using an iterator and its insertAfter() method. Then it waits for the user to interact with it. In the following sample interaction, the user displays the list, resets the iterator to the beginning, goes forward two links, gets the current link’s key value (which is 60), inserts 100 before this, inserts 7 after the 100, and displays the list again:
  • 37. Summary A linked list consists of one linkedList object and a number of Link objects. • The linkedList object contains a reference, often called first, to the first link in the list. • Each Link object contains data and a reference, often called next, to the next link in the list. • A next value of null signals the end of the list. • Inserting an item at the beginning of a linked list involves changing the new link’s next field to point to the old first link and changing first to point to the new item. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 37
  • 38. Summary 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 38 Stacks and queues are ADTs. They can be implemented using either arrays or linked lists. • In a sorted linked list, the links are arranged in order of ascending (or sometimes descending) key value. • Insertion in a sorted list takes O(N) time because the correct insertion point must be found. Deletion of the smallest link takes O(1) time. • In a doubly linked list, each link contains a reference to the previous link as well as the next link. • A doubly linked list permits backward traversal and deletion from the end of the list. • An iterator is a reference, encapsulated in a class object, that points to a link in an associated list. • Iterator methods allow the user to move the iterator along the list and access the link currently pointed to. • An iterator can be used to traverse through a list, performing some operation on selected links (or all links).