6 6 votes A stack is implemented with an array of $’A[0...N-1]’$ and a variable ‘$pos$’. The push and pop operations are defined by the following code. push (x) A[pos] <- x pos <- pos -1 end push pop() pos <- pos+1 return A[pos] end pop Which of the following will initialize an empty stack with capacity $N$ for the above implementation $pos \leftarrow -1$ $pos\leftarrow 0$ $pos\leftarrow 1$ $pos\leftarrow N-1$ Data Structures isro-2020 data-structures stack normal + – Satbir 7.0k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Himanshu Kumar Gupta commented Aug 22, 2020 reply Follow flag i think D is the ansewer 0 0 replyShare goku4199 commented Oct 23, 2025 reply Follow flag Ans 1 1 replyShare Please log in or register to add a comment.
5 5 votes Answer D. Stack is growing downwards, and on pushing an item, the top [pos] is decrementing by one, meaning the on the topmost position [pos] will be 0, or (N - 1 - (N - 1)], so the initial value of [pos] will be N - 1 habedo007 answered Jan 13, 2020 habedo007 comment Share Follow See all 3 Comments 3 3 Comments reply rohan_12 commented Jan 24, 2020 reply Follow flag @habedo007 if we try to perfom POP operation 1st then value of pos becomes N which will result in invalid array index.We should check if both operations can be done appropriately or not. If we check for both the operations then none of the pos value is correct. Please clear my doubt if i am wrong. 0 0 replyShare Kshitij Sharma commented Dec 18, 2024 reply Follow flag push(x) pos <- pos - 1 // Pointer ko peeche le jao A[pos] <- x // Us position pe value store karo end push code ye nahi hona chahiye kya ?sawal vala code mujhe galat lag rha hai. 0 0 replyShare Tejaswee_Bommaluleni commented Nov 5, 2025 reply Follow flag @Kshitij Sharma Nahi bhaiya ,kyunki hum push operation mein first element insert karte hain ,baad mein pointer change karenga. That why it should be A[pos] <- x //push element pos <- pos -1 //decrement pos 1 1 replyShare Please log in or register to add a comment.
1 1 vote push (x) A[pos] <- x pos <- pos -1 end push Look at the push operation. $x$ is pushed at pos, then pos is decremented. This means if our array is $A[0,1,2,3]$ then in order to push four elements, we first we're pushing at index 3, then at index 2, then at index 1 and finally at index 0. We have to initialize pos with the value equalling $N-1$ Option D JashanArora answered Feb 21, 2020 JashanArora comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes I think answer will be d) $pos\leftarrow N-1$ Because here the 1st element is added at the end first and we find that the pointer is decremented after it has added the element. So, here the pointer shouldhave been initiallized to N-1. Pratyush Priyam Kuan answered Feb 11, 2020 Pratyush Priyam Kuan comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Ans goku4199 answered Oct 23, 2025 goku4199 comment Share Follow 0 reply Please log in or register to add a comment.