Java Stack Implementation

[Solved] Java Stack Implementation | Java - Code Explorer | yomemimo.com
Question : java stack methods

Answered by : ashwin-selvakumar

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

Source : | Last Update : Mon, 13 Apr 20

Question : Java Queue Implementation

Answered by : fo-angel

import java.util.*;
public class Main { public static void main(String[] args) { Queue<Integer> q1 = new LinkedList<Integer>(); //Add elements to the Queue q1.add(10); q1.add(20); q1.add(30); q1.add(40); q1.add(50); System.out.println("Elements in Queue:"+q1); //remove () method =>removes first element from the queue System.out.println("Element removed from the queue: "+q1.remove()); //element() => returns head of the queue System.out.println("Head of the queue: "+q1.element()); //poll () => removes and returns the head System.out.println("Poll():Returned Head of the queue: "+q1.poll()); //returns head of the queue System.out.println("peek():Head of the queue: "+q1.peek()); //print the contents of the Queue System.out.println("Final Queue:"+q1); }
}

Source : https://www.softwaretestinghelp.com/java-queue-interface/ | Last Update : Sun, 17 Apr 22

Answers related to java stack implementation

Code Explorer Popular Question For Java