0% found this document useful (0 votes)
8 views5 pages

Lab Assignment 12.1.2 (28-10-2024) (3pm To 5pm)

Uploaded by

2303A54054
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)
8 views5 pages

Lab Assignment 12.1.2 (28-10-2024) (3pm To 5pm)

Uploaded by

2303A54054
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/ 5

Subject – Data structures Date: 18-10-2024

Lab assignment- [email protected]


Implement a Stack using static Arrays (MAX_SIZE 100) with the following operations:
StackUnderFlow: Prints “STACK UNDERFLOW” if stack has no elements in it otherwise Prints
the no.of elements in the Stack.
StackOverFlow: Prints “STACK OVERFLOW” if stack is Full (reached Max_SIZE) otherwise
prints the no.of elements in the Stack.
Peek: Checks StackUnderFlow condition and prints the current top most element in the
Stack only when the Stack has at least one element.
Push<Element>: Inserts an integer element into the Stack only when Stack can
accommodate elements.
Pop: Removes the top most element of the Stack only when Stack has elements.
Traverse: Prints the elements of the Stack from “top of the Stack to bottom of the Stack”.
You will be N – queries where each query has an Integer ID which denotes the operation to
be performed based on the ID values you need to perform the required operations.
Input Format:
The first line contains an integer (N) denoting the number of queries.
The next “N” lines contain the query input where it will read an Integer ID:
 If ID is “1”; performs the “StackUnderFlow” operation and prints the output
accordingly.
 If ID is “2”; performs the “StackOverFlow” operation and prints the output
accordingly.
 If ID is “3”; performs the “Peek” operation and prints the output accordingly.
 If ID is “4”; stands for “Push” operation. Here again you need to read an Integer value
and insert it into the Stack accordingly.
 If ID is “5”; performs the “Pop” operation and prints the removed element only when
Stack has at least one element.
 If ID is “6”; performs the “Traverse” operation and prints the Stack (if exists) from top
of the Stack to Bottom.
Output Format: Print the updated Stack in every Traverse Operation.
Sample Test Case:
15
4 10
4 20
4 30
3
6
1
5
2
4 40
4 50
4 60
3
5
6
2
Sample Output:
30
30 20 10
3
30
2
60
60
50 40 20 10
4
Explanation: Here N = 15 represents total number of Queries to be executed. The Queries
are executed in the following order.
Query ID Operation Explanation
4 10 Push Pushes 10 into Stack
4 20 Push Pushes 20 into Stack
4 30 Push Pushes 30 into Stack
3 Peek Print Peek Element, 30
6 Traverse Prints 30 20 10
1 StackUnderFlow 5 Pop 2 StackOverFlow 4 40 Push 4 50 Push 4 60 Push 3 Peek 5 Pop 6
Traverse 2 StackOverFlow Prints 3 as there are 3
elements
Removes 30 and Prints it
Prints 2 as there are 2
elements
Pushes 40 into Stack
Pushes 50 into Stack
Pushes 60 into Stack
Prints 60
Removes 60 and Prints it
Prints 50 40 20 10
Prints 4 as there are 4
elements in Stack
Code :

import java.util.Scanner;

class Stack {
private static final int MAX_SIZE = 100;
private int[] stack;
private int top;

public Stack() {
stack = new int[MAX_SIZE];
top = -1;
}

public void stackUnderFlow() {


if (top == -1) {
System.out.println("STACK UNDERFLOW");
} else {
System.out.println(top + 1);
}
}

public void stackOverFlow() {


if (top == MAX_SIZE - 1) {
System.out.println("STACK OVERFLOW");
} else {
System.out.println(top + 1);
}
}

public void peek() {


if (top == -1) {
System.out.println("STACK UNDERFLOW");
} else {
System.out.println(stack[top]);
}
}

public void push(int value) {


if (top == MAX_SIZE - 1) {
System.out.println("STACK OVERFLOW");
} else {
stack[++top] = value;
}
}

public void pop() {


if (top == -1) {
System.out.println("STACK UNDERFLOW");
} else {
System.out.println(stack[top--]);
}
}

public void traverse() {


if (top == -1) {
System.out.println("STACK UNDERFLOW");
} else {
for (int i = top; i >= 0; i--) {
System.out.print(stack[i] + " ");
}
System.out.println();
}
}
}

public class StackUsingArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack stack = new Stack();
int n = sc.nextInt();

for (int i = 0; i < n; i++) {


int operation = sc.nextInt();
switch (operation) {
case 1:
stack.stackUnderFlow();
break;
case 2:
stack.stackOverFlow();
break;
case 3:
stack.peek();
break;
case 4:
int value = sc.nextInt();
stack.push(value);
break;
case 5:
stack.pop();
break;
case 6:
stack.traverse();
break;
default:
System.out.println("Invalid Operation");
}
}
sc.close();
}
Output :

30
30 20 10
3
30
2
60
60
50 40 20 10
4

You might also like