Skip to content

Instantly share code, notes, and snippets.

@affandes
Last active January 2, 2024 02:09
Show Gist options
  • Save affandes/76e9875ae2c7a813284a61599cda040a to your computer and use it in GitHub Desktop.
Save affandes/76e9875ae2c7a813284a61599cda040a to your computer and use it in GitHub Desktop.
Contoh membuat class Stack sendiri pada Java
public class Stack {
public int[] data;
public int top;
public Stack(int n) {
this.data = new int[n];
this.top = 0;
}
public void push(int d) {
if (this.top < this.data.length) {
this.data[this.top] = d;
this.top++;
}
}
public int pop() {
if (this.top > 0) {
this.top--;
return this.data[this.top];
}
return 0;
}
public int peek() {
if (this.top > 0) {
return this.data[this.top-1];
}
return 0;
}
public boolean empty() {
return this.top == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment