Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Stack {
- private Node first = null;
- private class Node
- {
- String item;
- Node next = null;
- }
- public boolean isEmpty() {
- return first == null;
- }
- public void push(String item)
- {
- Node oldfirst = first;
- first = new Node();
- first.item = item;
- first.next = oldfirst;
- }
- public void pop()
- {
- if(isEmpty()){
- System.out.print("Stack underflow");
- return;
- }
- String item = first.item;
- first = first.next;
- }
- public String peek(){
- if(isEmpty())
- System.out.print("Stack underflow");
- return first.item;
- }
- public static void main(String[] args) {
- Stack list = new Stack();
- list.push("co");
- list.push("bao");
- list.push("gio");
- list.push("em");
- list.push("nghi");
- list.push("nghi");
- list.push("ve");
- list.push("anh");
- while (!list.isEmpty()) {
- System.out.println(list.peek());
- list.pop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement