Created
January 2, 2024 02:23
-
-
Save affandes/1fa30af64f0b651a26ad3d68d8e71ee3 to your computer and use it in GitHub Desktop.
Contoh kode program Stack dengan tipe data String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Stack { | |
public String[] data; // tipe data String | |
public int top; | |
public Stack(int n) { | |
this.data = new String[n]; // tipe data String | |
this.top = 0; | |
} | |
public void push(String d) { // tipe data String | |
if (this.top < this.data.length) { | |
this.data[this.top] = d; | |
this.top++; | |
} | |
} | |
public String pop() { // tipe data String | |
if (this.top > 0) { | |
this.top--; | |
return this.data[this.top]; | |
} | |
return 0; | |
} | |
public String peek() { // tipe data String | |
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