Created
October 9, 2023 10:20
-
-
Save asbp/ab82e64fda3fe32f2bae42ade232ee00 to your computer and use it in GitHub Desktop.
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
// 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