Log in Sign up
Back to Discover
💻

Stack (abstract data type)

technology Maturity 7-9

A stack is like a pile.

Tallrik - Ystad-2018.jpg
Tallrik - Ystad-2018.jpg
You put things on the top. You take things from the top too. It is just like plates. This helps computers work. Do you like stacks of toys?

37 words

A stack is like a pile of things.

Tallrik - Ystad-2018.jpg
Tallrik - Ystad-2018.jpg
You can add a new item to the top. This is called a push. You can also take the top item off. This is called a pop.
Lifo stack.svg
Lifo stack.svg
The last thing you put on top is the first thing you take off. If the pile gets too big, it is called an overflow. If the pile is empty, it is called an underflow. You can even peek to see the top item. This lets you look without moving anything. A stack is a very useful tool for computers.

100 words

A stack is a way to organize data. It works like a pile of plates.

Tallrik - Ystad-2018.jpg
Tallrik - Ystad-2018.jpg
You can only add or remove items from the top. This makes it easy to use.
Lifo stack.svg
Lifo stack.svg

There are two main steps in a stack. The first is called push. This means you add a new item to the top. The second is called pop. This means you take the top item off. Because of this, the last item you add is the first one you take away. Scientists call this LIFO. LIFO stands for last in, first out.

You can also use a step called peek. This lets you look at the top item. It does not move the item or change the stack.

Sometimes, a stack can have problems. If the stack is full, it is called a stack overflow. This happens when you try to push an item but there is no room. If the stack is empty, it is called an underflow. This happens if you try to pop an item when nothing is there.

Stack Push operation-en.gif
Stack Push operation-en.gif

181 words

A stack is a special way to organize information in a computer. It is called an abstract data type. This means it describes how data behaves rather than how it is built. You can imagine a stack of plates in a cafeteria.

Tallrik - Ystad-2018.jpg
Tallrik - Ystad-2018.jpg
When you add a plate, you put it on the very top. When you need a plate, you take the one from the top. This order is called LIFO, which stands for last in, first out. The last item you put in is the very first one you take out.
Lifo stack.svg
Lifo stack.svg

There are two main ways to change a stack. The first way is called a push. A push adds a new item to the top of the collection. The second way is called a pop. A pop removes the most recent item from the top. You can also use a tool called a peek. A peek lets you look at the top item without moving it.

Stack Push operation-en.gif
Stack Push operation-en.gif
If you try to push an item into a stack that is already full, it causes a stack overflow. If you try to pop an item from an empty stack, it causes an underflow.
Stack Pop Operation-en.gif
Stack Pop Operation-en.gif

People have been using these ideas for a long time. In 1946, a famous scientist named Alan Turing used similar ideas. He used the words "bury" and "unbury" to talk about computer tasks. In 1945, Konrad Zuse built a machine called the Z4 that used a two-level stack. Later, in 1955, Klaus Samelson and Friedrich L. Bauer proposed a stack idea. They called it an "operational cellar." They even filed a patent for it in 1957.

ProgramCallStack2 en.svg
ProgramCallStack2 en.svg

Computer scientists build stacks in different ways. One way is using an array, which is a list of items in a row. Another way is using a linked list. A linked list uses pointers to connect one item to the next.

Stack UML class diagram.svg
Stack UML class diagram.svg
In a linked list, you can keep adding items until the computer runs out of memory. This means a linked list stack does not usually have an overflow problem. Many programming languages use stacks every day. Languages like Python, JavaScript, and LISP all have ways to use stacks.
Stack Push operation-en.gif
Stack Push operation-en.gif

Stacks are helpful because they follow very strict rules. They have a fixed bottom and a moving top. A stack pointer is a tool that keeps track of where the top is. This is very important for computer hardware. Some machines, like the PDP-11, use special modes to handle stacks. Even small tools like programmable calculators use these concepts to work.

Hewlett-Packard HP-42S, programmable calculator with RPN (combined from two images, cropped).jpg
Hewlett-Packard HP-42S, programmable calculator with RPN (combined from two images, cropped).jpg
Stacks help computers remember where they are in a task. They make it easy to return to a previous step.

474 words

In computer science, a stack is an abstract data type used to organize collections of elements. An abstract data type defines how data behaves rather than how it is physically stored in memory. A stack is a sequential collection with one end that is fixed, called the bottom. The other end is the top, which is the only position where data can be added or removed. This structure follows a specific order known as LIFO, or Last In, First Out. This means the most recent item added to the stack is always the first one to be removed.

Lifo stack.svg
Lifo stack.svg

To understand how a stack works, we can look at its primary operations. The first essential operation is called a push. A push adds a new element to the very top of the collection. The second essential operation is called a pop. A pop removes the most recently added element from the top. There is also a non-essential operation called a peek, or top of stack. A peek allows a user to observe the value of the top element without modifying the stack.

Stack Push operation-en.gif
Stack Push operation-en.gif

Stacks can encounter specific error states based on their capacity. If a stack has a bounded capacity, it can only hold a certain number of items. When the stack is full and cannot accept more elements, it enters a state called stack overflow. Conversely, if a user attempts to perform a pop or a peek operation on an empty stack, it results in a stack underflow. These conditions are critical for programmers to manage to prevent system errors.

Stack Pop Operation-en.gif
Stack Pop Operation-en.gif

History shows that the concept of the stack has evolved since the mid-1940s. In 1946, Alan Turing used the terms "bury" and "unbury" to describe calling and returning from subroutines. By 1945, Konrad Zuse had already implemented subroutines and a two-level stack in his Z4 machine. In 1955, Klaus Samelson and Friedrich L. Bauer proposed an "operational cellar" concept. They filed a patent for this idea in 1957. Later, in March 1988, Bauer received the IEEE Computer Pioneer Award for his work on the stack principle.

ProgramCallStack2 en.svg
ProgramCallStack2 en.svg

Computer scientists implement stacks using different underlying structures. One common method is using an array. In an array-based stack, the program tracks the size using a variable called "top." This variable points to the index where the next element will be inserted. Another method is using a singly linked list. In this implementation, the stack is a pointer to the head of the list. A linked-list stack can grow as long as there is available memory, making overflow unlikely.

Stack UML class diagram.svg
Stack UML class diagram.svg

Many modern programming languages utilize stack operations within their standard libraries. Languages such as Perl, LISP, JavaScript, and Python include push and pop functions for their list types. Some families of languages, like Forth, are designed entirely around stacks that programmers manipulate directly. In the C++ Standard Library, certain container types provide LIFO semantics. Even specialized hardware, like the PDP-11 or Motorola 68000 series, includes addressing modes specifically designed for stack manipulation.

At the hardware level, stacks are vital for managing computer memory and architecture. A stack is typically an area of memory with a fixed origin and a variable size. A specialized tool called a stack pointer keeps track of the most recent location. When a push occurs, the stack pointer is adjusted to accommodate the new data. When a pop occurs, the data is read and the pointer moves back. This mechanism allows processors to efficiently allocate and access memory during complex tasks.

Stack Push operation-en.gif
Stack Push operation-en.gif

609 words
🖼️ Images & Media (9)
File:Tallrik - Ystad-2018.jpg
Tallrik - Ystad-2018.jpg
File:Lifo stack.svg
Lifo stack.svg
File:Stack UML class diagram.svg
Stack UML class diagram.svg
File:Stack Push operation-en.gif
Stack Push operation-en.gif
File:Stack Pop Operation-en.gif
Stack Pop Operation-en.gif
File:Linked-list-based Stack Push operation-en.gif
Linked-list-based Stack Push operation-en.gif
File:Linked-list-based Stack Pop Operation-en.gif
Linked-list-based Stack Pop Operation-en.gif
File:Hewlett-Packard HP-42S, programmable calculator with RPN (combined from two images, cropped).jpg
Hewlett-Packard HP-42S, programmable...
File:ProgramCallStack2 en.svg
ProgramCallStack2 en.svg
Up Next
💻
Queue (abstract data type)
Technology
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.