Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Singly Linked Lists in Data Structures and Advanced Programming, Study notes of Data Structures and Algorithms

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

  • How do you add an item to a Singly Linked List?
  • What is a Singly Linked List?
  • How do you retrieve an item from a specific index in a Singly Linked List?
  • What are the standard operations of a Singly Linked List?
  • How do you remove an item from a Singly Linked List?

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

paperback
paperback 🇺🇸

4.8

(12)

264 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS062
DATA STRUCTURES AND ADVANCED PROGRAMMING
9: Singly Linked Lists
Alexandra Papoutsaki!
LECTURES
Mark Kampe!
LABS
Some slides adopted from Princeton C0S226 course or Algorithms, 4th Edition
BASIC DATA STRUCTURES
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Singly Linked Lists in Data Structures and Advanced Programming and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS

DATA STRUCTURES AND ADVANCED PROGRAMMING

9: Singly Linked Lists

Alexandra Papoutsaki

LECTURES

Mark Kampe

LABS

Some slides adopted from Princeton C0S226 course or Algorithms, 4th Edition

BASIC DATA STRUCTURES

TODAY’S LECTURE IN A NUTSHELL Lecture 9: Singly Linked Lists ▸ Singly Linked Lists

Some slides adopted from Algorithms 4th Edition and Oracle tutorials

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 sll = new SinglyLinkedList(); What should happen? sll.add(“CS062”);

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 /**

  • Returns true if the singly linked list does not contain any item.
  • @return true if the singly linked list does not contain any item / public boolean isEmpty() { return first == null; // return size() == 0; } /*
  • Returns the number of items in the singly linked list.
  • @return the number of items in the singly linked list */ public int size() { return n; }

SINGLY LINKED LISTS Retrieve item from specified index /**

  • Returns item at the specified index.
  • @param index
  • the index of the item to be returned
  • @return the item at specified index */ public Item get(int index) { rangeCheck(index); Node finger = first; // search for index-th element or end of list while (index > 0) { finger = finger.next; index--; } return finger.item; }

SINGLY LINKED LISTS Check if index is >=0 and <n /**

  • A helper method to check if an index is in range 0<=index<n
  • @param index
  • the index to check */ private void rangeCheck(int index) { if (index > n || index < 0) throw new IndexOutOfBoundsException("Index " + index + " out of bounds"); }

SINGLY LINKED LISTS Insert item at a specified index /**

  • Inserts the specified item at the specified index.
  • @param index
  • the index to insert the node
  • @param item
  • the item to insert */ public void add(int index, Item item) { rangeCheck(index); if (index == 0) { add(item); } else { Node previous = null; Node finger = first; // search for index-th position while (index > 0) { previous = finger; finger = finger.next; index--; } // create new value to insert in correct position. Node current = new Node(); current.next = finger; current.item = item; // make previous value point to new value. previous.next = current; n++; } }