
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An in-depth explanation of Singly Linked Lists, including their definition, operations, and implementation in Java. It includes adding items to the list, removing items, and retrieving items from a specific index.
What you will learn
Typology: Study notes
1 / 24
This page cannot be seen from the preview
Don't miss anything!
BASIC DATA STRUCTURES
TODAY’S LECTURE IN A NUTSHELL Lecture 9: Singly Linked Lists ▸ Singly Linked Lists
SINGLY LINKED LISTS Recursive Definition of Singly Linked Lists ‣ (^) A singly linked list is either empty (null) or a node having a reference to a singly linked list. ‣ (^) Node: is a data type that holds any kind of data and a reference to a node. item Node item1 item2 item3 item4 item Head/Beginning/Front/First
SINGLY LINKED LISTS Node private class Node { Item item; Node next; } item Node
SINGLY LINKED LISTS SinglyLinkedList(): Constructs an empty SLL first = null n = 0 SinglyLinkedList
SINGLY LINKED LISTS add(Item item):Inserts the specified item at the head of the singly linked list sll.add(“CS062”) n= CS Head/Beginning/Front/First What should happen? sll.add(“ROCKS”);
SINGLY LINKED LISTS add(Item item):Inserts the specified item at the head of the singly linked list sll.add(“!”) n= ROCKS Head/Beginning/Front/First ! CS What should happen? sll.add(1,“?”);
SINGLY LINKED LISTS add(int index, Item item):Adds item at the specified index sll.add(1,“?”) n= ROCKS Head/Beginning/Front/First !? CS What should happen? sll.remove();
SINGLY LINKED LISTS remove(int index):Retrieves and removes the item at the specified index sll.remove(1) n= Head/Beginning/Front/First ? CS
SINGLY LINKED LISTS Our own implementation of Singly Linked Lists ‣ (^) We will follow the textbook style. ‣ (^) It does not offer a class for this so we will build our own. ‣ (^) We will work with generics because we don’t want to offer multiple implementations. ‣ (^) We will use an inner class Node and we will keep track of how many elements we have in our singly linked list.
SINGLY LINKED LISTS Check if is empty and how many items /**
SINGLY LINKED LISTS Retrieve item from specified index /**
SINGLY LINKED LISTS Check if index is >=0 and <n /**
SINGLY LINKED LISTS Insert item at a specified index /**