What is stack? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is stack?

Please explain with an example. What I can't understand is the mechanism by which it stores values.

24th Nov 2017, 4:39 PM
Infinity
Infinity - avatar
3 Answers
+ 1
a stack of frames >> suppose theres a biggg boxx... thousands of frames piled up ONE AFTER ANOTHER... now if u wanna insert another...u need to keep it on the top.. if u want to remove one...AGAIN u need to take it from the top... U CANT GET ACCESS OF ANY FRAME INSTEAD OF THE CURRENT TOP ONE.. INSERT-TOP DELETE-TOP TOP IS ONLY IMPORT-EXPORT BUSINESS we often say it as... last in ; last out (LIFO) meaning >>> if u wanna insert ; u hv to do it at last ( on top) if u wanna take out(delet)..again from last(top).. bottom == first top == last ## HOPE IT HELPED ##
24th Nov 2017, 5:30 PM
sayan chandra
sayan chandra - avatar
+ 1
Stack is a data structure to implement the LAST IN FIRST OUT method ... for example consider a set of plates are arranged in the order of one above another. The first plate is in the bottom and the the last plate is in the top. if you want to take the first plate you must remove the all other plates ,that means the top is removed until the plates are empty .this is the simplest way to understand the concept of stack. to store values ,,stack can be implemented by linked list and array. The easiest way is array . if you want to store 5 elements in the stack means,, you should follow these steps: 1. stack[5] (create an array) 2. declare top=-1 (because array takes the address from 0) 3. if you want to add a value increase the top value and store the value in the array top=top+1 stack[top]=value you can store the values until top=4 (here the stack limit is 5 (0,1,...4) ) 4.to remove the value you an reduce the top value top=top-1 thus the values are stored and removed from the stack.... If you didn't understand yet, ask again......
24th Nov 2017, 5:44 PM
RAVIKUMAR R
RAVIKUMAR R - avatar