Implementation of Two Stacks with one Array in Java

This is the continuation of previous post on Array implementation of stack in java. In this post, we implements 2 stacks using one array.

While implementing stack using array we took one index which first initially point to -1, In this case of 2 stacks we take two index one point to -1 and other point to the end of the array i.e, size of the array.

Algorithms :

  • Start with two indexes one at the left and other at the right end of the array.
  • Left index simulate the first stack and second index simulate the right stack.
  • If we want to put the element in the first stack then put the element at the left index. Similarly, if we want to put the element in the second stackthen put the element at the right index.
  • First stack grow towards left and second stack grow towards left.

Click here to read more

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s