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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why Lightspeed invested in Hasura: bringing GraphQL to everyone

How to Setup WordPress on the Raspberry Pi

Stacked Pull Requests on Github

Getting Native Reservations on OpenStreetMap: Transcript

What makes Solana the fastest public blockchain ?

Ask Me Anything with tehmoonwalker

Bringing “surround with” functionality to vscode. Checkout out this extension.

GSoC@OpenMRS 6thWeek

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
Spriha Gautam

Spriha Gautam

More from Medium

Searching in a sorted 2D array

How Hashing is useful in Recent Technologies?

Leetcode: 1305. All Elements in Two Binary Search Trees

Quick Sort — Javascript implementation