KEMBAR78
Two Stack Assignment | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
13 views3 pages

Two Stack Assignment

Uploaded by

Anvita Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Two Stack Assignment

Uploaded by

Anvita Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.

data;

import java.util.Scanner;

public class TwoStackAss {


private int[] stack;
private int tos1;
private int tos2;
private int maxSize;

public TwoStackAss(int size) {


maxSize = size;
stack = new int[maxSize];
tos1 = -1;
tos2 = maxSize;
}

public void pushToStack1(int value) {


if (isFull()) {
System.out.println("Stack Overflow: Cannot push " + value + " to Stack
1.");
} else {
stack[++tos1] = value;
System.out.println("Element " + value + " pushed to Stack 1.");
}
}

public void pushToStack2(int value) {


if (isFull()) {
System.out.println("Stack Overflow: Cannot push " + value + " to Stack
2.");
} else {
stack[--tos2] = value;
System.out.println("Element " + value + " pushed to Stack 2.");
}
}

public boolean isFull() {


return tos1 + 1 == tos2;
}

public int popFromStack1() {


if (isStack1Empty()) {
System.out.println("Stack is Empty Cannot pop from Stack 1.");
return -1;
} else {
return stack[tos1--];
}
}

public int popFromStack2() {


if (isStack2Empty()) {
System.out.println("Stack is Empty Cannot pop from Stack 2.");
return -1;
} else {
return stack[tos2++];
}
}
public boolean isStack1Empty() {
return tos1 == -1;
}

public boolean isStack2Empty() {


return tos2 == maxSize;
}

public void printStack1() {


if (isStack1Empty()) {
System.out.println("Stack 1 is empty.");
} else {
System.out.println("Stack 1 contents: ");
for (int i = tos1; i >= 0; i--) {
System.out.println(stack[i]);
}
}
}

public void printStack2() {


if (isStack2Empty()) {
System.out.println("Stack 2 is empty.");
} else {
System.out.println("Stack 2 contents: ");
for (int i = tos2; i < maxSize; i++) {
System.out.println(stack[i]);
}
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter the total size for the two stacks: ");
int size = sc.nextInt();

TwoStackAss twoStacks = new TwoStackAss(size);


int ch, value;

do {
System.out.println("\nMenu:");
System.out.println("1. Push to Stack 1");
System.out.println("2. Push to Stack 2");
System.out.println("3. Pop from Stack 1");
System.out.println("4. Pop from Stack 2");
System.out.println("5. Print Stack 1");
System.out.println("6. Print Stack 2");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
ch = sc.nextInt();

switch (ch) {
case 1:
System.out.print("Enter value to push to Stack 1: ");
value = sc.nextInt();
twoStacks.pushToStack1(value);
break;
case 2:
System.out.print("Enter value to push to Stack 2: ");
value = sc.nextInt();
twoStacks.pushToStack2(value);
break;
case 3:
int poped1 = twoStacks.popFromStack1();
if (poped1 != -1) {
System.out.println("Poped from Stack 1: " + poped1);
}
break;
case 4:
int poped2 = twoStacks.popFromStack2();
if (poped2 != -1) {
System.out.println("Poped from Stack 2: " + poped2);
}
break;
case 5:
twoStacks.printStack1();
break;
case 6:
twoStacks.printStack2();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice! Please try again.");
}
} while (ch != 0);

}
}

You might also like