Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.

Slides:



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

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.
Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Topic 9 The Queue ADT.
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.
Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
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 3 COLLECTIONS Abstract Data Types. 2 A data type consists of a set of values or elements, called its domain, and a set of operators acting on.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
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 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
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 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
Topic 3 The Stack ADT.
Lists Based on content from: Java Foundations, 3rd Edition.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
COS 312 DAY 21 Tony Gauvin. Ch 1 -2 Agenda Questions? Last Capstone Progress reports due April 23 Assignment 6 Due Assignment 7 Posted – Chapters 15,
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – Week 8 Linked List (a reference based.
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.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
CHAPTER 4: Linked Structures
The List ADT.
Chapter 4 Linked Structures.
Lists A List ADT Types of Lists Using ordered lists – Tournament Maker
Top Ten Words that Almost Rhyme with “Peas”
Java Software Structures: John Lewis & Joseph Chase
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Java Software Structures: John Lewis & Joseph Chase
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks, Queues, and Deques
CHAPTER 3: Collections—Stacks
Presentation transcript:

Chapter 8 Lists

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques Define a list abstract data type Demonstrate how a list can be used to solve problems Examine various list implementations Compare list implementations

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-3 Lists Lists are linear collections, like stacks and queues, but are more flexible Adding and removing elements in lists are not restricted by the collection structure That is, they don't have to operate on one end or the other We will examine three types of list collections: ordered lists unordered lists indexed lists

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-4 Ordered Lists The elements in an ordered list are ordered by some inherent characteristic of the elements names in alphabetical order scores in ascending order Therefore, the elements themselves determine where they are stored in the list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-5 figure 8.1 A conceptual view of an ordered list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-6 Unordered Lists There is an order to the elements in an unordered list, but that order is not based on element characteristics The user of the list determines the order of the elements A new element can be put on the front or the rear of the list, or it can be inserted after a particular element already in the list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-7 figure 8.2 A conceptual view of an unordered list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-8 Indexed Lists In an indexed list, elements are referenced by their numeric position in the list Like an unordered list, there is no inherent relationship among the elements The user can determine the order Every time the list changes, the indexes are updated

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-9 figure 8.3 A conceptual view of an indexed list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-10 List Operations There are several operations common to all three list types These include removing elements in various ways and checking the status of the list The key differences between the list types involve the way elements are added

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-11 figure 8.4 The common operations on a list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-12 figure 8.5 The operation particular to an ordered list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-13 figure 8.6 The operations particular to an unordered list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-14 figure 8.7 The operations particular to an indexed list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-15 List Operations As with other collections, we use Java interfaces to define the collection operations Recall that interfaces can be defined via inheritance (derived from other interfaces) We define the common list operations in one interface, then derive three others from it that define the interfaces of the three list types See ListADT.java (page 223)ListADT.java See OrderedListADT.java (page 224)OrderedListADT.java See UnorderedListADT.java (page 224)UnorderedListADT.java See IndexedListADT.java (page 225)IndexedListADT.java

figure 8.8 The various list interfaces

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-17 Tournament Maker Let's use an ordered list to help manage a bowling tournament Teams are ordered by their number of wins in the regular season To create the first round match-ups, the team with the best record is matched against the team with the worst record The second best team is matched agains the second worst team, etc.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-18 figure 8.9 Bowling league team names and number of wins

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-19 figure 8.10 Sample tournament layout for a bowling league tournament

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-20 Tournament Maker A class that represents a team will be Comparable, based on their number of wins See Tournament.java (page 228)Tournament.java See Team.java (page 230)Team.java See TournamentMaker.java (page 231)TournamentMaker.java

figure 8.11 UML description of the Tournament class

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-22 The Josephus Problem In a Josephus problem, a set of elements are arranged in a circle Starting with a particular element, every i th element is removed Processing continues until there is only one element left The question: given the starting point and remove count (i), which element is left? See Josephus.java (page 235)Josephus.java

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-23 figure 8.12 UML description of the Josephus program

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-24 Implementing Lists with Arrays An array implementation of a list could follow strategies similar to those used for a queue We could fix one end of the list at index 0 and shift as needed when an element is added or removed Or we could use a circular array to avoid the shift at one end However, there is no avoiding a shift when an element in the middle is added or removed Let's examine the fixed version

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-25 figure 8.13 An array implementation of a list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-26 The remove Operation // // Removes and returns the specified element. // public Object remove (Object element) { Object result; int index = find (element); if (index == NOT_FOUND) throw new NoSuchElementException("list"); result = list[index]; // shift the appropriate elements for (int scan=index; scan < rear; scan++) list[scan] = list[scan+1]; rear--; list[rear] = null; return result; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-27 The find Method // // Returns the array index of the specified element, or the // constant NOT_FOUND if it is not found. // private int find (Object 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; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-28 The contains Operation // // Returns true if this list contains the specified element. // public boolean contains (Object target) { return (find(target) != NOT_FOUND; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-29 The add Operation (ordered list) // // Adds the specified Comparable element to the list, keeping // the elements in sorted order. // public void add (Comparable element) { if (size() == list.length) expandCapacity(); int scan = 0; while (scan 0) scan++; for (int scan2=rear; scan2 > scan; scan2--) list[scan2] = list[scan2-1] list[scan] = element; rear++; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-30 Implementing lists with links As with stack and queue, we can implement a list collection with an underlying linked list All operations can be accomplished using techniques similar to ones we've already explored However, let's examine the remove operation with this traditional approach, then alter the underlying representation for comparison

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-31 The remove Operation // // 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 EmptyListException if the list is empty. Throws a // NoSuchElementException if the specified element is not found // on the list. // public Object remove (Object targetElement) throws EmptyCollectionException, ElementNotFoundException { if (isEmpty()) throw new EmptyCollectionException ("List"); boolean found = false; LinearNode previous = null; LinearNode current = head;

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-32 remove Continued 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; else if (current.equals (head)) head = current.getNext(); else

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-33 remove Continued if (current.equals (tail)) { tail = previous; tail.setNext(null); } else previous.setNext(current.getNext()); count--; return current.getElement(); }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-34 Doubly Linked Lists A doubly linked list has two references in each node, one to the next element in the list and 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 There is, however, a bit more overhead when managing the list See DoubleNode.java (page 245)DoubleNode.java

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-35 figure 8.14 A doubly linked list

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-36 The remove Operation revisited // // Removes and returns the specified element. // public Object remove (Object targetElement) throws ElementNotFoundException { Object result; DoubleNode nodeptr = find (element); if (nodeptr == null) throw new ElementNotFoundException ("list"); result = nodeptr.getElement(); // check to see if front or rear if (nodeprt == front) result = this.removeFirst();

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-37 The remove Operation revisited else if (nodeptr == rear) result = this.removeLast(); else { nodeptr.getNext().setPrevious(nodeptr.getPrevious()); nodeptr.getPrevious().setNext(nodeptr.getNext()); count--; } return result; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-38 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) In particular situations, the frequency of the need for particular operations may guide the use of one approach over another