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

Stack Queue Updated

The document contains Java programs for implementing a stack and a queue using arrays, including classes 'StackOperation' and 'QueueOps'. Each class provides methods for basic operations such as push, pop, and display, along with a main method for user interaction. Additionally, a Variable Description Table (VDT) is included for both classes, detailing the purpose of each variable used.
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)
15 views5 pages

Stack Queue Updated

The document contains Java programs for implementing a stack and a queue using arrays, including classes 'StackOperation' and 'QueueOps'. Each class provides methods for basic operations such as push, pop, and display, along with a main method for user interaction. Additionally, a Variable Description Table (VDT) is included for both classes, detailing the purpose of each variable used.
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

Updated Java Programs with Purpose-Based Variable Names

Q1. Stack using Array

Class: StackOperation

import [Link].*;

class StackOperation {
char stkArr[]; // Stack array to hold characters
int stkCap; // Stack capacity
int stkTop; // Stack top index

StackOperation(int stkMaxSize) {
stkCap = stkMaxSize;
stkTop = -1;
stkArr = new char[stkCap];
}

void push() {
Scanner in = new Scanner([Link]);
if (stkTop == stkCap - 1)
[Link]("Stack Overflow");
else {
[Link]("Enter element to push: ");
char eleToPush = [Link]().charAt(0);
stkArr[++stkTop] = eleToPush;
[Link]("Element added");
}
}

void pop() {
if (stkTop == -1)
[Link]("Stack Underflow");
else {
[Link]("Element deleted: " + stkArr[stkTop]);
stkTop--;
}
}

void display() {
if (stkTop == -1)
[Link]("Stack Underflow");
else {
for (int idx = 0; idx <= stkTop; idx++)
[Link](stkArr[idx]);

1
}
}

public static void main() {


Scanner in = new Scanner([Link]);
[Link]("Enter stack size (≤30): ");
int stkSize = [Link]();

if (stkSize <= 30) {


StackOperation stkObj = new StackOperation(stkSize);
int menuChoice;
while (true) {
[Link]("[Link]\[Link]\[Link]\[Link]\nEnter
your choice:");
menuChoice = [Link]();
switch (menuChoice) {
case 1: [Link](); break;
case 2: [Link](); break;
case 3: [Link](); break;
case 4: [Link](0);
default: [Link]("Invalid choice");
}
}
} else {
[Link]("Invalid size");
}
}
}

VDT (Variable Description Table)

Variable Type Purpose

stkArr[] char[] Stack array

stkCap int Stack capacity

stkTop int Top of the stack index

stkSize int Input stack size

eleToPush char Character to push

menuChoice int Menu option

idx int Loop index for display

Q2. Queue using Array

Class: QueueOps

2
import [Link].*;

class QueueOps {
double qArr[]; // Queue array to hold double values
int qCap; // Queue capacity
int qFront; // Front pointer
int qRear; // Rear pointer

QueueOps(int qSize) {
qCap = qSize;
qArr = new double[qCap];
qFront = qRear = -1;
}

void push(int qEle) {


if (qFront == qCap - 1)
[Link]("Queue Overflow");
else {
if (qFront == -1) {
qArr[++qFront] = qEle;
qRear = 0;
} else {
qArr[++qFront] = qEle;
}
}
}

int pop() {
if (qFront == -1 && qRear == -1) {
return -99999;
} else if (qFront == qRear) {
int deletedVal = (int) qArr[qFront];
qFront = qRear = -1;
return deletedVal;
} else if (qFront > qRear) {
return (int) qArr[qRear++];
} else {
return -99999;
}
}

void show() {
if (qFront == -1 && qRear == -1)
[Link]("Queue Underflow");
else {
for (int dispIdx = qRear; dispIdx <= qFront; dispIdx++)
[Link](qArr[dispIdx]);
}
}

3
public static void main() {
Scanner in = new Scanner([Link]);
[Link]("Enter queue size (≤50): ");
int qSize = [Link]();

if (qSize <= 50) {


QueueOps qObj = new QueueOps(qSize);
int menuChoice;

while (true) {
[Link]("[Link]\[Link]\[Link]\[Link]\nEnter
your choice:");
menuChoice = [Link]();

switch (menuChoice) {
case 1:
[Link]("Enter element to add: ");
int qEle = [Link]();
[Link](qEle);
break;
case 2:
double poppedVal = [Link]();
if (poppedVal == -99999)
[Link]("Queue Underflow");
else
[Link]("Element deleted: " +
poppedVal);
break;
case 3: [Link](); break;
case 4: [Link](0);
default: [Link]("Invalid choice");
}
}
} else {
[Link]("Invalid size");
}
}
}

VDT (Variable Description Table)

Variable Type Purpose

qArr[] double[] Queue array

qCap int Queue capacity

qFront int Front index

qRear int Rear index

qSize int Input queue size

4
Variable Type Purpose

qEle int Element to enqueue

menuChoice int Menu option

poppedVal double Dequeued value

dispIdx int Index for display loop

You might also like