Docsity

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

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++; } }