2. WHAT ARE STACKS?
● A stack is an Abstract Data Type (ADT), commonly used
in most programming languages. It is named stack as it
it behaves like a real-world stack, for example – a deck of
cards or a pile of plates, etc.
ABSTRACT DATA TYPE
An abstract data type is defined as a mathematical model of
the data objects that make up a data type as well as the
functions that operate on these objects. There are no standard
conventions for defining them. A broad division may be drawn
between "imperative" and "functional" definition styles.
3. STACK
REPRESENTATION
A stack can be implemented
by means of Array,
Structure, Pointer, and
Linked List. Stack can
either be a fixed size one or
it may have a sense of
dynamic resizing. Here, we
are going to implement
stack using arrays, which
makes it a fixed size stack
implementation.
4. BASIC OPERATIONS
● Stack operations may involve initializing the stack, using
it and then de-initializing it. Apart from these basic stuffs,
a stack is used for the following two primary operations : -
push() − Pushing (storing) an element on the stack.
pop() − Removing (accessing) an element from the stack.
● Data is either PUSHed onto stack or POPed out of the
stack.
5. ● To use a stack efficiently, we need to check the
status of stack as well. For the same purpose, the
following functionality is added to stacks : -
peek() − get the top data element of the stack, without
removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
● At all times, we maintain a pointer to the last PUSHed
data on the stack. As this pointer always represents the
top of the stack, hence named top. The top pointer provides
top value of the stack without actually removing it.
6. BASIC FEATURES OF
STACK
● Stack is an ordered list of similar data type.
● Stack is a LIFO structure. (Last in First out).
● push() function is used to insert new elements into the Stack
● pop() function is used to delete an element from the stack.
● Both insertion and deletion are allowed at only one end
of Stack called Top.
● Stack is said to be in Overflow state when it is completely
full and is said to be in Underflow state if it is completely
empty.
7. HARDWARE STACK
A common use of stacks at the architecture level is as a means
of allocating and accessing memory.
● STACK IN MAIN MEMORY
1. Many CISC - type CPU designs, including the x86, Z80
and 6502, have a dedicated register for use as the call
stack, stack pointer with dedicated call, return, push,
and pop instructions that implicitly update the dedicated
register, thus increasing code density.
2. Some CISC processors, like the PDP-11 and the 68000,
also have special addressing modes for implementation of
stacks, typically with a semi-dedicated stack pointer as well.
8. STACK IN REGISTERS/
DEDICATED MEMORY
● The x87 floating point architecture is an example of a set
of registers organised as a stack where direct access
to individual registers is also possible. As with stack-based
machines in general, having the top-of-stack as an implicit
argument allows for a small machine code footprint with
a good usage of bus bandwidth and code caches, but it also
prevents some types of optimizations possible on processors
permitting random access to the register file for all operand.
● A stack structure also makes superscalar implementations
with register renaming somewhat more complex to
implement.
9. IMPLEMEMTATION OF
STACK
Stack can be easily implemented using an Array or a Linked
List. Arrays are quick, but are limited in size and Linked List
requires overhead to allocate, link, unlink, and deallocate, but
is not limited in size. Here we will implement Stack using
array.
10. PUSH OPERATION
The process of putting a new data element onto stack is known
as a Push Operation. Push operation involves a series of steps :
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next
empty space.
Step 4 − Adds data element to the stack location, where top
is pointing.
Step 5 − Returns success.
11. If the linked list is used to implement the stack, then in step 3,
we need to allocate space dynamically.
12. POP OPERATION
Accessing the content while removing it from the stack, is
known as a Pop Operation. In an array implementation of
pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to
point to the next value. But in linked-list, pop() actually
removes data element and deallocates memory space.
A Pop operation may involve the following steps −
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
13. Step 3 − If the stack is not empty, accesses the data
element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
14. APPLICATIONS OF
STACK
● EXPRESSION EVALUATION AND SYNATX
PARSING
Calculators employing reverse Polish notation use a stack
structure to hold values. Expressions can be represented in
prefix, postfix or infix notations and conversion from one
form to another may be accomplished using a stack. Many
compilers use a stack for parsing the syntax of expressions,
program blocks etc. before translating into low level code.
Most programming languages are context-free languages,
allowing them to be parsed with stack based machines.
15. ● BACKTRACKING
Another important application of stacks is backtracking.
Consider a simple example of finding the correct path
in a maze. There are a series of points, from the
starting point to the destination. We start from one point. To
reach the final destination, there are several paths. Suppose
we choose a random path. After following a certain path, we
realise that the path we have chosen is wrong. So we need
to find a way by which we can return to the beginning of
path. This can be done by stacks. With the help of stacks,
we remember the point where we have reached. This is
done by pushing that point into the stack. In case we end
up on the wrong path, we can pop the last point from the
stack and thus return to the last point and continue our
quest to find the right path. This is called backtracking.
16. ● The simplest application of a stack is to reverse a word.
You push a given word to stack - letter by letter - and then
pop letters from the stack.
● COMPILE TIME MEMORY MANAGEMENT
A number of programming languages are stack - oriented
meaning they define most basic operations as taking
their arguments from the stack, and placing any return
values back on the stack. For example, PostScript has a
return stack and an operand stack, and also has a graphics
state stack and a dictionary stack. Many virtual machines
are also stack- oriented including the p-code machine and
the Java Virtual Machine.
17. ● TOWERS OF HANOI
One of the most interesting applications of stacks can be found
in solving a puzzle called Tower of Hanoi. According to an old
Brahmin story, the existence of the universe is calculated in
terms of the time taken by a number of monks, who are
working all the time, to move 64 disks from one pole to
another. But there are some rules about how this should be
done, which are:
- You can move only one disk at a time.
- For temporary storage, a third pole may be used.
- You cannot place a disk of larger diameter on a disk of
smaller diameter.
- Here we assume that A is first tower, B is second tower &
C is third tower.