Singly Linked Lists - IT Collegeenos.itcollege.ee/~jpoial/algorithms/GT/Goodrich_6... · Singly...
Transcript of Singly Linked Lists - IT Collegeenos.itcollege.ee/~jpoial/algorithms/GT/Goodrich_6... · Singly...
![Page 1: Singly Linked Lists - IT Collegeenos.itcollege.ee/~jpoial/algorithms/GT/Goodrich_6... · Singly Linked Lists 3/18/14 1 © 2014 Goodrich, Tamassia , Goldwasser Singly Linked Lists](https://reader036.fdocuments.net/reader036/viewer/2022062918/5ede3bbead6a402d66698cb8/html5/thumbnails/1.jpg)
Singly Linked Lists 3/18/14
1
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 1
Singly Linked Lists
Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 2
Singly Linked List ! A singly linked list is a
concrete data structure consisting of a sequence of nodes, starting from a head pointer
! Each node stores n element n link to the next node
next
element node
A B C D
∅
head
![Page 2: Singly Linked Lists - IT Collegeenos.itcollege.ee/~jpoial/algorithms/GT/Goodrich_6... · Singly Linked Lists 3/18/14 1 © 2014 Goodrich, Tamassia , Goldwasser Singly Linked Lists](https://reader036.fdocuments.net/reader036/viewer/2022062918/5ede3bbead6a402d66698cb8/html5/thumbnails/2.jpg)
Singly Linked Lists 3/18/14
2
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 3
A Nested Node Class
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 4
Accessor Methods
![Page 3: Singly Linked Lists - IT Collegeenos.itcollege.ee/~jpoial/algorithms/GT/Goodrich_6... · Singly Linked Lists 3/18/14 1 © 2014 Goodrich, Tamassia , Goldwasser Singly Linked Lists](https://reader036.fdocuments.net/reader036/viewer/2022062918/5ede3bbead6a402d66698cb8/html5/thumbnails/3.jpg)
Singly Linked Lists 3/18/14
3
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 5
Inserting at the Head • Allocate new
node • Insert new
element • Have new
node point to old head
• Update head to point to new node
© 2014 Goodrich, Tamassia, Goldwasser
Inserting at the Tail • Allocate a new node • Insert new element • Have new node point to null • Have old last node point to new node • Update tail to point to new node
Singly Linked Lists 6
![Page 4: Singly Linked Lists - IT Collegeenos.itcollege.ee/~jpoial/algorithms/GT/Goodrich_6... · Singly Linked Lists 3/18/14 1 © 2014 Goodrich, Tamassia , Goldwasser Singly Linked Lists](https://reader036.fdocuments.net/reader036/viewer/2022062918/5ede3bbead6a402d66698cb8/html5/thumbnails/4.jpg)
Singly Linked Lists 3/18/14
4
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 7
Java Methods
© 2014 Goodrich, Tamassia, Goldwasser
Removing at the Head
• Update head to point to next node in the list
• Allow garbage collector to reclaim the former first node
Singly Linked Lists 8
![Page 5: Singly Linked Lists - IT Collegeenos.itcollege.ee/~jpoial/algorithms/GT/Goodrich_6... · Singly Linked Lists 3/18/14 1 © 2014 Goodrich, Tamassia , Goldwasser Singly Linked Lists](https://reader036.fdocuments.net/reader036/viewer/2022062918/5ede3bbead6a402d66698cb8/html5/thumbnails/5.jpg)
Singly Linked Lists 3/18/14
5
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists 9
Java Method
© 2014 Goodrich, Tamassia, Goldwasser
Removing at the Tail • Removing at the tail of a singly linked list is
not efficient! • There is no constant-time way to update the
tail to point to the previous node
Singly Linked Lists 10