Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question
100%

PLEASE READ: Do not code unless the question asks for it, then code in java. Importantly and clearly answer the question by explaining with words fully first, thank you

Explain how to implement two stacks in one array A[1..n] in such a way that neither stack
overflows unless the total number of elements in both stacks together is n. The PUSH and POP
operations should run in O(1) time.
Justify your answer and give some examples.
expand button
Transcribed Image Text:Explain how to implement two stacks in one array A[1..n] in such a way that neither stack overflows unless the total number of elements in both stacks together is n. The PUSH and POP operations should run in O(1) time. Justify your answer and give some examples.
Expert Solution
Check Mark
Step 1

Answer:

->The purpose is to start two stacks from two extreme corners of arr[]/stack1 starts from the leftmost element, the first element in stack1 is pushed at index 0.

->The stack2 starts from the rightmost corner, the first element in stack2 is pushed at index(n-1). Both stacks grow in opposite direction.

->To check for overflow, all we need to check is for space between top elements of both stacks. This check is highlighted in the below code:

Programming Code:

import java.io.*;

import java.util.*;

class twoStacks

{

int *arr;

int size;

int tops1,top2;

public:

  twoStacks(int n) //constructor

{

size=n;

arr= new int[n];

top1=-1;

top2= size;

}

//Method to push an element x to stack1

void push(intx)

{

//There is atleast one empty space for new element

if(top1<top2-1)

{

top1++;

arr[top1]=x;

}

else

{

System.out.println("Stack Overflow");

exit(1);

}

//Method to push an element x to stack2

void push2(int x)

{

//There is at least one empty space for new element

if(top1<top2-1)

{

top2--;

arr[top2]=x;

}

else

{

System.out.println("Stack Overflow");

exit(1);

}

}

//Method to pop an element from first stack

int pop1()

{

if(top1>=0)

{

int x=arr[top1];

top1..;

return x;

}

else

{

System.out.println("Stack underFlow");

exit(1);

}

}

//Method to pop an element from second stack

int pop2()

{

if(top2<size)

{

int x=arr[top2];

top2++;

return x;

}

else

{

System.out.println("Stack underFlow");

exit(1);

}

}

}

 

Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY