Breaking down N/k stack implementation in a single array

Initialisation of the array
push(2,20) : push element 20 in stack number 2.
  1. i=free;
  2. We correspondingly update the value of free to next free space available with the help of next array. // free= next[I];
  3. We store the value of element at the first free space in our array. arr[I]=element;
  4. in our next array, we update the value of the element corresponding to the free space. next[I]=top[sn];
  5. Finally, we store top[sn]=I;
  1. We go to the top pointer array to find out where our top element of the stack is, we store this index as i. i = top[sn];
  2. We update top[sn] with the next element from the next array, ie. -1. //top[sn]=next[top[sn]];
  3. Now, since this is a free space, we need to update our free variable to 0. But we also simultaneously, have to link the previous free variable. We do so by updating our next array. //next[I]=free; //free=I;
pop() operation

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store