Advanced Java Concepts Unit 2: Linked Lists.

Similar documents
Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

Java Arrays (review) Linked Lists (preview)

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

CSE 143X. Accelerated Computer Programming I/II

Chapter 29B: More about Strings Bradley Kjell (Revised 06/12/2008)

Interfaces, collections and comparisons

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Topic 11 Linked Lists

Java Review: Objects

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Linked List Implementation of Queues

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

Arrays.

COMP 110 Programming Exercise: Simulation of the Game of Craps

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Array Based Lists. Collections

A Linked Structure. Chapter 17

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

+ Fraction Class (gcd)

Java Language Features

Class 26: Linked Lists

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Linked List. ape hen dog cat fox. tail. head. count 5

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

AP CS Unit 7: Arrays Exercises

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

AP CS Unit 4: Classes and Objects Programs

ArrayList. Introduction. java.util.arraylist

CSE1030 Lecture #18. CSE1030 Introduction to Computer Science II. Linked List Terminology

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Lecture 6: ArrayList Implementation

The Java Collections Framework and Lists in Java Parts 1 & 2

Computational Expression

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

AP Computer Science Summer Assignment (updated 5/29/2018) DUE : Sept. 4, 2018

PASS4TEST IT 인증시험덤프전문사이트

ECE 122. Engineering Problem Solving with Java

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

CSE 143. Lecture 7: Linked List Basics reading: 16.2

Outline. iterator review iterator implementation the Java foreach statement testing

C27a: Stack and Queue

Examination Questions Midterm 1

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

AP CS Unit 7: Interfaces. Programs

Linked Lists. Chapter 12.3 in Savitch

A+ Computer Science -

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Java Collections Framework

Programming Abstractions

CSC9T4: Object Modelling, principles of OO design and implementation

Advanced Java Concepts Unit 3: Stacks and Queues

Introduction to Computer Science II (ITI 1121) Final Examination

CS201 Discussion 7 MARKOV AND RECURSION

CS 307 Final Spring 2009

STUDENT LESSON A15 ArrayList

Dynamic Data Structures and Generics

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Dynamic Data Structures and Generics

+ Abstract Data Types

Lists. The List ADT. Reading: Textbook Sections

Linked Lists. References and objects

Last class. -More on polymorphism -super -Introduction to interfaces

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Doubly linked lists and freelist node caches

Review: Array Initializer Lists

Programming II (CS300)

Introduction to Computer Science II CS S-20 Linked Lists III

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

CS 307 Final Fall 2009

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

Name: CS 159 Practice Final Fall 2015

Recursion (Part 3) 1

Set<Integer> s = new TreeSet<Integer>(); s.add( 7 ); s.add( 7 ); System.out.println( s.size() );

COS 126 Midterm 2 Programming Exam Fall 2012

Unit 10: Sorting/Searching/Recursion

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Name: CS 159 Practice Final Fall 2015

EECS2030 Fall 2016 Preparation Exercise for Lab Test 2: A Birthday Book

type conversion polymorphism (intro only) Class class

Nested Loops. A loop can be nested inside another loop.

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

AP Computer Science Lists The Array type

ASSIGNMENT 5 Data Structures, Files, Exceptions, and To-Do Lists

CS 200 Objects and ArrayList Jim Williams, PhD

What does this output? Is there a better way to say the same thing? What does this do? Is there a better way to say the same thing?

Transcription:

Advanced Java Concepts Unit 2: Linked Lists. The List interface defines the structure of a linear collection. Here are some of its methods. boolean add( E element ) Appends the element to the end of the list void add( int index, E element ) Inserts the element at the specified index. 0 <= index <= size void clear Removes all elements from the list boolean contains( Object obj ) Returns true if the list contains the object E get( int index ) Returns the element at the index E remove( int index ) Removes the element at the index and returns it boolean remove( Object obj ) Removes the first occurrence of the object E set( int index, E element ) Replaces the element at specified index and returns the displaced element. int size() Returns the number of elements in the list. As you may remember from AP Computer Science, the ArrayList class implements the List interface. An array list object stores all the data into an array (by default the array size is ten). If the number of items in the list gets close to the array size, the object will automatically create a new, larger array and copy the old data into the new array. If later the number of items decreases then a new, smaller array is created and used. Linked Lists. A linked list is a dynamic data structure, growing and shrinking during runtime. In contrast, an array is a static data structure. Once an array is instantiated, its size does not change. A linked list consists of nodes. Here is an outline of a node class that we use. public class Node { public Object ; public Node ; public Node ; The fields are public to make the exercise problems easier to read. public Node( Object initvalue, Node Node, Node Node ){ = initvalue; = Node; HEY! We will use the term node in connection = Node; with other data structures. This particular Node class just applies to some types of linked lists. public String tostring(){ return "node " +.tostring(); The purpose of a node is to contain data and link to other nodes. A linked list is a collection of nodes. Page 1

Read over this problem then read the discussion that follows. Then start solving the problems. 1. This compiles and runs. What is displayed? 1 2 3 4 5 6 7 8 9 Node n1 = new Node( "rat",, ); Node n2 = new Node( "fish", n1, ); System.out.println( n1 ); System.out.println( n2 ); Node n3 = n2.; System.out.println( n3 ); Node n4 = n1.; System.out.println( n4 ); The first two lines create two variables, n1 and n2, and two Node objects. The arrows represent references. n1 rat n2 fish n1 and n2 contain references to their respective Node objects. The field of the second Node object also contains a reference to the first Node object. After the third line is executed, we now have: n1 rat n2 fish This picture of variables and field containing references to each other will help you answer these problems. 2. This compiles and runs. What is displayed? Node n1 = new Node( "B",, ); Node n2 = new Node( "X",, ); Node n3 = new Node( "W",, ); Node n4 = new Node( "E",, ); n2. = n4; n3. = n1; n1. = n2; n4. = n3; System.out.println( n1.. ); System.out.println( n1... ); System.out.println( n1... ); Page 2

3. Does this code print or does it throw a NullPointerException? Node n1 = new Node( "cat",, ); System.out.println( n1.. ); 4. This compiles but eventually throws a runtime exception. Before that happens, what is displayed? What line throws the exception? 1 2 3 4 5 6 7 8 9 10 Node node = new Node( 3,, ); node = new Node( 2.72, node, ); node = new Node( "swan", node, ); System.out.println( node.); 5. This compiles. What does it display? If it crashes, explain. If it is an endless loop then show what is displayed before it starts repeating. Node n1 = new Node( "bob",, ); Node n2 = new Node( "is",, n1 ); Node n3 = new Node( 16,, n2 ); n1. = n3; Node x = n1; while ( x!= ) { System.out.println( x. ); x = x.; 6. What is displayed? Node node = new Node( "boo",, ); node = new Node( n+"", node, ); { 7. What is displayed? Node node = new Node( "boo",, ); node = new Node( n+"", node, ); { 8. What is displayed? Node node = new Node( "hoo",, ); node = new Node( n+"",, node ); Page 3

9. What is displayed? Node n1 = new Node( "do",, ); Node n2 = new Node( "re",, ); Node n3 = new Node( "mi",, ); n1. = n3; n2. = n1; n3. = n2; Node node = n2; { 10. What is displayed? If there's a run-time error, why? 11. What is displayed? If there's a run-time error, what is displayed up to the error? 12. What is displayed? If there's a run-time error, what is displayed up to the error? Node n1 = new Node( "E",, ); Node n2 = new Node( "D",, n1 ); Node node = n2.; Node n1 = new Node( "U",, ); Node n2 = new Node( "P", n1, n1 ); Node node = n2.; Node n1 = new Node( 17,, ); Node n2 = new Node( 55,, ); Node n3 = new Node( 88,, ); n2. = n3; n2. = n1; n3. = n2; Node node = n1; while ( node!= && node.!= ){ while ( node!= && node.!= ){ Page 4

13. The first 3 lines make a simple linked list with A in the front and C at the end. In the blank lines, write the code that correctly inserts another Node, containing "B", between the other two nodes. You may not need all the blank lines 14. The first 5 lines make a simple linked list with X in the front, then Y, then Z at the end. In the blank lines, write the code that correctly deletes the middle node and leaves the X and Z still linked to each other. You may not need all the blank lines Node n1 = new Node( "A",, ); Node n2 = new Node( "C", n1, ); Node n1 = new Node( "X",, ); Node n2 = new Node( "Y", n1, ); Node n3 = new Node( "Z", n2, ); n2. = n3; While there are different types of linked lists, we will only cover one type a doubly linked list. A doubly linked lists consists of nodes which each have a link to the ious node and the node in the list. Here is the design that we will use for our doubly linked list. There is a head node. There is no node before it and it contains no data. It simply contains a reference to the start of the list. There is a tail node. There is no node after it and it contains no data. It simply contains a reference to the end of the list. Data nodes. These nodes contain data and have links to the nodes before and after them. Let s picture a node as containing three parts: a reference to a node before it, the data, and a reference to the node after it. ious node data node This figure shows what our linked list looks like when it is first created. head ref. to the tail ref. to the head tail The logical size of this linked list is zero because it only contains a head and a tail (which will never contain any data). Page 5

After a node is added to the linked list, it looks like this. data head index 0 tail The logical size of the linked list is now one and the node that was added is considered to be at index 0 with references to the head and tail of the list. A doubly linked list with a size of two looks like this: data data head index 0 index 1 tail ****************************************************************************** Inserting a Node in a Doubly Linked List. Suppose you have a list with 50 nodes and you want to insert a new index 14 (after the item at index 13 and before the item currently at index 14). 1. Start at the head and link to node in front of it. Have that node link to the one in front of it. Keep linking until you have reached the index 13. 2. Create a new node. Its ious node is the 13 and its node is the 14. 3. Change node 13 so that its node is the new node. Change node 14 so that its ious node is the new node. Step 2 Step 3 index 13 index 14 index 13 index 14 new node new node Note. The nodes in a linked list do not keep track of their index. Each node simply knows which node comes before it, which node comes after it, and the data it contains. Page 6

Deleting a Node from a Double Linked List. This works in a manner similar to deleting a node. 1. Find the node you want to delete. 2. Change the nodes before and after that node so that they refer to each other. 3. At this point nothing is referring to the deleted node and it will eventually be garbage collected. Step 1 Step 2 node to be deleted node to be deleted The List Interface: ArrayLists vs. LinkedLists method ArrayLists Linked Lists add( int index, E element ) The closer to the front of the list, the longer this process takes because elements must be moved to make room for the new. get( int index ) remove( int index ) This is a quick operation that is not effected by the size of the list. The closer to the front of the list, the longer this process takes because elements must be moved If the index is near the front (or end) of the list, this is a quick operation that is not effected by the size of the list. Starts at the front or end of the list and moves from node to node until reaching the index. If the index is near the front (or end) of the list, this is a quick operation that is not effected by the size of the list. As a general rule, people implement a List object using the ArrayList class because overall it is a faster implementation. However, in certain situations (such routinely inserting and removing from the front of a large list), LinkedList is the better choice. Page 7