1 / 22

Chapter 2 : List ADT Part I – Sequential List

Chapter 2 : List ADT Part I – Sequential List. Overview. 2.1 Introduction to Lists 2.1.1 What is Lists? 2.1.2 Types of Lists 2.2 Sequential Lists 2.2.1 Implementation 2.2.2 Application 2.3 Linked Lists 2.3.1 Implementation 2.3.2 Application 2.4 Other types of Lists . Introduction.

roymorrison
Download Presentation

Chapter 2 : List ADT Part I – Sequential List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 : List ADTPart I – Sequential List

  2. Overview 2.1 Introduction to Lists 2.1.1 What is Lists? 2.1.2 Types of Lists 2.2 Sequential Lists 2.2.1 Implementation 2.2.2 Application 2.3 Linked Lists 2.3.1 Implementation 2.3.2 Application 2.4 Other types of Lists

  3. Introduction • List is an ordered sequence of the same type elements (example: telephone directory, students) • List provides methods for : a) manipulating a specified range of elements, b) searching for elements c) access the elements d) delete the elements

  4. [0] [1] [2] [3] [4] Introduction • List is divided by 2 types: a) Sequential List • Arrays structure – store elements in a contiguous memory b) Linked List • Linked structure – store elements anywhere in memory and linked by a reference/pointer

  5. CSC258 DATA STRUCTURE

  6. CSC258 DATA STRUCTURE

  7. Sequential List • Sequential list contains a number of elements that is less than or equal to its capacity. • Java provides a resizable-array implementation for a sequential list known as ArrayList (dynamic application) • The advantages of using Sequential list: a) Appropriate data structure - when the number of data is predictable. b) Simple to sort data - sorting can be done easily after all the data has been stored (ascending or descending order).

  8. Sequential List (cont …) • Implementation of : • ArrayList – like array structure whose objects can grow and shrink in response to a program’s storage requirements • In Java, ArrayList can be accessed from package ( java.util.* ) • Example: // create an empty list ArrayList ListName = new ArrayList( ); //create list with capacity 50 ArrayList ListName = new ArrayList (50); ArrayList Sequential List

  9. ArrayList (cont …) • The main methods in ArrayList (provided by Java package) are : • Constructor construct a list • Add adds a new node • Set updates the contents of a node • Remove removes a node • Get returns the value at a specified index • IndexOf returns the index in the list of a specified element • Sizereturns the size (number of elements in the list) • isEmpty return true if the list is Empty (contains no elements)

  10. ArrayList (cont …) a) Constructor ArrayList ( ) - construct an empty list Example : ArrayList stuList= new ArrayList ( ) ArrayList ( int initialCapacity) - construct a list with the specified initial capacity Example : ArrayList stuList = new ArrayList (10)

  11. ArrayList (cont …) b) Add Void add (int , Object) - insert the specified element (object) at the specified location Example : stuList.add (3,”Fahrin”); c) Set Object set (int , Object ) Example : stuList.set (7, “Anuar”);

  12. ArrayList (cont …) d) Remove Object remove (int) Example : stuList.remove (5); e) Get Object get (int) Example : stuList.get (7);

  13. ArrayList (cont …) f) IndexOf Int indexOf (Object); Example : stuList.IndexOf(“zaki”); g) Size Example : int stu; stu= stuList.size ( ); or for (int x=0; x< stuList.size ( ); x++)

  14. ArrayList (cont …) h) isEmpty Example : if stuList.isEmpty ( )

  15. ArrayList (cont …)

  16. ArrayList (cont …)

  17. ArrayList (cont …) • Determine the number of elements ArrayList fruits = new ArrayList(); fruits.add (“apples”); fruits.add (“oranges”); fruits.add (“durian”); fruits.add (“apples”); System.out.println (fruits.size());

  18. ArrayList (cont …) • Returns the elements at specified index ArrayList fruits = new ArrayList(); fruits.add (“apples”); fruits.add (“oranges”); fruits.add (“durian”); fruits.add (“apples”); System.out.println (fruits.get(2));

  19. ArrayList (cont …) • Replaces the element at the specified index in this ArrayList ArrayList fruits = new ArrayList(); fruits.add (“apples”); fruits.add (“oranges”); fruits.add (“durian”); fruits.add (“apples”); System.out.println (fruits.set(2, “banana”));

  20. ArrayList (cont …) • Example of sequential list using ArrayList class with primitive data import java.util.*; class Erase { public static void main(String[] args) { ArrayList names = new ArrayList(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); System.out.println(names.size()); names.remove (2); System.out.println(names); } }

  21. Exercises • Write Java program segments to: • Create two sequential lists named liststudent1 and liststudent2 • Create two sequential lists named Book1 and Book2 • Insert 20 students into liststudent1 • Store 10 floating point numbers into ArrayList named NumberList • Insert 10 books into Book1 • Search for the student who has the studId = 188 from listStudent1

  22. The End

More Related