Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

stacks based on a linked list

// Java program to Implement a stack
// using singly linked list
// import package
import static java.lang.System.exit;
  
// Create Stack Using Linked list
class StackUsingLinkedlist {
  
    // A linked list node
    private class Node {
  
        int data; // integer data
        Node link; // reference variable Node type
    }
    // create global top reference variable global
    Node top;
    // Constructor
    StackUsingLinkedlist()
    {
        this.top = null;
    }
  
    // Utility function to add an element x in the stack
    public void push(int x) // insert at the beginning
    {
        // create new node temp and allocate memory
        Node temp = new Node();
  
        // check if stack (heap) is full. Then inserting an
        //  element would lead to stack overflow
        if (temp == null) {
            System.out.print("
Heap Overflow");
            return;
        }
  
        // initialize data into temp data field
        temp.data = x;
  
        // put top reference into temp link
        temp.link = top;
  
        // update top reference
        top = temp;
    }
  
    // Utility function to check if the stack is empty or not
    public boolean isEmpty()
    {
        return top == null;
    }
  
    // Utility function to return top element in a stack
    public int peek()
    {
        // check for empty stack
        if (!isEmpty()) {
            return top.data;
        }
        else {
            System.out.println("Stack is empty");
            return -1;
        }
    }
  
    // Utility function to pop top element from the stack
    public void pop() // remove at the beginning
    {
        // check for stack underflow
        if (top == null) {
            System.out.print("
Stack Underflow");
            return;
        }
  
        // update the top pointer to point to the next node
        top = (top).link;
    }
  
    public void display()
    {
        // check for stack underflow
        if (top == null) {
            System.out.printf("
Stack Underflow");
            exit(1);
        }
        else {
            Node temp = top;
            while (temp != null) {
  
                // print node data
                System.out.printf("%d->", temp.data);
  
                // assign temp link to temp
                temp = temp.link;
            }
        }
    }
}
// main class
public class GFG {
    public static void main(String[] args)
    {
        // create Object of Implementing class
        StackUsingLinkedlist obj = new StackUsingLinkedlist();
        // insert Stack value
        obj.push(11);
        obj.push(22);
        obj.push(33);
        obj.push(44);
  
        // print Stack elements
        obj.display();
  
        // print Top element of Stack
        System.out.printf("
Top element is %d
", obj.peek());
  
        // Delete top element of Stack
        obj.pop();
        obj.pop();
  
        // print Stack elements
        obj.display();
  
        // print Top element of Stack
        System.out.printf("
Top element is %d
", obj.peek());
    }
}
Comment

stacks based on a linked list

// Java program to Implement a stack
// using singly linked list
// import package
import static java.lang.System.exit;
  
// Create Stack Using Linked list
class StackUsingLinkedlist {
  
    // A linked list node
    private class Node {
  
        int data; // integer data
        Node link; // reference variable Node type
    }
    // create global top reference variable global
    Node top;
    // Constructor
    StackUsingLinkedlist()
    {
        this.top = null;
    }
  
    // Utility function to add an element x in the stack
    public void push(int x) // insert at the beginning
    {
        // create new node temp and allocate memory
        Node temp = new Node();
  
        // check if stack (heap) is full. Then inserting an
        //  element would lead to stack overflow
        if (temp == null) {
            System.out.print("
Heap Overflow");
            return;
        }
  
        // initialize data into temp data field
        temp.data = x;
  
        // put top reference into temp link
        temp.link = top;
  
        // update top reference
        top = temp;
    }
  
    // Utility function to check if the stack is empty or not
    public boolean isEmpty()
    {
        return top == null;
    }
  
    // Utility function to return top element in a stack
    public int peek()
    {
        // check for empty stack
        if (!isEmpty()) {
            return top.data;
        }
        else {
            System.out.println("Stack is empty");
            return -1;
        }
    }
  
    // Utility function to pop top element from the stack
    public void pop() // remove at the beginning
    {
        // check for stack underflow
        if (top == null) {
            System.out.print("
Stack Underflow");
            return;
        }
  
        // update the top pointer to point to the next node
        top = (top).link;
    }
  
    public void display()
    {
        // check for stack underflow
        if (top == null) {
            System.out.printf("
Stack Underflow");
            exit(1);
        }
        else {
            Node temp = top;
            while (temp != null) {
  
                // print node data
                System.out.printf("%d->", temp.data);
  
                // assign temp link to temp
                temp = temp.link;
            }
        }
    }
}
// main class
public class GFG {
    public static void main(String[] args)
    {
        // create Object of Implementing class
        StackUsingLinkedlist obj = new StackUsingLinkedlist();
        // insert Stack value
        obj.push(11);
        obj.push(22);
        obj.push(33);
        obj.push(44);
  
        // print Stack elements
        obj.display();
  
        // print Top element of Stack
        System.out.printf("
Top element is %d
", obj.peek());
  
        // Delete top element of Stack
        obj.pop();
        obj.pop();
  
        // print Stack elements
        obj.display();
  
        // print Top element of Stack
        System.out.printf("
Top element is %d
", obj.peek());
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: check if combobox has specified value 
Java :: This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer. 
Java :: shuffle a string in java 
Java :: java boolean even number 
Java :: Java Enum toString() 
Java :: Repeat execution of function infini android studio 
Java :: string tmp java 
Java :: java arduino 
Java :: how to shorten if else if and else in java 
Java :: java windowbuilder full screen 
Java :: java list sort comparator date descending lambda 
Java :: como llamar a un metodo static en java 
Java :: textbox to arraylist 
Java :: are classes in java public by default 
Java :: java secureRandom certain range 
Java :: java to kotlin online converter 
Java :: Java Creating LinkedHashMap from Other Maps 
Java :: hide frame ( acces to the top level component ) 
Java :: search for a string in byte array 
Java :: serilize filke in java 
Java :: popcat 
Java :: compile time exception in java 
Java :: Example of ArrayDeque 
Java :: check whether an entry in hashmap is deleted in java 
Java :: java program for calendar using applet 
Java :: Java Numbers and Strings 
Java :: Java Enable assertion in class names 
Java :: cannot apply java lang integer android 
Java :: Caused by: java.lang.ClassNotFoundException: 
Java :: += in java 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =