Laporan Praktikum Algoritma & Struktur Data Bab 7 Fakultas Ilmu Komputer Universitas Brawijaya 2017/2018
Soal :
Source code :
Soal no.1 & 2
Soal no.3
Class BukuArray
Class BukuLL
Download the file here
Soal :
1.
Buat program untuk menkonvesi dari infix ke
notasi prefix menggunakan program stack di atas!
2. Kembangkan program di atas untuk menghitung
nilai dari ekspresi postfix dengan inputan angka 1-9.
3. Kembangkan program di atas dengan membuat stack
yang berisi Object sehingga Stack dapat diisi dengan object buku yang telah
dibuat pada praktikum sebelumnya. Terapkan kedua jenis stack, baik yang array
maupun single linked list!
Source code :
Soal no.1 & 2
package asdmodul3; import java.util.Scanner; import java.util.Stack; public class ASDModul3 { // Struktur Data private int size; private int top; private int[] data; // method public ASDModul3(int n) { top = -1; size = n; data = new int[size]; } public boolean isPalindrome() { return true; } public boolean isFull() { return top == (size - 1) ? true : false; //if (top == size-1)return true; //else return false; } public boolean isEmpty() { return top == -1 ? true : false; //if (top == -1) return true; //else return false; } public void push(int dt) { if (!isFull()) { data[++top] = dt; } } public int pop() { int hasil = -999; if (!isEmpty()) { hasil = data[top--]; } return hasil; } public static String prefix(String a) { Stack stack = new Stack(); String prefix = ""; for (int i = a.length() - 1; i >= 0; i--) { char c = a.charAt(i); if (Character.isLetter(c)) { prefix = ((Character) c).toString() + prefix; } else if (c == '(') { prefix = ((Character) stack.pop()).toString() + prefix; } else if (c == ')') { continue; } else { stack.push(c); } } return prefix; } public static String postfix(String a) { Stack stack = new Stack(); String postfix = ""; for (int i = 0; i < a.length(); i++) { char c = a.charAt(i); if (Character.isLetter(c)) { postfix = postfix + c; } else if (c == '(') { continue; } else if (c == ')') { postfix = postfix + ((Character) stack.pop()).toString(); } else { stack.push(c); } } return postfix; } public static int calPostfix(String exp) { Stackstck = new Stack (); Scanner sausan = new Scanner(exp); while (sausan.hasNext()) { if (sausan.hasNextInt()) { stck.push(sausan.nextInt()); } else { int m = stck.pop(); int h = stck.pop(); String op = sausan.next(); if (op.equals("+")) { stck.push(h + m); } else if (op.equals("-")) { stck.push(h - m); } else if (op.equals("*")) { stck.push(h * m); } else { stck.push(h / m); } } } return stck.pop(); } public static void main(String[] args) { ASDModul3 st = new ASDModul3(3); st.push(0); st.push(6); st.push(7); while (!st.isEmpty()) { System.out.println(st.pop()); } //app stack int nilai = 1234; ASDModul3 s = new ASDModul3(100); while (nilai != 0) { int sisa = nilai % 2; s.push(sisa); nilai = nilai / 2; } while (!s.isEmpty()) { System.out.print(s.pop()); } String infix = "((a+b)*(z+x))"; String postfix1 = "1 2 +"; String postfix2 = "2 3 + 4 5 * +"; String postfix3 = "8 5 * 7 4 2 + * +"; System.out.println("\nPostfix : " + postfix(infix) + "\nPrefix : " + prefix(infix)); System.out.println("Hasil dari postfix " + postfix1 + " adalah " + calPostfix(postfix1)); System.out.println("Hasil dari postfix " + postfix2 + " adalah " + calPostfix(postfix2)); System.out.println("Hasil dari postfix " + postfix3 + " adalah " + calPostfix(postfix3)); } }
Soal no.3
Class BukuArray
package asdmodul3; import java.util.Scanner; import java.util.Stack; public class BukuArray { // Struktur Data private int size; private int top; private Object[] data; // method public BukuArray(int n) { top = -1; size = n; data = new Object[size]; } public boolean isPalindrome() { return true; } public boolean isFull() { return top == (size - 1) ? true : false; } public boolean isEmpty() { return top == -1 ? true : false; } public void push(String j, String k, double s, double d) { if (!isFull()) { data[++top] = "Judul : " + j + "\nKategori : " + k + "\nBiaya Sewa : " + s + "\nBiaya Denda : " + d + "\n==============================="; } } public Object pop() { Object hasil = -999; if (!isEmpty()) { hasil = data[top--]; } return hasil; } public static void main(String[] args) { BukuArray st = new BukuArray(3); st.push("Jigsaw", "Thriller", 20000, 5000); st.push("Night", "Horror", 25000, 5000); st.push("Hujan", "Romance", 10000, 3000); while (!st.isEmpty()) { System.out.println(st.pop()); } } }
Class BukuLL
package asdmodul3; import java.util.EmptyStackException; import java.util.Stack; class NodeBukuLL { Object data; NodeBukuLL next; String judul, kategori; double sewa, denda; NodeBukuLL(String j, String k, double s, double d) { judul = j; kategori = k; sewa = s; denda = d; } } public class BukuLL { private NodeBukuLL head, tail, top; public BukuLL() { head = tail = top = null; } public boolean isEmpty() { return top == null; } public Object pop() { if (isEmpty()) { throw new EmptyStackException(); } Object dataPop = top.judul + "\n" + top.kategori + "\n" + top.sewa + "\n" + top.denda; removeLast(); top = tail; return dataPop; } private void removeLast() { NodeBukuLL temp = head; if (!isEmpty()) { if (tail == head) { head = tail = null; } else { while (temp.next != tail) { temp = temp.next; } temp.next = null; tail = temp; temp = null; } } } public void push(String j, String k, double s, double d) { addLast(j, k, s, d); top = tail; } private void addLast(String j, String k, double s, double d) { NodeBukuLL newNode = new NodeBukuLL(j, k, s, d); if (isEmpty()) { head = tail = newNode; } else { tail.next = newNode; tail = newNode; } } public Object peek() { return top.judul + " " + top.kategori + " " + top.sewa + " " + top.denda; } public void cetak(String komentar) { System.out.println(komentar); NodeBukuLL pointer = head; while (pointer != null) { System.out.print("\nJudul : " + pointer.judul + "\nKategori : " + pointer.kategori + "\nBiaya sewa : " + pointer.sewa + "\nBiaya Denda : " + pointer.denda + "\n============================="); pointer = pointer.next; } System.out.println(""); } public static void main(String[] args) { BukuLL st = new BukuLL(); st.push("Anne", "Biography", 20000, 5000); st.push("Friends", "Novel", 25000, 5000); st.push("OPPA", "Novel", 13000, 2000); st.cetak("DATA BUKU"); st.pop(); st.cetak("DATA BUKU YANG TERAKHIR DIINPUT DIHAPUS"); System.out.println(st.peek()); while (!st.isEmpty()) { System.out.println(st.pop()); } } }
Download the file here
0 comments:
Posting Komentar