Uploaded by Atharva Biyani

2. Stack

advertisement
Chapter Two: Stacks
What is a Stack?
A stack is a very intuitive data structure. It’s pretty much exactly how it sounds. Let’s
take a simple analogy: A stack of papers. In the following example, a sheet of paper is
analogous to a single element.
When we stack paper (which, if you’re superswe, is often), we place the new paper on
top of the stack, thus obstructing the previous top of the stack with another paper. In a
Stack data structure, this is called a push() operation. Note that when we push an
element onto the stack, we cannot access the elements without first removing the
elements on top of it (more on this later). We can look at the top element (i.e., the paper
at the top of the stack) by performing a peek() operation. Here’s a visual to demonstrate
this idea:
Now that we’ve “covered” element 100 with element 2, how can we look through the
stack to find 100? Well, that’s one of the limitations of stacks – we can’t. To reach 100,
we must first remove 2. The removal of an element from a stack is called a pop()
operation. In most languages, pop() will also return the top element. There is a visual to
assist on the next page.
These three operations (push, pop, and peek) make up the main operations of a stack.
Stacks, therefore, use a Last-In, First-Out (LIFO) queuing discipline. This means that
the last element put in the stack is the first to be removed. The way I like to think of it is,
stacks are “unfair” – the element who has been in the stack for longest is the last to be
removed.
Make sure to remember this term, LIFO, and what it means. This is an important
terminology to know.
Common applications for stacks include:

Converting postfix expressions (this is a common interview question!)

Balancing symbols (e.g., [brackets], (parenthesis))
o This is also an interview question.

Allocating memory for function calls (used in recursion!)

Tracking undo and redo in editors, photoshop, etc.
Note: Some languages do not have a standard stack; however, you can usually use
push (append in python) and pop functions with a list structure to similar effect (for
LeetCode questions). Look into your specific language for this.
Required Work
Complete this work before moving to the next chapter.
1. Read Introduction to Stack - Data Structure and Algorithm Tutorials - GeeksforGeeks and
implement a stack.
-
Review all operations + pseudocode.
-
Understand the array implementation.
-
Implement the stack yourself using an array. You will probably do this in your Data Structures
course as well.
o
You can start by using this coding problem.

Note: The coding problem is not as in-depth as an implementation in your own
IDE. However, it is a good start for the push and pop functions.
o
Then, implement it:

Recommended to do this phase in Java, C++, or another language with an array
structure. This is because it is too easy to implement with a list structure (like in
Python).

Do not copy word-for-word; use the video guides below as a means of
understanding the implementation.
-

Video Guide for Java

Video Guide for C++
Note: You will not yet understand the Linked List implementation. This is OK. We go over
linked lists later, and you’ll have a chance to revisit this implementation.
2. Investigate your programming language’s stack structure. Note that many programming languages
do not natively support stacks; in this case, look into your language’s list structure, and how you can
use it as a Stack. It is imperative you know this for coding problems on GeeksForGeeks and
LeetCode. Here are a few resources for popular languages:
-
Python Lists - GeeksforGeeks – See Append() and Pop() functions
o
Also look at Deque in Python - GeeksforGeeks

Performs more efficiently than lists when used as a stack.

Note that dequeue has extra functionality, but can be used as a stack
-
Vector in C++ STL - GeeksforGeeks – See push_back() and pop_back()
o
Also look at Stack in C++ STL - GeeksforGeeks

-
Preferable to vector, but should understand vector as well
Stack Class in Java - GeeksforGeeks
o
Java’s ArrayList is un-intuitive for stack operations; better to use Stack class.
3. Complete the following exercises, using stack (if using a list, only stack operations – push and pop):
Reverse a string using Stack | Practice | GeeksforGeeks
Reverse each word in a given string | Practice | GeeksforGeeks
There are more stack-related exercises at the end of the next chapter.
Download