0% found this document useful (0 votes)
4 views4 pages

Two Stacks in One Array C++ Code

Uploaded by

231571
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)
4 views4 pages

Two Stacks in One Array C++ Code

Uploaded by

231571
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

#include <iostream>

using namespace std;

class TwoStacksOneArray {

private:

int* arr;

int size;

int evenIndex;

int oddIndex;

public:

TwoStacksOneArray(int n) {

size = n;

arr = new int[size];

evenIndex = 0;

oddIndex = 1;

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

arr[i] = -1;

void push(int value) {

if (value % 2 == 0) {

if (evenIndex < size) {

arr[evenIndex] = value;

evenIndex += 2;

} else {

cout << "Even Stack Overflow!" << endl;

}
} else {

if (oddIndex < size) {

arr[oddIndex] = value;

oddIndex += 2;

} else {

cout << "Odd Stack Overflow!" << endl;

void popEven() {

if (evenIndex > 0) {

evenIndex -= 2;

cout << "Popped from Even Stack: " << arr[evenIndex] << endl;

arr[evenIndex] = -1;

} else {

cout << "Even Stack Underflow!" << endl;

void popOdd() {

if (oddIndex > 1) {

oddIndex -= 2;

cout << "Popped from Odd Stack: " << arr[oddIndex] << endl;

arr[oddIndex] = -1;

} else {

cout << "Odd Stack Underflow!" << endl;

}
void display() {

cout << "Array: [ ";

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

if (arr[i] != -1) {

cout << arr[i] << " ";

} else {

cout << "_ ";

cout << "]" << endl;

~TwoStacksOneArray() {

delete[] arr;

};

int main() {

cout << "IBRAHIM AHMAD 231571" << endl;

TwoStacksOneArray stacks(10);

[Link](2);

[Link](5);

[Link](4);

[Link](7);

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

[Link]();

[Link]();

return 0;

You might also like