0% found this document useful (0 votes)
14 views1 page

Stack Introduction in Stacks and Queues

A stack is a data structure that operates on a Last In, First Out (LIFO) principle, meaning the last item added is the first one to be removed. Key operations include 'push' to add an item, 'pop' to remove the top item, and 'peek' to view the top item without removing it. Additionally, one can check if the stack is empty and track its size as items are added or removed.

Uploaded by

Asif Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views1 page

Stack Introduction in Stacks and Queues

A stack is a data structure that operates on a Last In, First Out (LIFO) principle, meaning the last item added is the first one to be removed. Key operations include 'push' to add an item, 'pop' to remove the top item, and 'peek' to view the top item without removing it. Additionally, one can check if the stack is empty and track its size as items are added or removed.

Uploaded by

Asif Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1/26/25, 4:12 PM Stack Introduction in Stacks and Queues

Stack Introduction
A stack data structure is a collection that follows a specific order in which
operations are performed. The order is Last In, First Out (LIFO). This means
that the last element added to the stack will be the first one to be removed.

To visualize a stack, consider a pile of books placed one on top of the other.
Here's how the stack data structure would work in this analogy:

Adding a book (Push operation): When you add a new book to the stack,
you place it on top of the existing pile. This is similar to the "push" operation
in a stack data structure. You can only add (push) a new book at the top of
the pile, not in the middle or at the bottom.

Removing a book (Pop operation): When you want to remove a book from
the stack, you always take it from the top. This is like the "pop" operation.
You can't remove a book from the middle or the bottom without first
removing all the books on top of it. Popping a book off the stack reveals the
book that was previously underneath it, which you can then read or pop off
next.

Peeking at the top book (Peek operation): If you want to check which book
is currently at the top of the stack without removing it, you can take a quick
look. In stack terminology, this is called a "peek" operation, where you're
inspecting the element at the top of the stack without modifying the stack
itself.

Checking if the stack is empty: If you've removed all the books from the
pile and there are none left, your stack of books is now empty. In the stack
data structure, you can perform an operation to check whether the stack is
empty before attempting to pop, which would be an error since there's
nothing to remove.

Size of the stack: The number of books in the pile at any given time
corresponds with the size of the stack. As you add (push) and remove (pop)
books, the size of your stack changes.

In the stack of books analogy it's clear that you have access only to the
https://www.codechef.com/learn/course/stacks-and-queues/LSTACKS/problems/STACK08 1/1

You might also like