1 / 20

Pertemuan 8

Pertemuan 8. Stack (2). S tack dengan array. keseluruhan. import java.io.*; public class stack_array { private int maxsize ; // penentu batas elemen stack maksimum private int [] stackarray ; // array untuk menyimpan stack private int top; // indeks array

ariane
Download Presentation

Pertemuan 8

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. Pertemuan 8 Stack (2)

  2. Stack dengan array

  3. keseluruhan import java.io.*; public class stack_array { private intmaxsize; //penentubataselemen stack maksimum private int [] stackarray; //array untukmenyimpan stack private int top; //indeks array public void inisiasi(ints) { // isifungsiinisiasi } public void push(int data) { // isifungsi push} public double pop() { // isifungsi pop } public void view() {// isifungsi view} }

  4. inisiasi public void inisiasi(int s) //menentukanukurankapasitas stack { maxsize = s; stackarray = new int[maxsize]; top = -1; }

  5. push public void push(intdata) { if (top >= maxsize-1) System.out.println("Stack Penuh. "+data+" TidakBisaMasuk"); else { top++; stackarray[top] = data; System.out.println(data +"Masukke Stack"); } }

  6. pop public int pop() { int temp; if (top>=0) { temp = stackarray[top]; System.out.println(temp + " Keluardari Stack"); top--; return (temp); } else { System.out.println("Stack SudahKosong"); return(-1); } }

  7. view public void view() { System.out.print("Isi Stack: "); for(inti=0; i<=top; i++) System.out.print(stackarray[i] + " "); System.out.println(); }

  8. Contoh pemanggilan public static void main(String[] args) { stack_array stack = new stack_array(); stack.inisiasi(3); stack.push(3);stack.push(4);stack.push(2); stack.view(); stack.push(5);stack.push(1);stack.pop();stack.pop(); stack.view(); stack.pop();stack.pop();stack.pop(); stack.push(6);stack.push(8);stack.push(7);stack.push(9); stack.pop(); stack.view(); }

  9. Stack dengan linked list

  10. Push Operation

  11. Pop Operation

  12. Link list class item //atau node/simpul { public int data; // data item public item next; // next node link in list public item prev; // previous node link in list public item(int id) // constructor { data = id; // initialize data } // set to null) public void displayLink() // display ourself { System.out.print("{" + data + "}"); } } // end class Link

  13. Class stack keseluruhan class StackLinkList { private item top; // ref to first link on list private item bottom; // ref to last link on list public StackLinkList() // constructor { top = bottom = null; // no items on list yet // inisiasi stack } public booleanisEmpty() // true if list is empty { // isifungsicek empty } public void push(int id) { // isifungsi push } public item pop() // delete first item { // isifungsi pop } public void display() { // iisfungsi display } } // end class LinkList

  14. Inisasi stack public StackLinkList() // constructor { top = bottom = null; // no items on list yet }

  15. cek empty public booleanisEmpty() // true if list is empty { return (top==null); }

  16. push public void push(int id) //node baruselaludi top { // make new link item newitem = new item(id); if (top == null) // the first node created { top = bottom = newitem; // first –> newLink } else // the second node and the next node { top.next = newitem; //next dr top (awal) diarahkanke node baru newitem.prev = top; //prevdr node barudiarahkanke tail (awal) top = newitem; //top (baru) diarahkanke node baru } }

  17. pop public item pop() // delete first item { item temp = null; if (top == null) // stack is empty System.out.println("Stack is empty"); else if (top == bottom) // stack is only one data { temp = top; top = bottom = null; } else // stack has more than one data { temp = top; // save reference to link top = top.prev; // delete it: first–>old next top.next = null; } return temp; }

  18. display public void display() { item current = bottom; // start from the first data while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }

  19. Contoh penggunaan public static void main(String[] args) { StackLinkListtheStack = new StackLinkList(); // make new list System.out.println("Initializing Stack…"); theStack.push(22); // insert four items theStack.push(44); theStack.push(66); theStack.push(88); System.out.println("Display Forward :"); theStack.display(); // display list System.out.println("Delete Stack from Top…"); while( !theStack.isEmpty() ) // until it’s empty, { item aLink = theStack.pop(); // delete link System.out.print("Deleted "); // display it aLink.displayLink(); System.out.println(""); } theStack.display(); // display list } // end main()

  20. Tugas Final Project • Goal: Membuat video pembelajaranttgstruktur data. • Manfaat: Menjadi media pendukung keg. belajar-mengajarmatakuliahstruktur data dikelas. • Tema: Stack dan Queue • Aturan: • Durasi video antara 8 – 10 menit. • Jumlahanggota 7 – 9 mahasiswa. • Adapembagiantugas yang jelasuntuksetiapanggota. • Demo I (progress report): 2 pekansebelum UAS. Demo II (final product): 1 pekansebelum UAS. • Penilaian: • Kebenaranisi harusadasumberliteratur yang jelas. • Kreativitas. • Video editting. • Pembawaancerita/ skenario.

More Related