0% found this document useful (0 votes)
3 views2 pages

Stack Vs Queue Methods in Java Horizontal

The document compares Queue and Stack methods in Java, highlighting their respective operations such as adding, removing, and checking the size of elements. It provides code examples for both Queue and Stack, demonstrating their functionalities using the LinkedList and Stack classes. Key methods include add/offer, remove/poll, push/pop, and peek for both data structures.

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)
3 views2 pages

Stack Vs Queue Methods in Java Horizontal

The document compares Queue and Stack methods in Java, highlighting their respective operations such as adding, removing, and checking the size of elements. It provides code examples for both Queue and Stack, demonstrating their functionalities using the LinkedList and Stack classes. Key methods include add/offer, remove/poll, push/pop, and peek for both data structures.

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/ 2

Stack vs Queue Methods in Java

Method Queue Methods Stack Methods

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

move() removes
remove()front
/ poll()
element (throws exception if empty)poll() removes
pop() removes
front, returns
top element
null if(throws
empty exception if e

eek() returns
peek()front
/ element()
element (returns null if empty)element() returns front
peek()
(throws
returns
exception
top element
if empty)
(returns null if emp

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

size() size() returns number of elements in queuesize() 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