0

Was wondering how this can be done using C++.

Divide given array into 3 parts - (0 - N/3), (N/3 - 2N/3) , (2N/3 - N). How would i keep track of the overflow?

Thanks Kelly

2
  • So all the stacks are equal in size and are not growable. Right? Commented Aug 22, 2011 at 14:39
  • Yes. Lets say the array size is 300. Commented Aug 22, 2011 at 14:40

1 Answer 1

1

You must necessarily maintain a pointer-to-top for each of your stacks. Why not just check that these pointers don't exceed their bounds?

Sign up to request clarification or add additional context in comments.

7 Comments

@Kelly: void push(int stacknum, T data) { if (top[stacknum] == (N/3)*stacknum) { error("overflow"); } else { /* Push */ } }.
Certainly a possibility that way, but if you "sacrifice" one word per stackpart you can avoid having the user keep track of the stacknum somewhere. A negligible overhead for a much nicer API imho.
@Voo: I was assuming all of this would be encapsulated in a struct somewhere, along with a nice interface that hides all the details!
@Oli, but how do i find the index of the top element? I have tried something like this. In order to maintain the pointer , i have declared another array(not sure if this is the right way) int *arrayPointer[3] = { NULL, NULL ,NULL }; // Initially pointing NULL Let size of each stacks be 300, so my array size would be 3 * Size of each stack. int sizeOfStack = 300; int *buffer = new int [ 3 * sizeofStack ]; I am blank after this. void push ( int stackNum, int value)
@Kelly: You can either maintain pointers, or indices. Pointers would be: for (i = 0; i < 3; i++) { arrayPointer[i] = &buffer[sizeofStack*i]; }. Indices would be: int arrayIndices[3]; for (i = 0; i < 3; i++) { arrayIndices[i] = sizeofStack[i]; }.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.