0% found this document useful (0 votes)
3 views1 page

Stack & Queue

The document provides a Java program that demonstrates the functionality of Stack and Queue data structures. It shows how to implement Stack with LIFO behavior and Queue with FIFO behavior, allowing user input of three integers for each structure. The program includes operations such as push, pop, and peek for Stack, and add, poll, and peek for Queue, displaying the results accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Stack & Queue

The document provides a Java program that demonstrates the functionality of Stack and Queue data structures. It shows how to implement Stack with LIFO behavior and Queue with FIFO behavior, allowing user input of three integers for each structure. The program includes operations such as push, pop, and peek for Stack, and add, poll, and peek for Queue, displaying the results accordingly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Stack & Queue Class XII

- Write a program in Java to demonstrate the working of Stack and Queue data
structures.
o Use a Stack to insert and remove integers, showing LIFO (Last In, First Out)
behavior.
o Use a Queue to insert and remove integers, showing FIFO (First In, First
Out) behavior.
o Accept 3 integers each for Stack and Queue from the user and display the
results after performing push, pop, peek for Stack and add, poll, peek for
Queue.
import [Link].*;
public class StackQueue
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
// ---------- STACK DEMONSTRATION ----------
Stack<Integer> stack = new Stack<>();
[Link]("STACK DEMONSTRATION (LIFO)");
[Link]("Enter 3 numbers for Stack: ");
for (int i = 0; i < 3; i++)
{
int num = [Link]();
[Link](num); // pushing elements into stack
}

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


[Link]("Top element (peek): " + [Link]());
[Link]("Removed element (pop): " + [Link]());
[Link]("Stack after pop: " + stack);

// ---------- QUEUE DEMONSTRATION ----------


// Queue implemented using LinkedList
Queue<Integer> queue = new LinkedList<>();
[Link]("\nQUEUE DEMONSTRATION (FIFO)");
[Link]("Enter 3 numbers for Queue: ");
for (int i = 0; i < 3; i++)
{
int num = [Link]();
[Link](num); // offer() is safer than add()
}

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


[Link]("Front element (peek): " + [Link]());
[Link]("Removed element (poll): " + [Link]());
[Link]("Queue after poll: " + queue);
}
}

RAMAKRISHNA ACADEMY 7003793770


1

You might also like