Stacks
Stacks
Dr Athira B
Assistant Professor
Department of Computer Science and Engineering
IIIT Kottayam
Data Structures
A data structure is a data organization, management, and storage format that
enables efficient access and modification.
Types
Linear and Non Linear Data Structures
Linear Data Structures Non Linear Data Sructures
In a linear data structure, data elements are arranged in a In a non-linear data structure, data elements are attached
linear order where each and every elements are attached in hierarchically manner.
to its previous and next adjacent.
In linear data structure, single level is involved. Whereas in non-linear data structure, multiple levels are
involved.
Its implementation is easy in comparison to non-linear While its implementation is complex in comparison to
data structure. linear data structure.
In linear data structure, data elements can be traversed in While in non-linear data structure, data elements can’t be
a single run only. traversed in a single run only.
In a linear data structure, memory is not utilized in an While in a non-linear data structure, memory is utilized
efficient way. in an efficient way.
Its examples are: array, stack, queue, linked list, etc. While its examples are: trees and graphs.
Applications of linear data structures are mainly in Applications of non-linear data structures are in Artificial
application software development. Intelligence and image processing.
Difference between abstract and concrete data
type
Abstract data type is a type (or class) of objects whose behavior is defined
by a set of values and a set of operations. The user interacts with the
interface, using the operations that have been specified by the abstract
data type. It offers a high level use of a concept independent of it’s
implementation. They package data structure and operations on them
hiding internal details.
Eg: stack, queue
A concrete data type is a data type whose representation is known.
Concrete data types or structures (CDT’s) are direct implementations of a
relatively simple concept.
Arrays, structure, etc.
Stack
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages.
• It is named stack as it behaves like a real-world stack.
• Example – a deck of cards or a pile of plates.
Examples of stack
• Stack allows operations at one end only
• At any given time, we can only access the top element of a stack.
• This feature makes it LIFO data structure.
• LIFO stands for Last-in-first-out.
• The element which is placed last, is accessed first.
• In stack terminology, insertion operation is called PUSH operation
and removal operation is called POP operation.
Stack Representation
Operations on stack
• Push()-insertion
• Pop()-deletion
• Let Stack[ ] is the stack.
• Top is the integer denoting top index of the stack.
• N is the size of the stack.
Push()
Push()
• We cannot insert elements into a stack which is already filled.
• Hence before insertion we need to check whether the stack is full or
not.
• The condition where stack is full is called overflow.
• If the stack is not full push the element into the stack.
• Top=-1 initially
Checking for
stackFull() push()
int stackFull() void push(int item)
{
{
if(!stackFull())
if (Top>=N-1) {
return 1; stack[++top]=item
else }
else
return 0;
{
} printf(“Stack overflow”);
}
}
Pop()
• We can delete only from a non empty stack.
• Hence before deletion we need to check whether the stack is empty
or not.
• The condition where stack is empty is called underflow.
Pop Operation
Checking for stackEmpty() Pop()
Void pop()
int stackEmpty()
{
{
if(!StackEmpty())
if (Top==-1) {
return 1; return stack[top];
else top=top-1
return 0 }
} else
printf(“Stack underflow”);
}
Applications:
• Temporary storage structure for recursive operations
• Auxiliary storage structure for nested operations, function calls, deferred/postponed
functions
• Manage function calls
• Evaluation of arithmetic expressions in various programming languages
• Conversion of infix expressions into post fix expressions
• Checking syntax of expressions in a programming environment
• Matching of parenthesis
• String reversal
• In all the problems solutions based on backtracking.
• Used in depth first searching graph and tree traversal.
• Operating System functions
• UNDO and REDO functions in an editor.
• Advantages of Stack
• A Stack helps to manage the data in the ‘Last in First out’ method.
• When the variable is not used outside the function in any program, the Stack
can be used.
• It allows you to control and handle memory allocation and deallocation.
• It helps to automatically clean up the objects.
• Disadvantages of Stack
• It is difficult in Stack to create many objects as it increases the risk of the Stack
overflow.
• It has very limited memory.
• In Stack, random access is not possible.
Matching Paranthesis
recurse(int 3) {
if (3 > 1) recurse (3-1);
printf("%d", 3);
}
recurse(int 1) {
if (1 > 1) recurse (1-1);
printf("%d", 1); recurse(int 2) {
if (2 > 1) recurse (2-1);
printf("%d", 2);
}
recurse(int 3) {
if (3 > 1) recurse (3-1);
printf("%d", 3);
}
• Print - 1 2 3
Infix, Prefix and Postfix Expressions
Infix
Postfix
operand operator operand
operand operand operator
A+B
A+B+C AB+
A *( B + C * D ) + E AB+ C+
Prefix ABCD * + * E+
* + * CDBA + E
Solving Postfix
• Solve 2 3 + 4 5 + *
• Answer:45