210 likes | 378 Views
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
E N D
Pertemuan 8 Stack (2)
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} }
inisiasi public void inisiasi(int s) //menentukanukurankapasitas stack { maxsize = s; stackarray = new int[maxsize]; top = -1; }
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"); } }
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); } }
view public void view() { System.out.print("Isi Stack: "); for(inti=0; i<=top; i++) System.out.print(stackarray[i] + " "); System.out.println(); }
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(); }
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
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
Inisasi stack public StackLinkList() // constructor { top = bottom = null; // no items on list yet }
cek empty public booleanisEmpty() // true if list is empty { return (top==null); }
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 } }
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; }
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(""); }
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()
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.