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

Java Program 1 - Harshit

The document outlines a practical exercise to implement stack and queue concepts in Java. It explains the LIFO (Last In, First Out) behavior of stacks and FIFO (First In, First Out) behavior of queues, along with their common operations. Example code for both stack and queue implementations is provided, along with a comparison of their features.

Uploaded by

viewm830
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 views6 pages

Java Program 1 - Harshit

The document outlines a practical exercise to implement stack and queue concepts in Java. It explains the LIFO (Last In, First Out) behavior of stacks and FIFO (First In, First Out) behavior of queues, along with their common operations. Example code for both stack and queue implementations is provided, along with a comparison of their features.

Uploaded by

viewm830
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

Practical No-1

AIM:-Write a java program to implement stack and queue concept.


THEORY:- A Stack follows LIFO
Last In, First Out
Think of a stack of plates:

●​ You add a plate on top


●​ You remove the top plate first

🔹 Common Operations
push() → add element
pop() → remove top element
peek() → see top element
isEmpty() → check if stack is empty

Stack Example (Java)


import [Link];

public class StackExample {


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

[Link](10);
[Link](20);
[Link](30);

[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}
}

NAME = HARSHIT TRIPATHI​


Enroll no = 35213303124
Queue in Java
🔹 Concept
A Queue follows FIFO
First In, First Out
Think of a line at a ticket counter:

●​ First person in line is served first

Queue Operations

Operation Meaning

enqueue() Insert element


dequeue() Remove element
peek() View first element
isEmpty() Check if empty

Stack vs Queue (Quick Comparison)


Feature Stack Queue

Order LIFO FIFO

Add PUSH() add() / offer()

Remove POP() remove() / poll()

Access one end Both end

Queue Code
import [Link];

import [Link];

public class QueueExample

NAME = HARSHIT TRIPATHI​


Enroll no = 35213303124
public static void main(String[] args)

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

[Link](10);

[Link](20);

[Link](30);

[Link](queue);

[Link]();

[Link](queue);

[Link]([Link]());

Important Concept Reminder

✔ Queue follows FIFO (First In First Out)


✔ First added → First removed
Queue<Integer> queue = new LinkedList<>();
Creates a queue using LinkedList to store integer elements in FIFO order.

CODE:
import [Link].*;

class GfG {

static class Stack {

NAME = HARSHIT TRIPATHI​


Enroll no = 35213303124
static Queue<Integer> q1 = new LinkedList<>();

static Queue<Integer> q2 = new LinkedList<>();

static void push(int x) {

[Link](x);

while (![Link]()) {

[Link]([Link]());

[Link]();

Queue<Integer> q = q1;

q1 = q2;

q2 = q;

static void pop() {

if ([Link]())

return;

[Link]();

static int top() {

if ([Link]())

return -1;

return [Link]();

NAME = HARSHIT TRIPATHI​


Enroll no = 35213303124
static int size() {

return [Link]();

public static void main(String[] args) {

Stack s = new Stack();

[Link](1);

[Link](2);

[Link](3);

[Link]("current size: " + [Link]());

[Link]([Link]());

[Link]();

[Link]([Link]());

[Link]();

[Link]([Link]());

[Link]("current size: " + [Link]());

NAME = HARSHIT TRIPATHI​


Enroll no = 35213303124
OUTPUT 1:-

OUTPUT 2:-

OUTPUT 3:-

NAME = HARSHIT TRIPATHI​


Enroll no = 35213303124

You might also like