0% found this document useful (0 votes)
4 views3 pages

Stack Vs Queue Methods in Java Landscape

The document compares methods of Queue and Stack in Java, detailing their respective operations such as adding, removing, peeking, checking emptiness, and size. It includes example code for both a Queue and a Stack, demonstrating how to use these methods. The Queue uses LinkedList while the Stack uses the Stack class in Java.

Uploaded by

relishmunjal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Stack Vs Queue Methods in Java Landscape

The document compares methods of Queue and Stack in Java, detailing their respective operations such as adding, removing, peeking, checking emptiness, and size. It includes example code for both a Queue and a Stack, demonstrating how to use these methods. The Queue uses LinkedList while the Stack uses the Stack class in Java.

Uploaded by

relishmunjal07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Stack vs Queue Methods in Java

Method Queue Methods Stack Methods

add(E e) / offer(E
add(E
e) e) adds element to the queueoffer(E e) adds element, returns false on failure push(E e) adds element to the stack

remove()
remove()
/ poll()removes front element (throws exception if empty)poll() removes front, returnspop()
null ifremoves
empty top element (throws exception if empt

peek() peek()
/ element()
returns front element (returns null if empty)element() returns front (throws exceptionpeek()
if empty)
returns top element (returns null if empty)

isEmpty() isEmpty() returns true if queue is empty isEmpty() returns true if stack is empty

size() size() returns number of elements in queue size() returns number of elements in stack

Queue Example:

---------------

import java.util.Queue;

import java.util.LinkedList;

public class QueueExample {

public static void main(String[] args) {

Queue<String> queue = new LinkedList<>();


queue.add("Apple");

queue.add("Banana");

System.out.println("Queue size: " + queue.size()); // Output: 2

System.out.println("Head: " + queue.peek()); // Output: Apple

System.out.println("Removed: " + queue.remove()); // Output: Apple

System.out.println("Is queue empty? " + queue.isEmpty()); // Output: false

Stack Example:

--------------

import java.util.Stack;

public class StackExample {

public static void main(String[] args) {


Stack<String> stack = new Stack<>();

stack.push("Apple");

stack.push("Banana");

System.out.println("Stack size: " + stack.size()); // Output: 2

System.out.println("Top: " + stack.peek()); // Output: Banana

System.out.println("Popped: " + stack.pop()); // Output: Banana

System.out.println("Is stack empty? " + stack.isEmpty()); // Output: false

You might also like