Question

(Data Structure) Using only emptyCheck(), peek(), push(a), and pop() operations, write a function Middle(Stack S) that...

(Data Structure)

Using only emptyCheck(), peek(), push(a), and pop() operations, write a function Middle(Stack S) that returns the value from stack S that is in the middle, (i.e., location [size / 2]). You may create additional stacks as needed, but S must be restored by the end of the routine.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

function Middle(Stack S){
   Stack st;
   int n=0;
   int mid;
   while(S.empty() == false){
       st.push(S.peek());
       s.pop();
       n++;
   }
  
   for(int i=1;i<=n;i++){
       if(i==n/2){
           mid = st.peek();
           st.pop;
       }
       else{
           S.push(st.peek);
           st.pop();
       }
   }
   return mid;
}

Add a comment
Know the answer?
Add Answer to:
(Data Structure) Using only emptyCheck(), peek(), push(a), and pop() operations, write a function Middle(Stack S) that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT