Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Similar documents
Linked Lists. Chapter 12.3 in Savitch

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

Programming II (CS300)

Linked Lists. Chapter 4

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

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

Programming II (CS300)

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

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

Building Java Programs

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

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

Linked List Nodes (reminder)

Array Based Lists. Collections

Data Structures and Algorithms

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

CS S-05 Abstract Data Types and Lists 1

Laboratorio di Algoritmi e Strutture Dati

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

Linked Lists 11/16/18. Preliminaries. Java References. Objects and references. Self references. Linking self-referential nodes

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

Midterm Spring 2015 Solutions Version 1

1. ArrayList and Iterator in Java

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

List ADT. B/W Confirming Pages

Data Structures and Algorithms

CSE 143 Au03 Midterm 2 Page 1 of 7

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Class 26: Linked Lists

Recursive Objects. Singly Linked List (Part 2)

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

Assertions, pre/postconditions

Implementing Lists, Stacks, Queues, and Priority Queues

CMSC 132: Object-Oriented Programming II. Linked Lists

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

+ Abstract Data Types

Introduction to Computer Science II CS S-18 Linked Lists

CMSC 206: Data Structures Final Exam Reference May 2018

CS/CE 2336 Computer Science II

Computer Science 136. Midterm Examination

Lists. ordered lists unordered lists indexed lists 9-3

General Insert Operation

CMSC 341 Lecture 7 Lists

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

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

Model Solutions. COMP 103: Test April, 2013

ITI Introduction to Computing II

Java Review: Objects

Computer Science CS221 Test 4 Name

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

CSCD 326 Data Structures I Stacks

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

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Lecture 6: ArrayList Implementation

Specifications for the ADT List Ali list provides a way to organize data List of students List of sales List of lists

Computer Science 210: Data Structures. Linked lists

Chapter 17: Linked Lists

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

CE204 Data Structures and Algorithms Part 2

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Outline. iterator review iterator implementation the Java foreach statement testing

CMSC 341. Linked Lists. Textbook Section 3.5

LinkedList Implementation Mini-project intro

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CMPSCI 230 Discussion 1. Virtual Box and HW0

Chapter 17: Linked Lists

CS1020 Data Structures and Algorithms I Lecture Note #10. List ADT & Linked Lists

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CS S-20 Linked Lists IV 1

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

2/22/2013 LISTS & TREES

"apple" "grape" "grape" "grape" "apple"

CSE 143 SAMPLE MIDTERM

Lecture 12: Doubly Linked Lists

Linked Lists. It s all connected

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

EXAMINATIONS 2017 TRIMESTER 2

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

What is the Java Collections Framework?

Computer Science II CSci 1200 Sections 1-4,6 Week 6, Friday Class October 5, 2001 Linked Lists, Part 2

About this exam review

Linked Lists. It s a conspiracy!

Introduction to Object-Oriented Programming

Interfaces, collections and comparisons

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

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

ITI Introduction to Computing II

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

CSC 321: Data Structures. Fall 2017

Lecture 3 Linear Data Structures

Transcription:

Linked Lists Walls and Mirrors Chapter 5 Linked List Nodes public class Node { private int item; private Node next; public Node(int item) { this(item,null); public Node(int item, Node next) { setitem(item); setnext(next); A linked list is a collection of Nodes: 17 9 null 1

The list interface Method object get(index) index indexof(object) add (object) add (index, object) object remove(index) object remove(object) int size() boolean isempty() clear() Returns the element at the given position Returns the index of the first occurrence of the specified element Appends an element to the list inserts given value at given index, shifting subsequent values right Removes the element at the specified position (and returns it) Removes the element that corresponds to the given object (and returns it) returns the size of the list indicates if the list is empty removes all elements from the list index is an int, and object is of type Object Linked List version 1 public class LinkedList { private Node head; private int size; public LinkedList() { null; size = 0; LinkedList size = 0 methods go here 2

Implementing add n How do we add to a linked list? q add at the end of the list q add at a given index 17 9 null The add method public boolean add(int index, Object item){ if (index<0 index>size) return false; if (index == 0) { new Node(item, head); else { // find predecessor of node Node curr = head; for (int i=0; i<index-1; i++){ curr = curr.getnext(); curr.setnext(new Node (item, curr.getnext())); size++; return true; 3

Implementing remove // Removes value at given index from list. public void remove(int index) {... q How do we remove a node from a list? q Does it matter what the list's contents are before the remove? size = 3 17 element 0 element 1 element 2 Removing a node from a list n Before removing element at index 1: size = 3 20 element 0 element 1 element 2 n After: size = 2 20 element 0 element 1 4

Removing first node from a list n Before removing element at index 0: size = 3 20 element 0 element 1 element 2 n After: size = 2 20 element 0 element 1 List with a single element n Before: After: size = 1 data next 20 element 0 size = 0 q We must change the front field to store null instead of a node. q Do we need a special case to handle this? 5

The remove method public void remove(int index) { if (index<0 index >= size) throw new IndexOutOfBoundsException ("List index out of bounds"); if (index == 0) { // special case: removing first element head.getnext(); else { // removing from elsewhere in the list Node current = head; for (int i = 0; i < index - 1; i++) { current = current.getnext(); current.setnext(current.getnext().getnext()); size--; The clear method n How do you implement a method for removing all the elements from a linked list? 6

Linked lists recursively n Traversal: q Write the first node of the list q Write the list minus its first node n Let s code this! Recursive linked list traversal private static void writelist (Node node) { //precondition: linked list is referenced by node //postcondition: list is displayed. list is unchanged if (node!= null) { // write the first item System.out.println(node.getItem()); // write the rest of the list writelist(node.getnext()); 7

Recursive backward traversal n We had two ways for recursively traversing a string backwards: q Write the last character of the string s q Write string s minus its last character backward And q Write string s minus its first character backward q Write the first character of string s Recursive backward traversal n Translated to our problem: q write the last node of the list q write the list minus its last node backward And q write the list minus its first node backward q write the first node of the list Which of these strategies is better for linked lists? 8

Recursive backward traversal private static void writelistbackward (Node node) { //precondition: linked list is referenced by node //postcondition: list is displayed. list is unchanged if (node!= null) { // write the rest of the list writelistbackward(node.getnext()); // write the first item System.out.println(node.getItem()); Variations n Circular linked list n Doubly linked list n What are the advantages and disadvantages of a doubly linked list? images from: http://en.wikipedia.org/wiki/linked_list 9

Doubly linked list with header node n empty DLL: q header node points at itself n non empty: q header node points at first and last Inner classes n n n n Inner class: defined inside another class If declared private it can t be used by other classes The methods of the inner and outer classes have access to each other s methods and instance variables, even if declared private. Makes the DoublyLinkedList class self-contained. 10