Dr. Liu1 Chapter 3 Array-Based Lists. Dr. Liu2 Chapter Objectives Learn about lists Explore how various operations, such as search, insert, and remove,

Slides:



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

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Why not just use Arrays? Java ArrayLists.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Stacks, Queues, and Linked Lists
Linear Lists – Array Representation
DATA STRUCTURES USING C++ Chapter 5
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Chapter 10: Applications of Arrays and Strings J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second.
Data Structures Using Java1 Chapter 3 Array-Based Lists.
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Generics, Lists, Interfaces
Array-Based Lists List Length of a list
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Chapter 15: Generic Methods, Classes, and Array-Based Lists
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Data Structures Using Java1 Chapter 8 Search Algorithms.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Data Structures Using C++ 2E
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Data Structures Using C++1 Chapter 9 Search Algorithms.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
Applications of Arrays (Searching and Sorting) and Strings
ArrayList, Multidimensional Arrays
Chapter 11 Arrays Continued
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Chapter 14: Searching and Sorting
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 3 Pointers and Array-Based Lists Dr. Youssef Harrath
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
Collections Dwight Deugo Nesa Matic
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Click to edit Master text styles Stacks Data Structure.
Vectors Holds a set of elements, like an array
Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues: Implemented using Arrays
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Collections Framework
Array-Based Lists & Pointers
General List.
Presentation transcript:

Dr. Liu1 Chapter 3 Array-Based Lists

Dr. Liu2 Chapter Objectives Learn about lists Explore how various operations, such as search, insert, and remove, on lists are implemented Learn how to design and implement a generic class to process various types of lists Become aware of the class Vector

Dr. Liu3 Array-Based Lists List: A collection of elements of the same type Length of list is number of elements in list

Dr. Liu4 Operations Performed on a List Create the list: initialize to an empty state Determine whether the list is empty Determine whether the list is full Find the size of the list Destroy (clear) the list Determine whether an item is the same as a given list element

Dr. Liu5 Operations Performed on a List Insert an item in the list at the specified location Remove an item from the list at the specified location Replace an item at the specified location with another item Retrieve an item from the list at the specified location Search the list for a given item

Dr. Liu6 Type of List Elements class Object directly or indirectly becomes a superclass of every Java class, user defined or built in Reference variable of Object type can refer to any object of any class class DataElement, superclass of every class specifying data type of list elements Abstract methods of class DataElement: equals, compareTo, makeCopy, and getCopy

Dr. Liu7 Type of List Elements

Dr. Liu8 Type of List Elements class IntElement used when list of integers is manipulated class DoubleElement used when list of decimal numbers is manipulated class StringElement, designed in chapter, used when list of strings is manipulated

Dr. Liu9 class IntElement

Dr. Liu10 class StringElement

Dr. Liu11 class ArrayListClass An abstract class Is superclass of the classes that implement a list Has three instance variables –length: specifies the number of elements currently in the list –maxSize: specifies the maximum number of elements that can be processed by the list –list: an array of reference variables

Dr. Liu12 class ArrayListClass

Dr. Liu13 Definitions of Nonabstract Methods of ArrayListClass public boolean isEmpty() { return (length == 0); } public boolean isFull() { return (length == maxSize); } public int listSize() { return length; } public int maxListSize() { return maxSize; }

Dr. Liu14 Definitions of Nonabstract Methods of ArrayListClass public void print() { for(int i = 0; i < length; i++) System.out.print(list[i] + ); System.out.println(); } public boolean isItemAtEqual(int location, DataElement item) { return (list[location].equals(item)); }

Dr. Liu15 Definitions of Nonabstract Methods of ArrayListClass public void insertAt(int location, DataElement insertItem) { if(location = maxSize) System.err.println(The position of the item to + be inserted is out of range); else if(length >= maxSize) //list is full System.err.println(Cannot insert in a full list.); else { for(int i = length; i > location; i--) list[i] = list[i - 1]; //move the elements down list[location] = insertItem.getCopy(); //insert the //item at the specified position length++; //increment the length } }//end insertAt

Dr. Liu16 Definitions of Nonabstract Methods of ArrayListClass public void insertEnd(DataElement insertItem) { if(length >= maxSize) //the list is full System.err.println(Cannot insert in a full list.); else { list[length] = insertItem.getCopy(); //insert item //at end length++; //increment the length } }//end insertEnd

Dr. Liu17 Definitions of Nonabstract Methods of ArrayListClass public void removeAt(int location) { if(location = length) System.err.println(The location of the item to + be removed is out of range.); else { for(int i = location; i < length - 1; i++) list[i] = list[i + 1]; list[length - 1] = null; length--; } }//end removeAt

Dr. Liu18 Definitions of Nonabstract Methods of ArrayListClass public DataElement retrieveAt(int location) { if(location = length) { System.err.println("The location of the item to be " + "retrieved is out of range."); return null; } else return list[location].getCopy(); }//end retrieveAt

Dr. Liu19 Definitions of Nonabstract Methods of ArrayListClass public void replaceAt(int location, DataElement repItem) { if(location = length) System.err.println(The location of the item to + be replaced is out of range.); else list[location].makeCopy(repItem); }//end replaceAt public void clearList() { for(int i = 0; i < length; i++) list[i] = null; length = 0; System.gc(); }//end clearList

Dr. Liu20 Definition of ArrayListClass public abstract class ArrayListClass { protected int length; //to store the length //of the list protected int maxSize; //to store the maximum //size of the list protected DataElement[] list; //array to hold //list elements //Place the definitions of the instance // methods and abstract methods here. }

Dr. Liu21 Example testProg.cpp arrayListType intList(100); intList.insertAt(counter, number); intList.print(); intList.remove(number);

Dr. Liu22 Array-based List example p.182 Define class arrayListType Bool isEmpty(); Bool isFull(); Int listSize(); Bool isItemAtEqual(…); Void insertAt(….); RemoveAt(…)

Dr. Liu23 Search Necessary components to search a list: –Array containing the list –Length of the list –Item for which you are searching After search completed: –If item found, report success, return location in array –If item not found, report failure

Dr. Liu24 Search public int seqSearch(DataElement searchItem) { int loc; boolean found = false; for(loc = 0; loc < length; loc++) if(list[loc].equals(searchItem)) { found = true; break; } if(found) return loc; else return -1; }//end seqSearch

Dr. Liu25 Insert and Remove Insert –Inserts a new item in the list –Uses method seqSearch to determine whether insertItem is already in list Remove –deletes an item from the list –uses the methods seqSearch and removeAt to remove an item from the list

Dr. Liu26 Insert public void insert(DataElement insertItem) { int loc; if(length == 0) //list is empty list[length++] = insertItem; //insert the item and //increment the length else if(length == maxSize) System.err.println(Cannot insert in a full list.); else { loc = seqSearch(insertItem); if(loc == -1) //the item to be inserted //does not exist in the list list[length++] = insertItem.getCopy(); else System.err.println(The item to be inserted is + already in the list. No + duplicates are allowed.); } }//end insert

Dr. Liu27 Remove public void remove(DataElement removeItem) { int loc; if(length == 0) System.err.println(Cannot delete from an empty list.); else { loc = seqSearch(removeItem); if(loc != -1) removeAt(loc); else System.out.println(The item to be deleted is + not in the list.); } }//end remove

Dr. Liu28 Time Complexity of List Operations

Dr. Liu29 Vectors Class Vector can be used to implement a list Unlike array, size of Vector object can grow/shrink during program execution Do not need to worry about number of data elements in vector We will discuss this topic in different time.

Dr. Liu30 Programming Example: Polynomials Purpose: To design and implement the class Polynomial to perform various polynomial operations in a program Program implements the following polynomial operations: 1. Evaluate a polynomial at a given value 2. Add polynomials 3. Subtract polynomials 4. Multiply polynomials

Dr. Liu31 Programming Example: Polynomials

Dr. Liu32 Programming Example: Polynomials

Dr. Liu33 Chapter Summary Operations performed on a list Type of list elements Abstract class DataElement Classes IntElement, DoubleElement, StringElement class ArrayListClass –Definitions of Nonabstract Methods of ArrayListClass –Definition of ArrayListClass

Dr. Liu34 Chapter Summary Implementations of search, insert and remove Time Complexity of List Operations Vectors –Members of the class Vector, will discuss in next chapter. Programming examples – ArrayList Programming Assignment – Exercise 6 : modify progmming example to include RemoveAll function.