Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java stack using linked list

class StackUsingLinkedlist {
              private class Node {
                int data;
                Node next;
              }
            
              Node top;
            
              StackUsingLinkedlist() {
                this.top = null;
              }

            public void push(int data) {
              Node newNode = new Node();
              newNode.data = data;
              newNode.next = top;
              top = newNode;
            }
            
            public boolean isEmpty() {
              return top == null;
            }
            
            public int peek() {
              if (!isEmpty()) {
                return top.data;
              } else {
                System.out.println("Stack is empty");
                return -1;
              }
            }
            
            public void pop() {
              if (top == null) {
                System.out.print("
Stack empty");
                return;
              }
              top = top.next;
            }
            
            public void display() {
              if (top == null) {
                System.out.print("
Stack empty");
                exit(1);
              } else {
                Node node = top;
                while (node != null) {
                  System.out.printf("%d->", node.data);
                  node = node.next;
                }
              }
            }
Comment

Implement stack using Linked List Java

public class LinkedListStack {
    private Node head; // the first node
 
    // nest class to define linkedlist node
    private class Node {
        int value;
        Node next;
    }
 
    public LinkedListStack() {
        head = null;
    }
 
    // Remove value from the beginning of the list for demonstrating behaviour of stack
    public int pop() throws LinkedListEmptyException {
        if (head == null) {
            throw new LinkedListEmptyException();
        }
        int value = head.value;
        head = head.next;
        return value;
    }
 
    // Add value to the beginning of the list for demonstrating behaviour of stack
    public void push(int value) {
        Node oldHead = head;
        head = new Node();
        head.value = value;
        head.next = oldHead;
    }
 
    public static void main(String args[]) 
    {
        LinkedListStack lls=new LinkedListStack();
        lls.push(20);
        lls.push(50);
        lls.push(80);
        lls.push(40);
        lls.push(60);
        lls.push(75);
        System.out.println("Element removed from LinkedList: "+lls.pop());
        System.out.println("Element removed from LinkedList: "+lls.pop());
        lls.push(10);
        System.out.println("Element removed from LinkedList: "+lls.pop());
        printList(lls.head);
    }
    public static void printList(Node head) {
        Node temp = head;
        while (temp != null) {
            System.out.format("%d ", temp.value);
            temp = temp.next;
        }
        System.out.println();
    }
}
 
/**
 * 
 * Exception to indicate that LinkedList is empty.
 */
 
class LinkedListEmptyException extends RuntimeException {
    private static final long serialVersionUID = 1L;
 
    public LinkedListEmptyException() {
        super();
    }
 
    public LinkedListEmptyException(String message) {
        super(message);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java hello world 
Java :: public static void main(string args) 
Java :: java how to make a gui 
Java :: Service vs Intent Service 
Java :: java gui 
Java :: java stream order by property 
Java :: .now() java 
Java :: Read from text file android studio 
Java :: list to array java 
Java :: spring boot hibernate log sql 
Java :: java shutdown hook 
Java :: java reverse loop 
Java :: random word java 
Java :: java try catch 
Java :: how to declare array java 
Java :: java 16 raspberry pi 
Java :: read integer input java 
Java :: war file vs jar file 
Java :: java random unique key 
Java :: android string java 
Java :: key listener java 
Java :: java string format thousand separator 
Java :: share intent android 
Java :: how to convert epoch time to date in java 
Java :: check whether a double is not a number in java 
Java :: java bigdecimal check less zero 
Java :: android studio change menu title 
Java :: how to make a copy of an array java 
Java :: android studio pass value to another activity 
Java :: java remove duplicates 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =