1 / 138

Linear List

Algoritma & Struktur Data. Linear List. Teknik Informatika Universitas Muhammadiyah Malang SP - 201 3. Representasi Linear List dengan Array dan Linked list. Tujuan Instruksional. Mahasiswa mampu : Memahami struktur data dari linear list

lea
Download Presentation

Linear 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. Algoritma & Struktur Data Linear List Teknik Informatika Universitas Muhammadiyah Malang SP - 2013

  2. Representasi Linear List dengan Array dan Linked list

  3. Tujuan Instruksional • Mahasiswa mampu : • Memahami struktur data dari linear list • Mampu merepresentasikan linear list menggunakan array dan linked list • Mengimplementasikan linear list ke dalam program

  4. Struktur Data • Struktur data terdiridariobyek data danoperasi-operasi. • Obyek data adalahsekumpulan instance (model/sampling) • Contohobyek data : integer = {0, +1, -1, +2, -2, +3, -3, …} daysOfWeek = {S,M,T,W,Th,F,Sa} myDataObject = {apple, chair, 2, 5.2, red, green, Jack}

  5. Struktur Data • Struktur data dibedakanmenjadi 2 jenis : • Linear list direpresentasikandengan : array dan linked list • Struktur data hirarki Direpresentasikandengan : tree dan graph

  6. Linear Lists Disebutjugadenganordered list. Bentuk instance dari linear list : (e0, e1, e2, …, en-1) where ei denotes a list element n >= 0 is finite (list size isn)

  7. Linear Lists L = (e0, e1, e2, e3, …, en-1) relationships e0 is the zero’th (or front) element en-1 is the last element eiimmediately precedes ei+1

  8. Linear List Examples/Instances Students in COP3530 = (Jack, Jill, Abe, Henry, Mary, …, Judy) Exams in COP3530 = (exam1, exam2, exam3) Days of Week = (S, M, T, W, Th, F, Sa) Months = (Jan, Feb, Mar, Apr, …, Nov, Dec)

  9. Struktur data Operations • Operasi : aksi yang dapat dilakukan pada obyek data. • Operasi-operasi yang umum pada obyek data : penambahan elemen, penghapusan elemen, pengaksesan elemen, dll.

  10. Linear List Operations—size() determine list size L = (a,b,c,d,e) size = 5

  11. Linear List Operations—get(theIndex) get element with given index L =(a,b,c,d,e) get(0) = a get(2) = c get(4) = e get(-1) = error get(9) = error

  12. Linear List Operations—indexOf(theElement) determine the index of an element L =(a,b,d,b,a) indexOf(d) = 2 indexOf(a) = 0 indexOf(z) = -1

  13. Linear List Operations—remove(theIndex) remove and return element with given index L = (a,b,c,d,e,f,g) remove(2)returns c and L becomes (a,b,d,e,f,g) index ofd,e,f,andgdecrease by 1

  14. Linear List Operations—remove(theIndex) remove and return element with given index L = (a,b,c,d,e,f,g) remove(-1)=> error remove(20) => error

  15. Linear List Operations—add(theIndex, theElement) add an element so that the new element has a specified index L = (a,b,c,d,e,f,g) add(0,h)=> L = (h,a,b,c,d,e,f,g) index ofa,b,c,d,e,f,andgincrease by 1

  16. Linear List Operations—add(theIndex, theElement) L = (a,b,c,d,e,f,g) add(2,h)=> L = (a,b,h,c,d,e,f,g) index ofc,d,e,f,andgincrease by 1 add(10,h) => error add(-6,h) => error

  17. Latihan Diketahui L=(a,b,c,d). L adalah sebuah array linier list. Tentukan hasil yang didapatkan ketika dilakukan perintah berikut ini : • isEmpty() • size() • get(0), get(2),get(6),get(-3) • indexOf(a), indexOf(c), indexOf(q) • remove(0), remove(2), remove(3) • add(0,e), add(2,f), add(3,g), add(4,h), add(6,h), add(-3,h)

  18. Data Structure Specification • Language independent • Abstract Data Type • Java • Interface • Abstract Class

  19. Linear List Abstract Data Type AbstractDataTypeLinearList { instances ordered finite collections of zero or more elements operations isEmpty(): return trueif the list is empty, falseotherwise size(): return the list size (i.e., number of elements in the list) get(index): return theindexth element of the list indexO f(x): return the index of the first occurrence of xin the list, return -1 ifxis not in the list remove(index): remove and return theindexth element, elements with higher index have their index reduced by 1 add(theIndex, x): insert x as the indexth element, elements with theIndex >= indexhave their index increased by1 output(): output the list elements from left to right }

  20. Linear List as Java Interface An interface may include constants and abstract methods (i.e., methods for which no implementation is provided).

  21. Linear List as Java Interface public interfaceLinearList { public booleanisEmpty(); public int size(); public Object get(int index); public intindexOf(Object elem); public Object remove(int index); public void add(int index, Object obj); public StringtoString(); }

  22. Implementing An Interface public class ArrayLinearList implements LinearList { // code for all LinearList methods must be provided here }

  23. Linear List As An Abstract Class An abstract class may include constants, variables, abstract methods, and nonabstract methods.

  24. Linear List As Java Abstract Class publicabstract class LinearListAsAbstractClass { public abstract boolean isEmpty(); public abstract int size(); public abstract Object get(int index); public abstract int indexOf(Object theElement); public abstract Object remove(int index); public abstract void add(int index, Object theElement); public abstract String toString(); }

  25. Extending A Java Class public class ArrayLinearList extends LinearListAsAbstractClass { // code for all abstract classes must come here }

  26. 0 1 2 3 4 5 6 Linear List Array Representation use a one-dimensional array element[] a b c d e L = (a, b, c, d, e) Store element i of list in element[i].

  27. a b c d e 0 1 2 3 4 5 6 Representation Used In Text put element i of list in element[i] use a variablesizeto record current number of elements size = 5

  28. size = 5 a b c d e size = 6 a g b c d e Add/Remove An Element add(1,g)

  29. Data Type Of Array element[] Data type of list elements is unknown. Define element[]to be of data typeObject. Cannot put elements of primitive data types (int, float, double, char, etc.)into our linear lists.

  30. a b c d e f Increasing Array Length Length of array element[] is 6. First create a new and larger array newArray = new Object[15];

  31. a b c d e f a b c d e f Increasing Array Length Now copy elements from old array to new one.

  32. element[0] a b c d e f Increasing Array Length Finally, rename new array. element = newArray; element.length = 15

  33. Class ArrayLinearList • Merupakan hasil implements dari interface LinearList.

  34. Create An Empty List ArrayLinearList a = new ArrayLinearList(100), b = new ArrayLinearList(), c; LinearList d = new ArrayLinearList(1000), e = new ArrayLinearList(), f;

  35. Using A Linear List System.out.println(a.size()); a.add(0, new Integer(2)); b.add(0, new Integer(4)); System.out.println(a); b.remove(0); if (a.isEmpty()) a.add(0, new Integer(5));

  36. Array Of Linear Lists LinearList [] x = new LinearList [4]; x[0] = new ArrayLinearList(20); x[1] = new Chain(); x[2] = new Chain(); x[3] = new ArrayLinearList(); for (int i = 0; i < 4; i++) x[i].add(0, new Integer(i));

  37. The Class ArrayLinearList /** array implementation of LinearList */ packagedataStructures; importjava.util.*; // has Iterator interface import utilities.*; // has array resizing class public classArrayLinearListimplementsLinearList { // data members protectedObject [] element; // array of elements protected int size; // number of elements in array // constructors and other methods come here }

  38. A Constructor /** create a list with initial capacity initialCapacity * @throws IllegalArgumentException when * initialCapacity < 1 */ publicArrayLinearList(intinitialCapacity) { if (initialCapacity < 1) throw newIllegalArgumentException ("initialCapacity must be >= 1"); // size has the default initial value of 0 element = new Object [initialCapacity]; }

  39. Another Constructor /** create a list with initial capacity 10 */ publicArrayLinearList() {// use default capacity of 10 this(10); }

  40. The Method isEmpty /** @return true iff list is empty */ public booleanisEmpty() {return size == 0;}

  41. The Method size() /** @return current number of elements in list */ public int size() {return size;}

  42. The Method checkIndex /** @throws IndexOutOfBoundsException when * index is not between 0 and size - 1 */ voidcheckIndex(int index) { if (index < 0 || index >= size) throw newIndexOutOfBoundsException ("index = " + index + " size = " + size); }

  43. The Method get /** @return element with specified index * @throws IndexOutOfBoundsException when * index is not between 0 and size - 1 */ public Object get(int index) { checkIndex(index); return element[index]; }

  44. The Method indexOf /** @return index of first occurrence of theElement, * return -1 if theElement not in list */ public intindexOf(Object theElement) { // search element[] for theElement for (inti = 0; i < size; i++) if (element[i].equals(theElement)) returni; // theElement not found return -1; }

  45. The Method remove public Object remove(int index) { checkIndex(index); // valid index, shift elements with higher index Object removedElement = element[index]; for (inti = index + 1; i < size; i++) element[i-1] = element[i]; element[--size] = null; // enable garbage collection returnremovedElement; }

  46. The Method add public void add(int index, Object theElement) { if (index < 0 || index > size) // invalid list position throw newIndexOutOfBoundsException ("index = " + index + " size = " + size); // valid index, make sure we have space if (size == element.length) // no space, double capacity element = ChangeArrayLength.changeLength1D(element, 2 * size);

  47. The Method add // shift elements right one position for (inti = size - 1; i >= index; i--) element[i + 1] = element[i]; element[index] = theElement; size++; }

  48. Faster Way To Shift Elements 1 Right System.arraycopy(element, index, element, index + 1, size - index);

  49. Convert To A String public String toString() { StringBuffer s = newStringBuffer("["); // put elements into the buffer for (inti = 0; i < size; i++) if (element[i] == null) s.append("null, "); elses.append(element[i].toString() + ", "); if (size > 0) s.delete(s.length() - 2, s.length()); // remove last ", " s.append("]"); // create equivalent String return new String(s); }

  50. Linear list Linked-Lists Representation

More Related