Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is part of a class declaration and constructor for keeping two stacks with

ID: 3561559 • Letter: B

Question

Below is part of a class declaration and constructor for keeping two stacks within a single linear array. Neither stack should overflow until all memory in the array is utilized and an entire stack should never be shifted to a different location within the array. Write the methods push1(), push2(), pop1(), pop2(), clear1(), clear2(), isEmpty1(), isEmpty2(), and isFull() to manipulate the two stacks. (Hint: The two stacks grow toward each other.)

public class Stack {

                private int[] item;

                private int top1;

                private int top2;

                private int size;

}

public Stack (int max) {

                size = max;

                item = new int [size];

                top 1 = -1;

                top 2 = size;

}

Explanation / Answer

/*Include the necessary header files*/

#define MAX 10

typedef struct

{

int stack[MAX]; //Array of integers

int top1;

int top2;

}STACK;

void push1();

void pop1();

void display1();

void push2();

void pop2();

void display2(); //Function prototypes

int main()

{

STACK s;

int choice;

s.top1 = -1;

s.top2 = MAX;

do

{

printf(" Operation on two stacks:");

printf(" Stack 1 Stack 2");

printf(" __________ __________");

printf(" 1.) Push1 4.) Push2");

printf(" 2.) Pop1 5.) Pop2");

printf(" 3.) Display1 6.) Display2");

printf(" 7.) Exit");

printf(" Enter your choice: ");

scanf("%d", &choice);

switch( choice )

{

case 1: push1(&s);

break;

case 2: pop1(&s);

break;

case 3: display1(s);

break;

case 4: push2(&s);

break;

case 5: pop2(&s);

break;

case 6: display2(s);

break;

case 7: break;

default: printf(" Invalid choice! Enter again.");

}

}while(choice!=7);

getch();

}

void push1(STACK *p)

{

if(p->top1 == (p->top2) - 1)

{

printf(" Stack1 is overflow");

}

else

{

(p->top1)++;

printf(" Enter the element to push: ");

scanf("%d", &p->stack[p->top1]);

}

}

void pop1(STACK *p)

{

if(p->top1 == -1)

{

printf(" Stack1 is underflow");

}

else

{

printf(" Popped element: %d", p->stack[(p->top1)--]);

}

}

void display1(STACK s)

{

int i;

if(s.top1 == -1)

{

printf(" Stack1 is Empty");

}

else

{

printf(" Stack1 is: ");

for(i=s.top1; i>=0; i--)

{

printf(" %d", s.stack[i]);

}

}

}

void push2(STACK *p)

{

if(p->top2 == p->top1 + 1)

{

printf(" Stack2 is overflow");

}

else

{

(p->top2)--;

printf(" Enter the element to push: ");

scanf("%d", &p->stack[p->top2]);

}

}

void pop2(STACK *p)

{

if(p->top2 == MAX)

{

printf(" Stack2 is underflow");

}

else

{

printf(" Popped element: %d", p->stack[(p->top2)++]);

}

}

void display2(STACK s)

{

int i;

if(s.top2 == MAX)

{

printf(" Stack2 is Empty");

}

else

{

printf(" Stack2 is: ");

for(i=s.top2 ; i < MAX ; i++)

{

printf(" %d", s.stack[i]);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote