UNIT-3 Stack
UNIT-3 Stack
An abstract data type is an abstraction of a data structure that provides only the
interface to which the data structure must adhere. The interface does not give any
specific details about something should be implemented or in what programming
language.
In other words, we can say that abstract data types are the entities that are definitions of
data and operations but do not have implementation details. In this case, we know the
data that we are storing and the operations that can be performed on the data, but we
don't know about the implementation details. The reason for not having implementation
details is that every programming language has a different implementation strategy for
example; a C data structure is implemented using structures while a C++ data structure
is implemented using objects and classes.
STACK
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle.
Stack has one end, whereas the Queue has two ends (front and rear).
It contains only one pointer top pointer pointing to the topmost element of the stack.
Whenever an element is added in the stack, it is added on the top of the stack, and the
element can be deleted only from the stack.
In other words, a stack can be defined as a container in which insertion and deletion can
be done from the one end known as the top of the stack.
Some key points related to stack
• It is called as stack because it behaves like a real-world stack, piles of books, etc.
• A Stack is an abstract data type with a pre-defined capacity, which means that it can store
the elements of a limited size.
• It is a data structure that follows some order to insert and delete the elements, and that order
can be LIFO or FILO.
Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure there are five
memory blocks in the stack; therefore, the size of the stack is 5.
Suppose we want to store the elements in a stack and let's assume that stack is empty.
We have taken the stack of size 5 as shown below in which we are pushing the
elements one by one until the stack becomes full.
Since our stack is full as the size of the
stack is 5. In the above cases, we can
observe that it goes from the top to the
bottom when we were entering the new
element in the stack. The stack gets
filled up from the bottom to the top.
Standard Stack Operations
The following are some common operations implemented on the stack:
• push(): When we insert an element in a stack then the operation is known as a push. If the
stack is full then the overflow condition occurs.
• pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an
underflow state.
• isEmpty(): It determines whether the stack is empty or not.
• isFull(): It determines whether the stack is full or not.'
• peek(): It returns the element at the given position.
• count(): It returns the total number of elements available in a stack.
• change(): It changes the element at the given position.
• display(): It prints all the elements available in the stack.
Implementing Stack in C
Applications of Stack
The infix notation is a convenient way of writing an expression in which each operator
is placed between the operands. Infix expressions can be parenthesized or
unparenthesized depending upon the problem requirement.
Example: A + B, (C - D) etc.
All these expressions are in infix notation because the operator comes between the
operands.
Prefix Notation
The prefix notation places the operator before the operands. This notation was
introduced by the Polish mathematician and hence often referred to as polish notation.
Example: + A B, -CD etc.
All these expressions are in prefix notation because the operator comes before the
operands.
Postfix Notation
The postfix notation places the operator after the operands. This notation is just the
reverse of Polish notation and also known as Reverse Polish notation.
Example: AB +, CD+, etc.
All these expressions are in postfix notation because the operator comes after the
operands.
Conversion of Arithmetic Expression into
various Notations:
Stack is the ideal data structure to evaluate the postfix expression because the top
element is always the most recent operand. The next element on the Stack is the second
most recent operand to be operated on.
Before evaluating the postfix expression, the following conditions must be checked. If
any one of the conditions fails, the postfix expression is invalid.
• When an operator encounters the scanning process, the Stack must contain a pair of
operands or intermediate results previously calculated.
• When an expression has been completely evaluated, the Stack must contain exactly one
value.
Example:
Now let us consider the following infix expression 2 * (4+3) - 5.
Its equivalent postfix expression is 2 4 3 + * 5.
The following step illustrates how this postfix expression is evaluated.
2. Backtracking
One of the algorithm design techniques is backtracking. For that aim, we dig into one
path; if that path is inefficient, we return to the previous state and explore alternative
options. We must save the previous state in order to return from the current state.
Stack is required for this purpose. A famous example is N-queens problems.
3. Delimiter Checking
The common application of Stack is delimiter checking, i.e., parsing that involves
analyzing a source program syntactically.
It is also called parenthesis checking. When the compiler translates a source program
written in some programming language such as C, C++ to a machine language, it parses
the program into multiple individual parts such as variable names, keywords, etc.
By scanning from left to right. The main problem encountered while translating is the
unmatched delimiters.
We make use of different types of delimiters include the parenthesis checking (,), curly
braces {,} and square brackets [,], and common delimiters /* and */.
Every opening delimiter must match a closing delimiter, i.e., every opening parenthesis
should be followed by a matching closing parenthesis.
Valid Delimiter Invalid Delimiter
{ ( a + b) - c } { ( a + b) - c
To reverse a given set of data, we need to reorder the data so that the first and last
elements are exchanged, the second and second last element are exchanged, and so on
for all other elements.
Example: Suppose we have a string Welcome, then on reversing it would be Emoclew.
5. Processing Function Calls:
Stack plays an important role in programs that call several functions in succession.
Suppose we have a program containing three functions: A, B, and C. function A invokes
function B, which invokes the function C.
When we invoke function A, which contains a call to function B, then its processing will not
be completed until function B has completed its execution and returned. Similarly for
function B and C. So we observe that function A will only be completed after function B is
completed and function B will only be completed after function C is completed. Therefore,
function A is first to be started and last to be completed. To conclude, the above function
activity matches the last in first out behavior and can easily be handled using Stack.