The Stack
• A stack is one of the most important and useful non primitive linear data structure in computer
science.
• As all the deletion and insertion in a stack is done from top of the stack, the last added element will
be the first to be removed from the stack.
• This is the reason why the stack is also called Last In – First Out (LIFO) type of list.
• It is interesting to notice that the most frequently accessible element in the stack are the most top
elements, while the least accessible elements located at the bottom of the stack.
• In our everyday life, we come across many examples of stacks shown in the figure below
Consider a stack of books on a table. We can easily put a new book on the top of the stack,
and similarly, we can easily remove the topmost book as compared to the books lying in-
between or at the bottom positions. In the same way, only the topmost element of a stack
can be accessed while direct access of other intermediate positions is not feasible. Elements
may be added to or removed. from only one end, called the top of a stack.
Stack can be implemented in two ways:
1. Static implementation (using arrays)
2. Dynamic implementation (using pointers)
Static implementation uses arrays to create stack. Static implementation using
arrays is a very simple technique but is not a flexible way, as the size of the stack
has to be declared during the program design, because after that, the size cannot
be varied.
The insertion (or addition) operation is referred to as push, and the deletion (or
remove) operation as pop. A stack is said to be empty or underflow, if the stack
contains no elements. At this point the top of the stack is present at the bottom of
the stack.
And it is overflow when the stack becomes full, i.e., no other elements can be
pushed onto the stack. At this point the top pointer is at the highest location of
the stack.
Operations Performed on Stack
There are two basic operations that can be performed on the stack,
these are:
1. PUSH: is the process of adding a new element to the top of the stack.
As a new element pushed to the stack, top will be incremented by one
and denote to the added element. Adding a new element when the
stack is full is called stack overflow.
2. POP: is the process of deleting an element from the top of the stack.
After every pop operation, the top is decremented by one. If there are
no elements in the stack and pop operation is performed, the result is
called stack underflow.
Stack Terminology:
1. Size: this term refers to the maximum size of the stack or the number
of possibly added elements.
2. TOP: this term refers to the top of the stack. It is a stack pointer used
to check overflow and under flow conditions. The initial value of TOP
is -1 when the stack is empty.
3. Stack underflow: is the situation when the stack contains no
elements. At this point the top of the stack points to the stack bottom.
4. Stack overflow: is the situation when the stack is full and no more
elements can be added. At this point the top of the stack points to the
highest location in the stack.
Example of push and pop operations.
It is an ordered collection of items
into which new data items may be
added/inserted and from which
items may be deleted at only one
end, called the top of the stack.
Exercise:
Example: What is the obtained string after performing the following sequence of
push and pop:
PUSH(A), PUSH(B), POP, PUSH(C), POP, PUSH(D), PUSH(E), POP,
POP, POP
Solution:
So, the obtained string is (BCEDA)
Example: What is the obtained number after performing the following sequence of
push and pop:
PUSH(1), POP, PUSH(2), PUSH(3), POP, PUSH(4), POP, PUSH(5), POP,
POP
Solution:
So the obtained number is (13452).
Example: What is the required sequence of push and pop to obtain the string
(CBDAE)
from the initial input (ABCDE).
Solution:

The Stack (Data Structccccccccccccccccccc

  • 1.
    The Stack • Astack is one of the most important and useful non primitive linear data structure in computer science. • As all the deletion and insertion in a stack is done from top of the stack, the last added element will be the first to be removed from the stack. • This is the reason why the stack is also called Last In – First Out (LIFO) type of list. • It is interesting to notice that the most frequently accessible element in the stack are the most top elements, while the least accessible elements located at the bottom of the stack. • In our everyday life, we come across many examples of stacks shown in the figure below
  • 2.
    Consider a stackof books on a table. We can easily put a new book on the top of the stack, and similarly, we can easily remove the topmost book as compared to the books lying in- between or at the bottom positions. In the same way, only the topmost element of a stack can be accessed while direct access of other intermediate positions is not feasible. Elements may be added to or removed. from only one end, called the top of a stack.
  • 3.
    Stack can beimplemented in two ways: 1. Static implementation (using arrays) 2. Dynamic implementation (using pointers) Static implementation uses arrays to create stack. Static implementation using arrays is a very simple technique but is not a flexible way, as the size of the stack has to be declared during the program design, because after that, the size cannot be varied. The insertion (or addition) operation is referred to as push, and the deletion (or remove) operation as pop. A stack is said to be empty or underflow, if the stack contains no elements. At this point the top of the stack is present at the bottom of the stack. And it is overflow when the stack becomes full, i.e., no other elements can be pushed onto the stack. At this point the top pointer is at the highest location of the stack.
  • 4.
    Operations Performed onStack There are two basic operations that can be performed on the stack, these are: 1. PUSH: is the process of adding a new element to the top of the stack. As a new element pushed to the stack, top will be incremented by one and denote to the added element. Adding a new element when the stack is full is called stack overflow. 2. POP: is the process of deleting an element from the top of the stack. After every pop operation, the top is decremented by one. If there are no elements in the stack and pop operation is performed, the result is called stack underflow.
  • 5.
    Stack Terminology: 1. Size:this term refers to the maximum size of the stack or the number of possibly added elements. 2. TOP: this term refers to the top of the stack. It is a stack pointer used to check overflow and under flow conditions. The initial value of TOP is -1 when the stack is empty. 3. Stack underflow: is the situation when the stack contains no elements. At this point the top of the stack points to the stack bottom. 4. Stack overflow: is the situation when the stack is full and no more elements can be added. At this point the top of the stack points to the highest location in the stack.
  • 6.
    Example of pushand pop operations.
  • 7.
    It is anordered collection of items into which new data items may be added/inserted and from which items may be deleted at only one end, called the top of the stack.
  • 8.
  • 9.
    Example: What isthe obtained string after performing the following sequence of push and pop: PUSH(A), PUSH(B), POP, PUSH(C), POP, PUSH(D), PUSH(E), POP, POP, POP Solution: So, the obtained string is (BCEDA)
  • 10.
    Example: What isthe obtained number after performing the following sequence of push and pop: PUSH(1), POP, PUSH(2), PUSH(3), POP, PUSH(4), POP, PUSH(5), POP, POP Solution: So the obtained number is (13452).
  • 11.
    Example: What isthe required sequence of push and pop to obtain the string (CBDAE) from the initial input (ABCDE). Solution: