Recursive Objects Singly Linked Lists.

Slides:



Advertisements
Similar presentations
Linked Lists Linear collections.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
The Singleton Pattern II Recursive Linked Structures.
List Interface CS java.util.List Interface and its Implementers CS340 2.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Information and Computer Sciences University of Hawaii, Manoa
LinkedList Many slides from Horstmann modified by Dr V.
Information and Computer Sciences University of Hawaii, Manoa
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“When we quit thinking primarily about ourselves and our own self-preservation, we undergo a truly heroic transformation of consciousness.” – Joseph Campbell.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Recursive Objects  an object that holds a reference to its own type is a recursive object  linked lists and trees are classic examples in computer science.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
1 CS162: Introduction to Computer Science II Abstract Data Types.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Recursion Powerful Tool
Object-Oriented Concepts
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
EECE 310: Software Engineering
Computing with C# and the .NET Framework
Single-Linked Lists.
4. Linked Lists.
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part
Recursive Objects (Part 4)
Big-O notation Linked lists
CSC 222: Object-Oriented Programming
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
John Hurley Cal State LA
Efficiency of in Binary Trees
Programming Abstractions
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
Trees.
Searching.
LINKED LISTS CSCD Linked Lists.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Top Ten Words that Almost Rhyme with “Peas”
MyList<T> It’s a generic type, <T> is a type parameter
Java Software Structures: John Lewis & Joseph Chase
More Data Structures (Part 1)
TCSS 143, Autumn 2004 Lecture Notes
Priority Queues.
Lecture 26: Advanced List Implementation
Linked Lists [AJ 15].
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Generic Classes and Methods
Computer Science and Engineering
ArrayLists 22-Feb-19.
Collections Framework
slides created by Marty Stepp
Introduction to Data Structure
More Data Structures (Part 1)
Data Structures & Algorithms
Trees.
Programming II (CS300) Chapter 07: Linked Lists
Trees.
TCSS 143, Autumn 2004 Lecture Notes
Linked Lists and Loops based on slides by Ethan Apter
Presentation transcript:

Recursive Objects Singly Linked Lists

Recursive Objects an object that holds a reference to its own type is a recursive object linked lists and trees are classic examples in computer science of objects that can be implemented recursively

Data Structures data structures (and algorithms) are one of the foundational elements of computer science a data structure is a way to organize and store data so that it can be used efficiently list – sequence of elements set – a group of unique elements map – access elements using a key many more...

Singly Linked List a data structure made up of a sequence of nodes each node has some data a field that contains a reference (a link) to the next node in the sequence suppose we have a linked list that holds characters; a picture of our linked list would be: node 'a' 'x' 'r' 'a' 's' data null link

Singly Linked List the first node of the list is called the head node 'x' 'r' 'a' 's' data null link

UML Class Diagram LinkedList - size : int - head : Node ... Node - data : char - next : Node ... Node 'a' next data

Node nodes are implementation details that the client does not need to know about LinkedList needs to be able to create nodes i.e., needs access to a constructor if we create a separate Node class other clients can create nodes no way to hide the constructor from every client except LinkedList Java allows the implementer to define a class inside of another class

a nested class is a class that is defined inside of another class public class LinkedList { private static class Node { private char data; private Node next; public Node(char c) { this.data = c; this.next = null; } // ... Node is an nested class a nested class is a class that is defined inside of another class a static nested class behaves like a regular top-level class does not have access to private members of the enclosing class e.g., Node does not have access to the private fields of LinkedList a nested class is a member of the enclosing class LinkedList has direct access to private features of Node

LinkedList constructor /** * Create a linked list of size 0. * */ public LinkedList() { this.size = 0; this.head = null; }

Creating a Linked List to create the following linked list: 'a' 'x' LinkedList t = new LinkedList(); t.add(‘a’); t.add(‘x’); t.add(‘r’); t.add(‘s’); 'a' 'x' 'r' 'a' 's' null

Add to end of list (recursive) methods of recursive objects can often be implemented with a recursive algorithm notice the word "can"; the recursive implementation is not necessarily the most efficient implementation adding to the end of the list can be done recursively base case: at the end of the list i.e., next is null create new node and append it to this link recursive case: current link is not the last link add to the end of next

/. Adds the given character to the end of the list /** * Adds the given character to the end of the list. * * @param c The character to add */ public void add(char c) { if (this.size == 0) { this.head = new Node(c); } else { LinkedList.add(c, this.head); this.size++; recursive method

/. Adds the given character to the end of the list /** * Adds the given character to the end of the list. * * @param c The character to add * @param node The node at the head of the current sublist */ private static void add(char c, Node node) { if (node.next == null) { node.next = new Node(c); } else { LinkedList.add(c, node.next);

Add to end of list (iterative) adding to the end of the list can be done iteratively public void add(char c) { if (this.size == 0) { this.head = new Node(c); } else { Node n = this.head; while (n.next != null) { n = n.next; n.next = new Node(c); this.size++; Starting from the head of the list, follow the links from node to node until you reach the last node.

Getting an Element in the List a client may wish to retrieve the ith element from a list the ability to access arbitrary elements of a sequence in the same amount of time is called random access arrays support random access; linked lists do not to access the ith element in a linked list we need to sequentially follow the first (i - 1) links takes O(n) time versus O(1) for arrays 'a' 'x' 'r' 'a' 's' t.get(3) link 0 link 1 link 2

Getting an Element in the List validation? getting the ith element can be done recursively base case: index == 0 return the value held by the current link recursive case: get the element at index – 1 starting from next

/. Returns the item at the specified position. in the list /** * Returns the item at the specified position * in the list. * * @param index index of the element to return * @return the element at the specified position * @throws IndexOutOfBoundsException if the index * is out of the range * {@code (index < 0 || index >= list size)} */ public char get(int index) { if (index < 0 || index >= this.size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size); } return LinkedList.get(index, this.head); recursive method

/. Returns the item at the specified position. in the list /** * Returns the item at the specified position * in the list. * * @param index index of the element to return * @param node The node at the head of the current sublist * @return the element at the specified position */ private static char get(int index, Node node) { if (index == 0) { return node.data; } return LinkedList.get(index - 1, node.next);

Setting an Element in the List setting the ith element is almost exactly the same as getting the ith element

/. Sets the element at the specified position. in the list /** * Sets the element at the specified position * in the list. * * @param index index of the element to set * @param c new value of element * @throws IndexOutOfBoundsException if the index * is out of the range * {@code (index < 0 || index >= list size)} */ public void set(int index, char c) { if (index < 0 || index >= this.size) { throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size); } LinkedList.set(index, c, this.head); recursive method

/. Sets the element at the specified position. in the list /** * Sets the element at the specified position * in the list. * * @param index index of the element to set * @param c new value of the element * @param node The node at the head of the current sublist */ private static void set(int index, char c, Node node) { if (index == 0) { node.data = c; return; } LinkedList.set(index - 1, c, node.next);

toString finding the string representation of a list can be done recursively the string is "[a, x, r, a, s] the string is "[" + "a, " + toString(the list['x', 'r', 'a', 's']) 'a' 'x' 'r' 'a' 's'

toString base case: next is null return the value of the link as a string + "]" recursive case: current link is not the last link return the value of the link as a string + ", " + the rest of the list as a string

public String toString() { if (this public String toString() { if (this.size == 0) { return "[]"; } return "[" + LinkedList.toString(this.head); private static String toString(Node n) { if (n.next == null) { return n.data + "]"; String s = n.data + ", "; return s + LinkedList.toString(n.next); recursive method

Finding an element in the list often useful to ask if a list contains a particular element worst case: must visit every element of the list e.g., t.contains('z') 'a' 'x' 'r' 'a' 's'

Finding an element in the list contains can be solved recursively base case: found the character we are looking for i.e., node data is equal to the character we are searching for return true base case: at the end of the list i.e., node next is null return false recursive case: have not found the character we are searching for and not at the end of the list search the sublist starting at node.next return result

/** * Returns <code>true</code> if this list contains the specified element. * * @param c element to search for * @return <code>true</code> if this list contains the * specified element */ public boolean contains(char c) { if (this.size == 0) { return false; } return LinkedList.contains(c, this.head); recursive method

/** * Returns <code>true</code> if this list contains the specified element. * * @param c element to search for * @param node the node at the head of the current sublist * @return <code>true</code> if this list contains the * specified element */ private static boolean contains(char c, Node node) { if (node.data == c) { return true; } if (node.next == null) { return false; return LinkedList.contains(c, node.next);

Finding an element in the list closely related to contains is finding the index of an element in the list worst case: must visit every element of the list e.g., t.indexOf('s') 'a' 'x' 'r' 'a' 's'

Finding an element in the list indexOf can be solved recursively base case: found the character we are looking for i.e., node data is equal to the character we are searching for return 0 base case: at the end of the list i.e., node next is null return -1 recursive case: have not found the character we are searching for and not at the end of the list search the sublist starting at node.next return 1 + result

/. Returns the index of the first occurrence of the /** * Returns the index of the first occurrence of the * specified element in this list, or -1 if this list * does not contain the element. * * @param c * element to search for * @return the index of the first occurrence of the * specified element in this list, or -1 if this * list does not contain the element */ public int indexOf(char c) { if (this.size == 0) { return -1; } return LinkedList.indexOf(c, this.head); recursive method

// returns the index of the node containing c relative to node n private static int indexOf(char c, Node n) { if (n.data == c) { return 0; } if (n.next == null) { return -1; int i = LinkedList.indexOf(c, n.next); if (i == -1) { return 1 + i;