Computer Science and Engineering

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Queues and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
The List ADT Textbook Sections
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
LIST( using dynamic memory allocation). Introduction  We need dynamic Memory Allocation, when the data is dynamic in nature.  That is, the number of.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Using Linked Lists in java.util Package Linked Lists in java.util package: Introduction. LinkedList class of java.util package: Introduction. LinkedList.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
LinkedList Many slides from Horstmann modified by Dr V.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Data Structures Lakshmish Ramaswamy. Removing Element public Object remove(int index){ Object retobj; if(index size){ throw IndexOutOfBoundsException.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Advanced Programming 7/10/20161 Ananda Gunawardena.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Java Collections CHAPTER 3-February-2003
Chapter 5: The List Abstract Data Type
Lecture 6 of Computer Science II
Java Methods Lists and Iterators Object-Oriented Programming
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists Chapter 5 (continued)
CS2006- Data Structures I Chapter 5 Linked Lists I.
John Hurley Cal State LA
Sequences and Iterators
Data Structures Lakshmish Ramaswamy.
Implementing ArrayList Part 1
public class StrangeObject { String name; StrangeObject other; }
Chapter 17 Object-Oriented Data Structures
Introduction to Data Structures
Linked Lists.
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Collections in Java The objectives of this lecture are:
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Problem Understanding
ArrayLists 22-Feb-19.
Collections Framework
Linked Lists & Iterators
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Linked Lists Chapter 5 (continued)
Recursive Objects Singly Linked Lists.
TCSS 143, Autumn 2004 Lecture Notes
Matrix and Linked List ADTs
Part of the Collections Framework
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Presentation transcript:

Computer Science and Engineering Linked Structures Computer Science and Engineering 2/21/2019 B.Ramamurthy

Linked List We will study: The concept of linked structure (list) Java Linked list State design pattern-based realization of a linked list 2/21/2019 B.Ramamurthy

Consider an Array Array is fixed size (non-mutable, static) Array insert is difficult Default indexing 0..n-1, predefined sequence of elements Allows random access Insert here … N-1 0 1 2 3 4 2/21/2019 B.Ramamurthy

Linked List A linked list is an empty list or collection of a number of nodes, each containing one data element and one (or more) links to other nodes. Linked lists permit dynamic insertion and removal of nodes at any point in the list. Linked lists do not allow random access. The simplest kind of linked list is a singly linked list, which has one link per node. This link points to the next node in the list, or to a null value if it is the last node. This kind of list allows sequential access to elements. It is used where the size and structure of the data needs to be flexible and dynamic. 2/21/2019 B.Ramamurthy

Varieties of Linked List Simple linked list Linked list with special node(s) for head and/or tail Null terminator Sentinel as terminator Single link, double link, multi-link, circular linked Pointer to first and last node 2/21/2019 B.Ramamurthy

A node of linked list public class Node { Object item; Node next; } some times you have explicit representation for a head and or tail. 2/21/2019 B.Ramamurthy

Node Class Node // data Object item; Node next; // methods public Node() public Object getElement() public Node getNext() public void setElement(Object newItem) public void setNext(Node newNext) public boolean equals(Node other) public String toString() 2/21/2019 B.Ramamurthy

Linked List Operations Construct Insert (first, last, anywhere) Delete (first, last, anywhere) Size Search for an item Process: Traverse, iterate to do something 2/21/2019 B.Ramamurthy

JDK1.4 Linked List Object AbstractCollection Collection AbstractList AbstractSequentialList List, java.io.Serializable, java.lang.Cloneable LinkedList 2/21/2019 B.Ramamurthy

LinkedList’s inheritance and related classes Most of the linked list functionality is specified in the interface List. Serializable defines the disk archiving (object read/write) functionality. Cloneable defines the cloning ability. LinkedList class defines the variables needed for storing the header and size. A ListIterator class offers the Iterator facility. 2/21/2019 B.Ramamurthy

Linked List class //constructors add (index I, Object o); // inserts add(Object o); //appends addFirst(Object o); addLast(Object o); Object remove(index i); boolean remove(Object o); Object removeFirst(); Object removeLast(); Object get(index i); Object getFirst(); Object getLast(); Object set(index i, Object o); returns replaced Object 2/21/2019 B.Ramamurthy

Linked List Class (contd.) ListIerator listIterator(index i); boolean contains(Object o); clear(); int indexOf(Object o); int lastIndexOf(Object o); // first occurrence or –1 Object[] toarray(); int size(); 2/21/2019 B.Ramamurthy

2/21/2019 B.Ramamurthy

java.util.LinkedList Empty List previous next object header: Doubly linked list with a special node header. For an empty list all three fields point to the header itself 2/21/2019 B.Ramamurthy

Java.util.LinkedList Non-Empty List Object null previous next Header 2/21/2019 B.Ramamurthy

remove(Object o) Locate the Entry e with Object o (Object can be null). If no such Entry throw an exception. Update pointers: 3.1 Update previous entry’s next to e’s next 3.2 Update e’s next’s previous to e’s previous 3.3 update size; size = size –1 2/21/2019 B.Ramamurthy

Remove an entry Object null previous next Header Entry e Step 3.2 2/21/2019 B.Ramamurthy

Remove an entry null previous next Header Step 2 Object Object Object Entry e Step 1 2/21/2019 B.Ramamurthy

Java Code for Step 3 of remove … e.previous.next = e.next; //3.1 e.next.previous = e.previous; //3.2 size--; //3.3 2/21/2019 B.Ramamurthy

add(int index, Object o) Inserts an Entry of Object o before Entry at the given index. Retrieve the Entry e at the given index. Construct an Entry out of Object o and add it before e; 2.1 Construct Entry x with o as Object, e as next and e.previous as previous. 2.2 Update next pointer of x’s previous Entry 2.3 Update previous pointer of x’s next Entry. 2.4 size++ 2/21/2019 B.Ramamurthy

add(int index, Object o): Step 2 header previous next Entry e at given index Object o previous next Step 2.1 Entry x 2/21/2019 B.Ramamurthy

add(int index, Object o): Step 2 Let e be the entry at the given index. … Entry x = new Entry(o, e, e.previous); //step 2.1 x.previous.next = x; //step 2.2 x.next.previous = x; //step 2.3 size++; 2/21/2019 B.Ramamurthy

add(int index, Object o): Step 2 header previous next header Object Object Object Entry e at given index 2.2 2.3 Object o previous next Step 2.1 2/21/2019 B.Ramamurthy

Iterator Pattern When you want to access elements of a collection sequentially without the knowledge of the underlying representation. Ex 1: MineSweeper: surroundIterator that presents you list of all valid surrounding cells (0-8) for sequential access Ex 2: Maze: sequence of valid neighbors Ex 3: winterMonths: sequence of all winter months from a list of all months; can be defined for polymorphic dispatch for various types of geographic zones A simple iterator interface: hasNext(), next(), hasPrevious(), previous(), currentElem() 2/21/2019 B.Ramamurthy

Java Linked List Iterator Implemented as an inner class. Review: an inner class is a class embedded within another class. Scope is limited to the embedding class. In fact, LinkedList has two inner classes: ListItr and Entry. A listIterator(int index) : index specifies the starting point in the list for generating the elements of the iterator. You may request a partial list Iterator. 2/21/2019 B.Ramamurthy

Using a List Problem: Make a list of all routes in city map. class Route { int origin; int dest; Route (int a, int b){ }} LinkedList map = new LinkedList(); map.add( route(23,45)); etc. //print me a list of all train routes // assume a trainIterator is available //think about the implementation of trainIterator 2/21/2019 B.Ramamurthy

Using an Iterator // generate the iterator ListIterator tR = map.trainIterator(); while (tr.hasNext()) System.out.println(tr.next()); 2/21/2019 B.Ramamurthy