[go: up one dir, main page]

0% found this document useful (0 votes)
6 views7 pages

Lab DSA

The document provides Java implementations for a stack and a queue data structure, each with methods for basic operations such as push, pop, enqueue, and dequeue. It includes user interaction through a console menu for executing these operations and displaying the current state of the stack or queue. Sample input and output demonstrate the functionality of both data structures.

Uploaded by

Boomika G
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)
6 views7 pages

Lab DSA

The document provides Java implementations for a stack and a queue data structure, each with methods for basic operations such as push, pop, enqueue, and dequeue. It includes user interaction through a console menu for executing these operations and displaying the current state of the stack or queue. Sample input and output demonstrate the functionality of both data structures.

Uploaded by

Boomika G
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
You are on page 1/ 7

Lab DSA

1.stack

import java.util.Scanner;

class Stack {
int stack[] = new int[5]; // Stack size is 5
int top = -1;

// Push an element onto the stack


void push(int data) {
if (top == 4) {
System.out.println("Stack is full!");
} else {
top++;
stack[top] = data;
System.out.println(data + " pushed into the stack.");
displayStack();
}
}

// Pop an element from the stack


void pop() {
if (top == -1) {
System.out.println("Stack is empty!");
} else {
System.out.println(stack[top] + " popped from the stack.");
top--;
displayStack();
}
}

// Peek at the top element of the stack


void peek() {
if (top == -1) {
System.out.println("Stack is empty!");
} else {
System.out.println("Top element is " + stack[top]);
displayStack(); // Show the stack after peek
}
}
// Display the current elements of the stack
void displayStack() {
if (top == -1) {
System.out.println("Stack is empty!");
} else {
System.out.print("Current stack: ");
for (int i = 0; i <= top; i++) {
System.out.print(stack[i] + " ");
}
System.out.println();
}
}

// Check if the stack is empty


boolean isEmpty() {
return top == -1;
}
}

public class SimpleStack {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack s = new Stack();
int choice, data;

do {
System.out.println("\n1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter data to push: ");
data = sc.nextInt();
s.push(data);
break;
case 2:
s.pop();
break;
case 3:
s.peek();
break;
case 4:
System.out.println("Exiting...");
s.displayStack(); // Display the stack contents before exit
break;
default:
System.out.println("Invalid choice!");
}
} while (choice != 4);

sc.close();
}
}

Output:
1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 1
Enter data to push: 10
10 pushed into the stack.
Current stack: 10

1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 1
Enter data to push: 20
20 pushed into the stack.
Current stack: 10 20

1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 3
Top element is 20
Current stack: 10 20

1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 2
20 popped from the stack.
Current stack: 10

1. Push
2. Pop
3. Peek
4. Exit
Enter your choice: 4
Exiting...
Current stack: 10

2.Queue

Import java.util.Scanner;

Class Queue {
Int queue[] = new int[5]; // Queue size is 5
Int front = -1;
Int rear = -1;

// Enqueue an element into the queue


Void enqueue(int data) {
If (rear == 4) {
System.out.println(“Queue is full!”);
} else {
If (front == -1) {
Front = 0; // If the queue was empty, set front to 0
}
Rear++;
Queue[rear] = data;
System.out.println(data + “ enqueued into the queue.”);
displayQueue();
}
}
// Dequeue an element from the queue
Void dequeue() {
If (front == -1 || front > rear) {
System.out.println(“Queue is empty!”);
} else {
System.out.println(queue[front] + “ dequeued from the queue.”);
Front++;
displayQueue();
}
}

// Peek at the front element of the queue


Void peek() {
If (front == -1 || front > rear) {
System.out.println(“Queue is empty!”);
} else {
System.out.println(“Front element is “ + queue[front]);
displayQueue();
}
}

// Display the current elements of the queue


Void displayQueue() {
If (front == -1 || front > rear) {
System.out.println(“Queue is empty!”);
} else {
System.out.print(“Current queue: “);
For (int I = front; I <= rear; i++) {
System.out.print(queue[i] + “ “);
}
System.out.println();
}
}

// Check if the queue is empty


Boolean isEmpty() {
Return front == -1 || front > rear;
}
}

Public class SimpleQueue {


Public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Queue q = new Queue();
Int choice, data;

Do {
System.out.println(“\n1. Enqueue”);
System.out.println(“2. Dequeue”);
System.out.println(“3. Peek”);
System.out.println(“4. Exit”);
System.out.print(“Enter your choice: “);
Choice = sc.nextInt();

Switch (choice) {
Case 1:
System.out.print(“Enter data to enqueue: “);
Data = sc.nextInt();
q.enqueue(data);
break;
case 2:
q.dequeue();
break;
case 3:
q.peek();
break;
case 4:
System.out.println(“Exiting…”);
q.displayQueue(); // Display the queue contents before exit
break;
default:
System.out.println(“Invalid choice!”);
}
} while (choice != 4);

Sc.close();
}
}

Sample Input/Output:

1. Enqueue
2. Dequeue
3. Peek
4. Exit
Enter your choice: 1
Enter data to enqueue: 10
10 enqueued into the queue.
Current queue: 10

Enter your choice: 1


Enter data to enqueue: 20
20 enqueued into the queue.
Current queue: 10 20

Enter your choice: 2


10 dequeued from the queue.
Current queue: 20

Enter your choice: 3


Front element is 20
Current queue: 20

Enter your choice: 4


Exiting…
Current queue: 20

You might also like