Stack
Stack
Stacks
§ A stack is a linear data structure that follows LIFO (Last in First Out) order to perform operations.
§ Storing and retrieving operations can be performed only at one of its ends called top or tos (top of
stack).
§ Such a stack resembles piles of trays in a cafeteria. New trays are put on the top of the pile and top
tray is the first tray removed from the stack. For this reason, a stack is called a LIFO structure: last in
/first out.
1
Array implementation of stack
Structure for stack
struct stack
{
int stck[maxsize]; // declaring an array
int top; // top of a stack
}
struct stack s;
Function:
int isEmpty(struct stack *ps)
{
if(ps->top == -1)
return 1;
else
return 0;
}
Function:
int isFull (struct stack *ps)
{
if(ps->top == maxsize -1)
return 1;
else
return 0;
}
2
A. Push Operation:
• The process of insertion of new items onto a stack is called push operation.
• For performing push operation, check to see whether the stack is full or not.
• If we try to push an element onto a full stack, the condition is called stack overflow.
• The stack is full when top = size-1
else
{
printf (“Enter the element to be inserted:”);
scanf(“%d”, &item);
ps->stck[++(ps->top)] = item;
}
}
B. Pop Operation:
• The process of deletion of the topmost item from a stack is called pop operation.
• For performing pop operation, check to see whether the stack is empty or not.
• If we try to pop an element from an empty stack, the condition is called stack underflow.
• The stack is empty when top < 0 or top = -1
Algorithm that pop an ITEM from a stack: Method to pop an item from a stack:
3
Linked List implementation of stack
A linked list of nodes could be used to implement a stack. Linked list representation of stack is nothing but
a liked list of nodes where each node is element of the stack. To point to the top most node we have a
pointer top that points to node recently added. Every new node is added to the beginning of the liked list
and top point to it. The main advantage of this representation is that size of stack is not fixed so there is
least chance of stack overflow.
Structure for stack
struct linked_stack
{
int info;
struct linked_stack *next;
} *top;
Push operation:
Function:
4
POP Operation:
Step 1: Check to see if stack is empty (i.e., top == NULL)
a) If it is empty
print “Stack is empty”
return
b) else
define a node pointer temp
set temp = top
Step 2: Set top = top->next
Step 3: Delete temp i.e., free(temp)
Function:
void pop()
{
struct node *temp;
if (top == NULL)
{
printf(“Stack is empty”);
return;
}
else
{
temp = top;
top = top->next;
free(temp);
}
}
5
Application of Stack:
1. Start
2. read a character ch from file;
3. while not end of file
a) if ch is ‘(’, ‘[’, or ‘{’
push(ch);
b) else if ch is '/'
read the next character;
if this character is '*'
skip all characters until “*/” is found
and report an error if the end of file is reached before “*/” is encountered;
else ch = the character read in;
continue; // go to the beginning of the loop;
c) else if ch is ‘)’, ‘]’, or ‘}’
if ch and popped off delimiter do not match
failure;
d) read next character ch from file;
4. if stack is empty
print success;
else
print failure;
5. Stop
6
2. Evaluating Infix, Prefix and Postfix Expressions:
Infix expression:
• Infix expression is a mathematical notation in which operator is written in between the operands.
• Example: A+B, where A and B are operands and + is an operator.
Prefix expression:
• Prefix expression is a mathematical notation in which the operator preceds the two operands.
• Example: +AB, where A and B are operands and + is an operator.
Postfix expression:
• Infix expression is a mathematical notation in which the operator is written after the operands.
• Example: AB+, where A and B are operands and + is an operator.
Notation Conversions
• Let A + B * C be the given expression, which is an infix notation. To calculate this expression for
values 4, 3, 7 for A, B, C respectively we must follow certain rule (called BODMAS in general
mathematics) in order to have the right result.
• For example: A + B * C = 4 + 3 * 7 = 7 * 7 = 49
• The answer is not correct; multiplication is to be done before the addition, because multiplication
has higher precedence over addition. This means that an expression is calculated according to the
operator’s precedence not the order as they look like.
• The error in the above calculation occurred, since there were no braces to define the precedence of
the operators. Thus, expression A + B * C can be interpreted as A + (B * C).
• Using this alternative method, we can convey to the computer that multiplication has higher
precedence over addition.
7
CONVERTING INFIX TO POSTFIX EXPRESSION:
Example 1: A+B*C
8
Example 2: (A+B)*C
Example 3: ((A-(B+C))*D)$(E+F)
9
Converting Infix expressions to Prefix expressions:
• The precedence rule for converting an infix expression into prefix expression is identical to that of
infix to postfix.
• Only change is that the operator is placed before the operands.
(i) A+B-C
= +AB-C
= -+ABC
(ii) A$B*C-D+E/F/(G+H)
= A$B*C-D+E/F/(+GH)
= $AB*C-D+E/F/(+GH)
= *$ABC-D+E/F/(+GH)
= *$ABC-D+ (/EF)/(+GH)
= *$ABC-D+ (//EF+GH)
= -*$ABCD+ (//EF+GH)
= +-*$ABCD//EF+GH
10
Evaluating the Postfix Expression
Procedures to evaluate postfix expression:
• Each time we read an operand we push it onto a stack.
• When we reach an operator, its operands will be the top two elements on the stack.
• We then pop these two elements from stack and perform the indicated operation on them.
• Then push the result on the stack so that it will be available for use an operand of the next operator.
Examples:
(i) 345*+
= 3 20+ [ multiply 4 and 5]
= 23 [ add 3 and 20]
Tracing:
character op2 op1 result vstack
3 3
4 34
5 345
* 5 4 20 3 20
+ 20 3 23
(ii) 623+-382/+*2^3+
= 6 5 -382/+*2^3+
= 1 382/+*2^3+
= 1 3 4+*2^3+
= 1 7 *2^3+
= 7 2^3+
= 49 3+
= 52
11
623+-382/+*2^3+
(iii) 123+*321-+*
(iv) 48-2569/+1-*-3-
(ii) /+53-42
= / 8 -42
= /8 2
=4
12
Transform each of the following prefix expressions to infix:
(i) +-ABC
= + A-B C
= A-B+C
(ii) ++A-*^BCD/+EF*GHI
(iv) +-^ABC*D**EFG
(i) AB+C-
= A+BC-
= A+B-C
(ii) ABC+-
= AB+C-
= A-B+C
(iii) AB-C+DEF-+^
=A-B+C ^ D+E-F
(iv) ABCDE-+^*EF*-
13