What Is a Stack? Data Structure Explained
The last-in, first-out structure behind undo buttons and the 'back' button.
What a stack is
A stack is a data structure that stores items in a particular order: the last item added is the first one removed. This is called LIFO, short for 'last in, first out.' The classic analogy is a stack of plates: you add new plates to the top, and when you need one, you take it from the top too. You cannot easily grab a plate from the middle or bottom. A stack works exactly the same way with data.
Push and pop
Stacks have two main operations. 'Push' adds an item to the top of the stack. 'Pop' removes the item from the top and gives it back to you. Because both operations work only at the top, a stack is simple and fast. You always know where the action happens, at the top, and you always get back the most recently added item when you pop. These two operations are the heart of how every stack works.
The undo button
A perfect everyday example of a stack is the 'undo' feature in apps. Each action you take is pushed onto a stack. When you press undo, the program pops the most recent action off the stack and reverses it. Press undo again, and it reverses the action before that. This naturally undoes your actions in reverse order, most recent first, which is exactly the LIFO behavior a stack provides. Redo often uses a second stack.
The back button
Your web browser's back button is another stack in action. Each page you visit is pushed onto a stack. When you click back, the browser pops the current page and returns you to the previous one. Keep clicking back, and you retrace your steps in reverse order. This is why the back button always takes you to where you just were: it is popping pages off a stack, one at a time, in last-in-first-out order.
Stacks inside programs
Stacks are not just for user-facing features; they are fundamental inside programs too. When a program calls a function, information about that call is pushed onto a 'call stack,' and popped off when the function finishes. This is how a program keeps track of where to return after each function completes, and it is why nested and recursive function calls work correctly. The call stack is one of the most important uses of the stack structure.
Why it matters
The stack is a simple but essential data structure, and its last-in-first-out behavior shows up everywhere, from undo buttons and back buttons to the inner workings of programs themselves. Understanding stacks clarifies how these familiar features work and introduces a pattern of thinking that recurs throughout computer science. For anyone learning programming, the stack is a key concept that connects abstract theory to tools you use every day.
Related on Skillo
See also: What is a queue? Data structure explained, What is recursion? Explained simply.
Sources
Published date reflects the original event date (2024-09-24). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.
Written by
Skillo Staff
0 Comments
Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.