0% found this document useful (0 votes)
3 views

Dsa Lab

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)
3 views

Dsa Lab

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

#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);

stacks.push(2);

stacks.push(5);

stacks.push(4);

stacks.push(7);

stacks.display();
stacks.popEven();

stacks.popOdd();

stacks.display();

return 0;

You might also like