Dsa Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy