Implementasi Stack Menggunakan Java
Stack merupakan salah satu struktur data yang menggunakan konsep LIFO (Last In First Out), dimana data yang masuk paling akhir, dapat diakses atau digunakan sebagai data paling awal. Sebagai contoh yaitu permainan menara hanoi (tower of hanoi), dimana kita hanya dapat mengambil bagian paling atas dari menara, dan hanya dapat menempatkan bagian menara ke bagian paling atas. Pada dasarnya, Java telah menyediakan package khusus struktur data stack. Namun, untuk lebih memahami konsep stack, tidak ada salahnya untuk coding from scratch. Pada stack, terdapat beberapa metode yang sering diimplementasikan, yaitu :
- push : metode yang digunakan untuk memasukkan data ke dalam tumpukan paling atas.
- pop : metode yang digunakan untuk mengambil data pada tumpukan paling atas.
- peek : berbeda dengan pop, metode peek hanya melihat data pada tumpukan paling atas, tidak mengambilnya, sehingga tidak terjadi perubahan pada stack.
- count : metode yang digunakan untuk menghitung jumlah elemen pada tumpukan.
- clear : metode yang digunakan untuk membersihkan stack dari semua elemen.
Berikut ini merupakan contoh implementasi stack pada Java :
package assignment_04; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* This class represents stack data structure | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since April 9th 2021 | |
* | |
* @param <type> type of each element | |
* */ | |
public class Stack<type> { | |
/** Field to contain main list */ | |
private final List<type> list; | |
/** Field to contain peak index */ | |
private int currentIndex; | |
/** | |
* Constructor to initiate the list and current index | |
* */ | |
public Stack() { | |
this.list = new ArrayList<>(); | |
this.currentIndex = -1; | |
} | |
/** | |
* This method is used to add new data to the top of the list | |
* | |
* @param data value for the new data | |
* */ | |
public void push(type data) { | |
this.list.add(data); | |
currentIndex++; | |
} | |
/** | |
* This method is used to delete last data from the list | |
* | |
* @return value of the last element of the list | |
* */ | |
public type pop() { | |
type object = this.list.remove(currentIndex); | |
currentIndex--; | |
return object; | |
} | |
/** | |
* This method is used to get the number of element in the list | |
* | |
* @return size of the list | |
* */ | |
public int count() { return this.list.size(); } | |
/** | |
* This method is used to get value of the last element in the list | |
* | |
* @return value of the last element in a list | |
* */ | |
public type peek() { return list.get(currentIndex); } | |
/** | |
* This method is used to remove all elements from the list | |
* */ | |
public void clear() { | |
this.list.clear(); | |
currentIndex = -1; | |
} | |
} |
Dan berikut ini merupakan contoh penggunaan stack yang telah diimplementasikan pada Java :
package assignment_04; | |
/** | |
* This class is used as main console application | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since April 9th 2021 | |
* */ | |
public class StackApplication { | |
/** | |
* This is used as main method | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args) { | |
// PREPARE STACK | |
Stack<String> stack = new Stack<>(); | |
// PUSH DATA TO STACK | |
stack.push("Fajar"); | |
stack.push("Zuhri"); | |
stack.push("Hadiyanto"); | |
// GET NUMBER OF ELEMENT IN STACK | |
int count = stack.count(); | |
System.out.println("Number of data in stack : " + count); | |
// GET TOP DATA FROM STACK | |
String object = stack.peek(); | |
System.out.println("Top data in stack : " + object); | |
// POP STACK AND PRINT THAT ELEMENT | |
object = stack.pop(); | |
System.out.println("Object popped from stack : " + object); | |
// RECOUNT THE NUMBER OF STACK | |
count = stack.count(); | |
System.out.println("Number of data in current stack : " + count); | |
// GET TOP DATA FROM STACK | |
object = stack.peek(); | |
System.out.println("Top data in current stack : " + object); | |
} | |
} |
Setelah program dicompile, maka hasilnya akan seperti berikut.
Source code dari program di atas dapat diakses disini
Comments
Post a Comment