0% found this document useful (0 votes)
95 views

Stacks

The document discusses stacks, which are abstract data structures that follow the LIFO (last-in, first-out) principle. Stacks allow efficient insertion and removal of elements from one end only. Common stack operations are push to insert and pop to remove elements. Stacks have applications in parsing, expression conversion, and reversing words. The document also covers stack implementation in C using arrays, along with functions for push, pop, display, and other operations.

Uploaded by

Shailen.dra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Stacks

The document discusses stacks, which are abstract data structures that follow the LIFO (last-in, first-out) principle. Stacks allow efficient insertion and removal of elements from one end only. Common stack operations are push to insert and pop to remove elements. Stacks have applications in parsing, expression conversion, and reversing words. The document also covers stack implementation in C using arrays, along with functions for push, pop, display, and other operations.

Uploaded by

Shailen.dra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Stack

• Stack is an abstract data type with a bounded(predefined) capacity. Stacks are


also called as dynamic data structures that follow the Last In First Out
(LIFO) principle. It is a simple data structure that allows adding and removing
elements in a particular order. Every time an element is added, it goes on
the top of the stack and the only element that can be removed is the element that
is at the top of the stack.
Basic Features of Stack
•Stack is an ordered list of similar data type.
•Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
•push() function is used to insert new elements into the Stack and pop() function is used to remove an
element from the stack. Both insertion and removal 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.

Applications of Stack
•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.
•There are other uses also like:
•Parsing
•Expression Conversion(Infix to Postfix, Postfix to Prefix etc)
Push Operations on Stack
Algorithm to push an item into
stack
1) IF TOP = MAX then Print
“Stack is full”;
2)Exit;
3)Otherwise TOP: = TOP + 1;
/*increment TOP*/
STACK (TOP):= ITEM; 3) End
of IF
4) Exit
Pop Operation from Stack
POP_STACK(STACK,TOP,ITEM)
Algorithm to pop an element
from stack.

1)IF TOP = 0 then Print “Stack


is empty”; Exit;
2)ITEM:=STACK(TOP);
3) TOP:=TOP – 1;
4)3) End of IF
5)4) Exit
C Program for Stack Operations
• #include <stdio.h>
• #include <stdlib.h>
• {
• /*clrscr();*/
• printf("Enter Choice (1: display, 2: insert (PUSH), 3: remove(POP)), 4: Exit..:");
• scanf("%d",&choice);
• switch(choice)
• {
• case 1:
• display(STACK);
• break;
• case 2:
• printf("Enter Item to be insert :");
• scanf("%d",&ITEM);
• PUSH(STACK,ITEM);
• break;
• case 3:
• POP(STACK);
• break;
• case 4:
• exit(0);
•             default:
• printf("\nInvalid choice.");
• break;
• getch();
• }// end of while(1)

Push Operation on Stack
•   to push an item into stack.
• */
• void PUSH(int stack[],int item)
• {
• if(TOP==MAX-1)
• {
• printf("\nSTACK is FULL CAN't ADD ITEM\n");
• return;
• }
• TOP++;
• stack[TOP]=item;
• }
Pop operation on Stack
•/* function : POP(),
•    to pop an item from stack.
•*/
•void POP(int stack[])
•{
• int deletedItem;
• if(TOP==-1)
• {
• printf("STACK is EMPTY.\n");
• return;
• }

• deletedItem=stack[TOP];
• TOP--;
• printf("%d deleted successfully\n",deletedItem);
• return;
•}

Display Operation on Stack
• /* function : display(),
•     to display stack elements.
• */
• void display(int stack[])
•{
• int i=0;
• if(TOP==-1)
• {
• printf("Stack is Empty .\n");
• return;
• }

• printf("%d <-- TOP ",stack[TOP]);
• for(i=TOP-1;i >=0;i--)
• {
• printf("\n%d",stack[i]);
• }
• printf("\n\n");
•}

•   
Application of Stacks
Infix Notation
• A + B, this is an infix expression because the
operator “+” comes between operands “A” and “B”.
• To evaluate expressions manually infix notation is
helpful as it is easily understandable by the human
brain.
• But infix expressions are hard to parse in a computer
program hence it will be difficult to evaluate
expressions using infix notation. To reduce the
complexity of expression evaluation Prefix or Postfix
expressions are used in the computer programs.
Postfix expressions
Postfix expressions - operators come after the operands. Below are an infix and respective
Postfix expressions.
A+ B →AB +

Prefix notation (also known as "Polish notation"): Operators are written before their operands.
X+y -> +XY
Sr.No. Infix Prefix Postfix
Notation Notation Notation

1 a+b +ab ab+


2 (a + b) ∗ ∗+abc ab+c∗
c

3 a ∗ (b + ∗a+bc abc+∗
c)

4 a/b+c/ +/ab/c ab/cd/


d d +

5 (a + b) ∗ ∗+ab+ ab+cd
(c + d) cd +∗

6 ((a + b) ∗ - ∗ + a b ab+c∗
Operator Precedence
• When an operand is in between two different
operators, which operator will take the operand
first, is decided by the precedence of an
operator over others. For example −

• As multiplication operation has precedence over


addition, b * c will be evaluated first. A table of
operator precedence is provided later.
Associativity

Associativity describes the rule where operators with the same precedence appear in an expression. For example, in expression
a + b − c, both + and – have the same precedence, then which part of the expression will be evaluated first, is determined by
associativity of those operators. H
ere, both + and − are left associative, so the expression will be evaluated as (a + b) − c.
Precedence and associativity determines the order of evaluation of an expression. Following is an operator precedence and
associativity table (highest to lowest) −

Sr.No. Operator Precedence Associativity


1 Exponentiation ^ Highest Right Associative
2 Multiplication ( ∗ ) Second Highest Left Associative
& Division ( / )
3 Addition ( + ) & Lowest Left Associative
Subtraction ( − )
To convert an infix to postfix expression

• Step 1: Reverse the infix expression i.e A+B*C


will become C*B+A. Note while reversing each
‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
• Step 2: Obtain the postfix expression of the
modified expression i.e CB*A+.
• Step 3: Reverse the postfix expression. Hence
in our example prefix is +A*BC.
Infix to Prefix Conversion

Algorithm of Infix to Prefix


1.Step 1. Push “)” onto STACK, and add “(“ to end of the A
2.Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the
STACK is empty
3.Step 3. If an operand is encountered add it to B
4.Step 4. If a right parenthesis is encountered push it onto STACK
5.Step 5. If an operator is encountered then:
6.a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which
has same
7.or higher precedence than the operator.
8.b. Add operator to STACK
9.Step 6. If left parenthesis is encontered then
10.a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a
left parenthesis is encounterd)
11.b. Remove the left parenthesis
12.Step 7. Exit

You might also like