The List ADT.

Slides:



Advertisements
Similar presentations
Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
Advertisements

Linked Lists Linear collections.
Chapter 15 Lists. Chapter Scope Types of list collections Using lists to solve problems Various list implementations Comparing list implementations Java.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
CHAPTER 7 Queues.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
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.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Topic 3 The Stack ADT.
Lists Based on content from: Java Foundations, 3rd Edition.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Chapter 6 Lists Modified. Chapter Scope Types of lists Using lists to solve problems Various list implementations Comparing list implementations 6 - 2Java.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Linked Data Structures
Chapter 16: Linked Lists.
Iterators.
CSE 1342 Programming Concepts
The Tree ADT.
Stack: a Linked Implementation
Elementary Data Structures
CHAPTER 4: Linked Structures
Review Array Array Elements Accessing array elements
Chapter 4 Linked Structures.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Data Structure By Amee Trivedi.
Lists A List ADT Types of Lists Using ordered lists – Tournament Maker
Storage Strategies: Dynamic Linking
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Queues Queues Queues.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Chapter 6 Lists.
Top Ten Words that Almost Rhyme with “Peas”
structures and their relationships." - Linus Torvalds
Java Software Structures: John Lewis & Joseph Chase
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
List Data Structure.
Java Collections Framework
Java Software Structures: John Lewis & Joseph Chase
Outline Late Binding Polymorphism via Inheritance
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Example: LinkedSet<T>
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Arrays.
Chapter 20 Binary Search Trees
Data Structures & Programming
CHAPTER 3: Collections—Stacks
Presentation transcript:

The List ADT

Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations Compare list implementations

Lists A list is a linear collection, like a stack and queue, but more flexible: adding and removing elements from a list does not have to happen at one end or the other We will examine three types of list collections: ordered lists unordered lists indexed lists

Ordered Lists Ordered list: Its elements are ordered by some inherent characteristic of the elements Examples: Names in alphabetical order Numeric scores in ascending order So, the elements themselves determine where they are stored in the list

Conceptual View of an Ordered List front rear 16 23 29 40 51 67 88 New values must be inserted so that the ordering of the list is maintained 58

Unordered Lists Unordered list : the order of the elements in the list is not based on a characteristic of the elements, but is determined by the user of the list A new element can be put on the front of the list or on the rear of the list or after a particular element already in the list Examples: shopping list, to-do list, …

Conceptual View of an Unordered List front rear New values can be inserted anywhere in the list

Indexed Lists Indexed list: elements are referenced by their numeric position in the list, called its index It is the position in the list that is important, and the user can determine the order that the items go in the list Every time the list changes, the position (index) of an element may change Example: current first-place holder in the bobsled race

Conceptual View of an Indexed List front rear 1 2 3 4 5 6 7 New values can be inserted at any position in the list

List Operations Operations common to all list types include : Adding/Removing elements in various ways Checking the status of the list (isEmpty, size) Iterating through the elements in the list (more on this later!) The key differences between the list types involve the way elements are added: To an ordered list? To an unordered list? To an indexed list?

The Common Operations on a List Description removeFirst Removes the first element from the list removeLast Removes the last element from the list remove Removes a particular element from the list first Examines the element at the front of the list last Examines the element at the rear of the list contains Determines if a particular element is in the list isEmpty Determines whether the list is empty size Determines the number of elements in the list iterator Returns an iterator for the list’s elements toString Returns a string representation of the list

Operation Particular to an Ordered List Description add Adds an element to the list (in the correct place)

Operations Particular to an Unordered List Description addToFront Adds an element to the front of the list addToRear Adds an element to the rear of the list addAfter Adds an element after a particular element already in the list

Operations Particular to an Indexed List Description add Adds an element at a particular index in the list set Sets the element at a particular index in the list overwriting any element that was there get Returns a reference to the element at the specified index indexOf Returns the index of the specified element remove Removes and returns the element at a particular index

List Operations We use Java interfaces to formally define the operations on the lists, as usual Note that interfaces can be defined via inheritance (derived from other interfaces) Define the common list operations in one interface See ListADT.java Derive the thee others from it see OrderedListADT.java see UnorderedListADT.java see IndexedListADT.java

ListADT Interface import java.util.Iterator; public interface ListADT<T> { // Removes and returns the first element from this list public T removeFirst ( ); // Removes and returns the last element from this list public T removeLast ( ); // Removes and returns the specified element from this list public T remove (T element); // Returns a reference to the first element on this list public T first ( ); // Returns a reference to the last element on this list public T last ( ); // cont’d..

// ..cont’d // Returns true if this list contains the specified target element public boolean contains (T target); // Returns true if this list contains no elements public boolean isEmpty( ); // Returns the number of elements in this list public int size( ); // Returns an iterator for the elements in this list public Iterator<T> iterator( ); // Returns a string representation of this list public String toString( ); }

OrderedList ADT public interface OrderedListADT<T> extends ListADT<T> { // Adds the specified element to this list at the proper location public void add (T element); }

UnorderedListADT public interface UnorderedListADT<T> extends ListADT<T> { // Adds the specified element to the front of this list public void addToFront (T element); // Adds the specified element to the rear of this list public void addToRear (T element); // Adds the specified element after the specified target public void addAfter (T element, T target); }

IndexedListADT public interface IndexedListADT<T> extends ListADT<T> { // Inserts the specified element at the specified index public void add (int index, T element); // Sets the element at the specified index public void set (int index, T element); // Adds the specified element to the rear of this list public void add (T element); // Returns a reference to the element at the specified index public T get (int index); // Returns the index of the specified element public int indexOf (T element); // Removes and returns the element at the specified index public T remove (int index); }

Discussion Note that the add method in the IndexedList ADT is overloaded So is the remove method Why? Because there is a remove method in the parent ListADT This is not overriding, because the parameters are different

List Implementation using Arrays Container is an array Fix one end of the list at index 0 and shift as needed when an element is added or removed Is a shift needed when an element is added at the front? somewhere in the middle? at the end? Is a shift needed when an element is removed from the front? from somewhere in the middle? from the end?

An Array Implementation of a List An array-based list ls with 4 elements 1 2 3 4 5 … ? ? ? ls list 4 rear

The remove( ) operation //----------------------------------------------------------------- // Removes and returns the specified element. public T remove (T element) throws ElementNotFoundException { T result; int index = find (element); // uses helper method find if (index == NOT_FOUND) throw new ElementNotFoundException("list"); result = list[index]; rear--; // shift the appropriate elements for (int scan=index; scan < rear; scan++) list[scan] = list[scan+1]; list[rear] = null; return result; } The remove( ) operation

The find( ) helper method //----------------------------------------------------------------- // Returns the array index of the specified element, // or the constant NOT_FOUND if it is not found. private int find (T target) { int scan = 0, result = NOT_FOUND; boolean found = false; if (! isEmpty( )) while (! found && scan < rear) if (target.equals(list[scan]) found = true; else scan++; if (found) result = scan; return result; } The find( ) helper method

The contains( ) operation //----------------------------------------------------------------- // Returns true if this list contains the specified element. public boolean contains (T target) { return (find(target) != NOT_FOUND); //uses helper method find } The contains( ) operation

The add( ) operation of ArrayOrderedList //----------------------------------------------------------------- // Adds the specified Comparable element to the list, // keeping the elements in sorted order. public void add (T element) { if (size( ) == list.length) expandCapacity( ); Comparable<T> temp = (Comparable<T>)element; int scan = 0; while (scan < rear && temp.compareTo(list[scan]) > 0) scan++; for (int scan2=rear; scan2 > scan; scan2--) list[scan2] = list[scan2-1] list[scan] = element; rear++; } The add( ) operation of ArrayOrderedList

The Comparable Interface For an ordered list, the actual class for the generic type T must have a way of comparing elements so that they can be ordered So, it must implement the Comparable interface, i.e. it must define a method called compareTo But, the compiler does not know whether or not the class that we use to fill in the generic type T will have a compareTo method

The Comparable Interface So, to make the compiler happy: Declare a variable that is of type Comparable<T> Convert the variable of type T to the variable of type Comparable<T> Comparable<T> temp = (Comparable<T>)element; Note that an object of a class that implements Comparable can be referenced by a variable of type Comparable<T>

List Implementation Using Arrays, Method 2: Circular Arrays Recall circular array implementation of queues Exercise: implement list operations using a circular array implementation

List Implementation Using Links We can implement a list collection with a linked list as the container Implementation uses techniques similar to ones we've used for stacks and queues We will first examine the remove operation for a singly-linked list implementation Then we’ll look at the remove operation for a a doubly-linked list, for comparison

The remove( ) operation (singly-linked list) //----------------------------------------------------------------- // Removes the first instance of the specified element // from the list, if it is found in the list, and returns a // reference to it. Throws an ElementNotFoundException // if the specified element is not found on the list. public T remove (T targetElement) throws ElementNotFoundException { if (isEmpty( )) throw new ElementNotFoundException ("List"); boolean found = false; LinearNode<T> previous = null LinearNode<T> current = head; // cont’d.. The remove( ) operation (singly-linked list)

The remove( ) operation (cont’d) while (current != null && !found) if (targetElement.equals (current.getElement( ))) found = true; else { previous = current; current = current.getNext( ); } if (!found) throw new ElementNotFoundException ("List"); if (size( ) == 1) head = tail = null; if (current.equals (head)) head = current.getNext( ); // cont’d The remove( ) operation (cont’d)

The remove( ) operation (cont’d) if (current.equals (tail)) { tail = previous; tail.setNext(null); } else previous.setNext(current.getNext( )); count--; return current.getElement( ); The remove( ) operation (cont’d)

Doubly Linked Lists A doubly linked list has two references in each node: One to the next element in the list One to the previous element This makes moving back and forth in a list easier, and eliminates the need for a previous reference in particular algorithms Disadvantage? a bit more overhead when managing the list

Implementation of a Doubly-Linked List A doubly-linked list dl with 4 elements . . rear dl front 4 count

See DoubleNode.java We can then implement the ListADT using a doubly linked list as the container Following our usual convention, this would be called DoublyLinkedList.java

The remove( ) operation (doubly-linked list) //----------------------------------------------------------------- // Removes and returns the specified element. public T remove (T element) throws ElementNotFoundException { T result; DoubleNode<T> nodeptr = find (element); // uses helper method find for doubly-linked list if (nodeptr == null) throw new ElementNotFoundException ("list"); result = nodeptr.getElement( ); // check to see if front or rear if (nodeptr == front) result = this.removeFirst( ); // cont’d.. The remove( ) operation (doubly-linked list)

The remove( ) operation (cont’d) else if (nodeptr == rear) result = this.removeLast( ); { nodeptr.getNext( ).setPrevious(nodeptr.getPrevious( )); nodeptr.getPrevious( ).setNext(nodeptr.getNext( )); count--; } return result; The remove( ) operation (cont’d)

Analysis of List Implementations In both array and linked implementations, many operations are similar in efficiency Most are O(1) , except when shifting or searching need to occur, in which case they are order O(n) Exercise: determine the time complexity of each operation In particular situations, the frequency of the need for particular operations may guide the use of one approach over another