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

Demonstration of Stack and Queue Using Java

The document demonstrates the implementation of Stack and Queue data structures in Java. It provides code examples for both, illustrating their operations: Stack follows LIFO (Last In, First Out) while Queue follows FIFO (First In, First Out). Key operations for each structure are highlighted, including push/pop for Stack and enqueue/dequeue for Queue.
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)
2 views2 pages

Demonstration of Stack and Queue Using Java

The document demonstrates the implementation of Stack and Queue data structures in Java. It provides code examples for both, illustrating their operations: Stack follows LIFO (Last In, First Out) while Queue follows FIFO (First In, First Out). Key operations for each structure are highlighted, including push/pop for Stack and enqueue/dequeue for Queue.
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

Demonstration of Stack and Queue Using Java

1. Stack Demonstration (LIFO – Last In, First Out)

import [Link];

public class StackDemo {


public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();

// Push elements
[Link](10);
[Link](20);
[Link](30);

[Link]("Stack: " + stack);

// Peek top element


[Link]("Top element: " + [Link]());

// Pop element
[Link]("Removed element: " + [Link]());

[Link]("Stack after pop: " + stack);


}
}

Output:
Stack: [10, 20, 30]
Top element: 30
Removed element: 30
Stack after pop: [10, 20]

2. Queue Demonstration (FIFO – First In, First Out)


import [Link];
import [Link];

public class QueueDemo {


public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();

// Enqueue elements
[Link](10);
[Link](20);
[Link](30);

[Link]("Queue: " + queue);

// View front element


[Link]("Front element: " + [Link]());

// Dequeue element
[Link]("Removed element: " + [Link]());

[Link]("Queue after dequeue: " + queue);


}
}
Output:
Queue: [10, 20, 30]
Front element: 10
Removed element: 10
Queue after dequeue: [20, 30]

Difference
Stack Queue
Follows LIFO (Last In, First Out) Follows FIFO (First In, First Out)
Insertion and deletion occur at the same end Insertion occurs at the rear, deletion at the
(top) front
Operations: Push, Pop, Peek Operations: Enqueue, Dequeue, Peek

You might also like