Anda di halaman 1dari 25

CHAPTER 2 : LISTS 2.1 Introduction to Lists 2.1.1 What is Lists? 2 1 1 Wh i Li ? 2.1.2 Types of Lists Sequential Lists 2.2.

1 Implementation 2.2.2 2 2 2 Application Linked Lists 2.3.1 Implementation 231 I l t ti 2.3.2 Application Other types of Lists
Intersession May 2009 1

2.2

2.3

2.4

Introduction to List

What is List ? List is an ordered sequence of the same type elements (example: Telephone directory, students) List provides methods for manipulating elements via their indices ( l (elements index). t i d ) manipulating a specified range of elements, searching for elements access the elements delete the elements

Intersession May 2009

Introduction to List (cont.)


Type of Lists Sequential List q Arrays structure store elements in a contiguous memory

[0]

[1]

[2]

[3]

[4]

Linked List Linked structure store elements anywhere in y memory and linked by a reference/pointer

Intersession May 2009

Sequential List q
Sequential List S i l Li 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)

Intersession May 2009

Sequential list (cont)


The advantages of using Sequential list: g g q Appropriate data structure: when the number of data is predictable predictable. Simple to sort data: sorting can be done easily after all the data has been stored (ascending or descending order).

Intersession May 2009

Sequential List (cont)


Implementation of Sequential List ArrayList y Array-like structure whose objects can grow and shrink in response to a programs storage requirements Java, In Java ArrayList can be accessed from package ( java.util.* ) E l Example: // create an empty list y y ArrayList ListName = new ArrayList( ); //create list with capacity 50 ArrayList ListName = new ArrayList (50);
Intersession May 2009 6

Sequential List(cont.)
Methods in ArrayList (provided by Java package) a. Constructor ArrayList ( ) - construct an empty list Example : ArrayList namelist = new ArrayList ( ) ArrayList ( int initialCapacity) - construct a list with the specified initial capacity Example : ArrayList namelist = new ArrayList ( 50 )
Intersession May 2009 7

Sequential List(cont.)
b. void add (int , Object) insert the specified element ( j ) at the specified location p (object) p Example : namelist.add(2, Ahmad ); namelist add(2 Ahmad); c. boolean add (Object) append th specified element ( bj t) t th end of th li t d the ifi d l t (object) to the d f the list Example : p if (namelist.add(Fatimah)) or namelist.add( Fatimah ); namelist add(Fatimah);
Intersession May 2009 8

Sequential List(cont.)
d. void clear ( ) remove all elements from the list Example : namelist.clear( namelist clear( ); e. boolean contains (Object) return true if the list contains the specified element i i ifi Example : p if(namelist.contains(Fatimah)) statements;

Intersession May 2009

Sequential List(cont.)
e. Object get (int) return the element (object) from the specified location ( j ) p Example : Object data; data = namelist.get(5); f. f boolean i isEmpty ( ) return true if the list is Empty (contains no elements) Example : if (namelist.isEmpty( )) statements;
Intersession May 2009 10

Sequential List(cont.)
g. Object remove (int) remove the element (object) from the specified location, delete ( j ) p , the location and return the element Example : Object data; data = namelist.remove(5); h. Object t (int Object h Obj t set (i t , Obj t ) replace the elements at the specified location with specified element and return the old element Example : Object data; data = namelist set(5 Maisarah); namelist.set(5, Maisarah );
Intersession May 2009 11

Sequential List(cont.)
i. int size ( ) returns the size (number of elements in the list) Example : int numofelem; numofelem = namelist.size( );

or
for (int x=0; x< namelist.size( ); x++) statements; ; j. Void trimToSize ( ) trims the capacity of the list to the exact amount of data in the list Example : namelist.trimToSize( ) li i T Si ( );
Intersession May 2009 12

Sequential List(cont.)
1.2.2 Application of Sequential List (recall the seven basic algorithms!!!) Searching - search a particular element in list. h i l l i li Manipulating - find maximum/minimum element - calculate total and average - count number of data Sorting - Arrange the elements in the list into ascending or descending order Example : p Bubble sort, Insertion Sort, Selection Sort, Binary sort Quicksort, Mergesort, Heapsort etc

Intersession May 2009

13

Sequential List(cont.)
Insertion Sort rearrange the data in certain order by inserting element into its right location Steps to sort into ascending order using insertion sort technique start from second location compare element with previous locations (first location) if second element is less than ( < ) first element then swap else do nothing Look at third location and compare elements with previous locations (second location then first location). Swap elements if necessary continue with next location (fourth and so on) until all data a e so ted are sorted
Intersession May 2009 14

Sequential List(cont.)
Insertion Sort rearrange data in certain order by inserting element into its right location g 0 20 10 10 10 10 10 10 10 1 10 20 20 20 20 20 15 15 2 80 80 80 50 30 30 20 20 3 50 50 50 80 50 50 30 25 4 30 30 30 30 80 80 50 30 5 90 90 90 90 90 90 80 50 6 15 15 15 15 15 15 90 80 7 25 25 25 25 25 25 25 90 Location 1 2 3 3 5 6 7 8

Intersession May 2009

15

Sequential List(cont.)
Insertion Sort program segment using array: for (int i=1; i < item.length; i++) ( g ) { int nextItem = item[i]; for (int j=i-1; j>=0 && item[j] > nextItem ; j--) { item[j+1] = item[j]; item[j] = nextItem; } }

In Java, sorting technique is provided by Collections class (java.util.Collections) void Collections.sort (list) {}
Intersession May 2009 16

Sequential List(cont.)
Insertion Sort to sort a sequential list using ArrayList class with primitive data (created by programmer) import java.util.*; public class PrimitiveSeqListSort{ public static void main(String[] a) { ArrayList Li tB = new A A Li t ListB ArrayList(5); Li t(5) ArrayList sList = new ArrayList(5); for (int k=0; k<10; k++) { int y = k*100; ListB.add(""+y); } System.out.println( Sorted System out println("Sorted list :"); : ); sList = SortList (ListB); // call independent function } //end main } //end PrimitiveSeqListSort
Intersession May 2009 17

Sequential List(cont.)
// independent function/method sort using insertion sort public static ArrayList SortList(ArrayList List) { for (int i=1; i < List size(); i++) List.size(); { Object nexI = List.get(i); int nextItem = Integer.parseInt(nexI.toString()); for (int j=i-1; j> 0; j--) j i 1; j>=0; j ) { Object curI = List.get(j); int currItem = Integer.parseInt(curI.toString()); g p ( g()); if (currItem > nextItem) { List.set(j+1,List.get(j)); List.set(j,nexI); } } } return List; t Li t }
Intersession May 2009 18

Sequential List(cont.)
Application using ArrayList with ADT pp cat o us g ay st t public class Student { private String metric; private double gpa; public Student() { metric = "0000"; gpa = 0.0; } public Student(String m,double gp) { metric = m; gpa = gp; } public String Display( ) { String out2=""; out2 += "\nMetric No:" + metric + " Gpa:" + gpa; return out2; } public String theMetric( ) { return metric;} public double theGpa( ) { return gpa;} } // end of class student
Intersession May 2009 19

Sequential List(cont.)
EXAMPLE : Sequential List using ADT class student

import java.util.*; import j i javax.swing.*; i * public class StudentList{ public static void main(String[] arg) { ArrayList stuList = new ArrayList(5); for(int x=0;x<2;x++) { String nm = JOptionPane.showInputDialog("Enter student s metric:"); student's metric: ); String sgpa = JOptionPane.showInputDialog ("Enter student's cgpa:"); double gpa = Double.parseDouble(sgpa); gp p ( gp ); Student st = new Student(nm,gpa); stuList.add(st); } String out1="", out2="", out3=";
Intersession May 2009 20

Sequential List(cont.)
// Display data in list out1 += PrintContent(stuList); JOptionPane.showMessageDialog(null,out1,"FirstList", JOptionPane.PLAIN_MESSAGE); Student st1 = new Student("2312354", 2.4); Student st2 = new Student("4444455", 3.6); stuList.add(0,st1); ( ) stuList.add(st2); // Display data after insert 2 elements out2 +=PrintContent(stuList); JOptionPane.showMessageDialog(null,out2,"List After insert two data",JOptionPane.PLAIN_MESSAGE);

Intersession May 2009

21

Sequential List(cont.) List(cont )


// Display the best student out3 +=detBestStudent(stuList); JOptionPane.showMessageDialog(null,out3,"BestStudent", JOptionPane.PLAIN_MESSAGE);

// Display the sorting list sList=SortList(stuList); out4 += PrintContent(sList); JOptionPane.showMessageDialog(null,out4,"SortedList", JOptionPane.PLAIN_MESSAGE); JOptionPane PLAIN MESSAGE); } // end main

Intersession May 2009

22

Sequential List(cont.)
// method to print content of array public static String PrintContent(ArrayList List) { String t "" St i out =""; if(List.isEmpty()) out +="List is empty"; else { for(int z=0; z <List.size(); z++) { Object temp = List get(z); List.get(z); Student s = (Student)temp; out += "\nIndex :" + z + s.Display(); } } return out; }
Intersession May 2009 23

Sequential List(cont.)
// method to determine the best student public static String detBestStudent(ArrayList List) { Student s = (Student)List.get(0); (Student)List get(0); double bestGpa = s.theGpa(); Student best = s; for (int k=1; k <List.size(); k++) { Student st = (Student)List.get(k); double gpa = st.theGpa(); gp p () if(gpa > bestGpa) { bestGpa = gpa; best = st; } } return best.Display(); }
Intersession May 2009 24

Sequential List(cont.)
// method to sort student list public static ArrayList SortList(ArrayList List) { for (int i=1; i < List.size(); i++) { Student nexI = (Student)List.get(i); double nextgpa = nexI.theGpa(); for (int j=i-1; j>=0; j--) { Student St d t curI = (St d t)Li t t(j) I (Student)List.get(j); double currItem = curI.theGpa(); If (currItem < nextgpa) { List.set(j+1,List.get(j)); List set(j+1 List get(j)); List.set(j,nexI); } } } return List; } } // end class StudentList
Intersession May 2009 25

Anda mungkin juga menyukai