Skip to content

Instantly share code, notes, and snippets.

@asbp
Created October 9, 2023 10:20
Show Gist options
  • Save asbp/ab82e64fda3fe32f2bae42ade232ee00 to your computer and use it in GitHub Desktop.
Save asbp/ab82e64fda3fe32f2bae42ade232ee00 to your computer and use it in GitHub Desktop.
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import java.util.Scanner;
class LLStack {
class Node {
public String data;
public Node next;
}
private Node first;
public LLStack() {
first = null;
}
public void push(String data) {
Node n = new Node();
n.data=data;
n.next=first;
first=n;
}
public String peek() {
if(first==null) {
return null;
}
String result = first.data;
return result;
}
public String pop() {
if(first==null) {
return null;
}
Node temp = first;
first = first.next;
return temp.data;
}
}
class HelloWorld {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
LLStack list = new LLStack();
for(int i=0; i<5;i++) {
System.out.print("Input barang ke-"+(i+1)+": ");
String name = myScanner.nextLine();
list.push(name);
}
System.out.println("First peek: "+list.peek());
System.out.println("First pop: "+list.pop());
System.out.println("Second pop: "+list.pop());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment