Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6626899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:56:57+00:00 2026-05-25T21:56:57+00:00

I implemented it as an array with two stacks adjacent to each other, but

  • 0

I implemented it as an array with two stacks adjacent to each other, but their tops on the either ends. That is, if top(stack1) is at the beginning of keys, top(stack2) is at the end of keys. Bottom(Stack1) and Bottom(Stack2) should be adjacent, but anywhere between top(Stack1) and top(Stack2). To delete, I am poping from Top(Stack1), and for inserting, I am pushing in Top(stack2). Could somebody pls tell me if it is correct to do it this way?

When I read CLRS, I solved the que this way, and had no way to know that it was ryt or not. But it was asked in today’s exam, and everyone later on was discussing the way given officially (here and everywhere else on net), so it seems I am the only one to do it such a way. I really wanna know if this is wrong or right ? Please help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:56:58+00:00Added an answer on May 25, 2026 at 9:56 pm

    Queues and stacks are abstract data structures which have well defined behaviors and any implementation of these data structures should respect this contract.

    Your idea of using a single array to implement two stacks is good. But, the inserts and deletes should happen on both stacks.

    For eg. lets say you have this setup

    2 3 4 5 6

    top(stack1) is 2
    bottom(stack1) is 4
    top(stack2) is 6
    bottom(stack2) is 5

    after popping from stack1 3 times you would have reached the bottom of your stack i.e 4 and no longer able to pop anything even though there are two more elements in your QUEUE implementation. So, a few corrections to your implementation is required.

    So, if I were to implement two stacks which emulate a QUEUE, this is how I would do it.

    Stack1: 2 3 4 5 6 which is essentially an array
    2 is the bottom of the stack and 6 is the top of the stack.

    Stack2: empty

    Insert an element to Queue:
    This is very simple. Just add to the end of array i.e stack1
    stack1:2 3 4 5 6 7
    Now 7 is the top of the stack.

    Delete an element from Queue:
    1. Pop all elements in stack1 and insert them to stack2. So, your array will be reversed
    stack1:empty
    stack2: 7 6 5 4 3 2 . Now 2 is at the top of the stack.
    2. Now your top(stack2) will be pointing to 2. Just pop it.
    7 6 5 4 3
    3. Now for the remaining elements in stack2, pop from stack2 and insert them to stack1.
    stack2:empty
    stack1:3 4 5 6 7

    PS: The above algorithm assumes that you know how to manage memory for arrays as it shrinks or expands.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of classes that want to pass each other some information
I am making a program that makes use of two Array Lists. I've researched
Suppose we have a shared queue (implemented using an array), which two threads can
I can see the advantage of using two stacks if an array implementation is
I have two queues, one is implemented using an array for storage and the
I've got a polymorphic array of objects which implement two (informal) interfaces. I want
Is there a simple way to retrieve the length of an associative array (implemented
I've implemented this search algorithm for an ordered array of integers. It works fine
Its known that Java ArrayList is implemented using arrays and initializes with capacity of
If we need to implement a function that takes an array of integers and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.