Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java stack

// construct with non-primative elements only!
Stack<String> stack = new Stack<String>();

// to add a value to the top of the stack:
stack.push("Hello");

// to return and remove a value from the top:
String top = stack.pop();

// to return a value without removing it:
String peek = stack.peek();
Comment

java stack methods

import java.util.Stack<E>;
Stack<Integer> myStack = new Stack<Integer>();
myStack.push(1);
myStack.pop();
myStack.peek();
myStack.empty(); // True if stack is empty
Comment

Java Creating a Stack

// Create Integer type stack
Stack<Integer> stacks = new Stack<>();

// Create String type stack
Stack<String> stacks = new Stack<>();
Comment

Java Stack Declaration

public class Stack<E> extends Vector<E>
Comment

Stack java

// Java program to Implement a stack
// using singly linked list
// import package
import static java.lang.System.exit;
 
// Driver code
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());
    }
}
 
// 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.print(temp.data);
 
                // assign temp link to temp
                temp = temp.link;
                if(temp != null)
                    System.out.print(" -> ");
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java platform runlater keeps running 
Java :: read only jtextfield 
Java :: interview questions on ds and algo 
Java :: Iterating & Adding to a Stack 
Java :: find maximum number of rides in amusement park python 
Java :: success listener with Glide Android java 
Java :: java requirenonnull 
Java :: ternario java 
Java :: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type project and qualifiers [@Default] 
Java :: jsp multipart/form-data submition 
Java :: java Color on Enum call 
Java :: diamonds 
Java :: convert python to java translator online 
Java :: java scanner equation 
Java :: add dynamic view in android from xml 
Java :: has 8 digit in number 
Java :: implement hashmap 
Java :: java webelement how to double click 
Java :: java minimize all windows 
Java :: convert code python to java 
Java :: default access modifier java 
Java :: how to put comments on swagger documentation in spring boot 
Java :: free pearson uk textbooks 
Java :: java two constructors 
Java :: get steps counts using pedometer sensor in android 
Java :: how to wait in the javaprogram 
Java :: String in Queue 
Java :: how to hide password in java code 
Java :: material design implement full screen dialog android java 
Java :: java combine to byte[] 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =