0% found this document useful (0 votes)
7 views32 pages

DS-Chapter - 3

The document provides an overview of stacks, a Last In, First Out (LIFO) data structure, detailing its operations such as push and pop. It discusses stack implementations, both static and dynamic, and highlights various applications of stacks, including reversing strings and checking the validity of expressions. Additionally, it covers the conversion of infix expressions to postfix notation.

Uploaded by

mabbaskhan771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

DS-Chapter - 3

The document provides an overview of stacks, a Last In, First Out (LIFO) data structure, detailing its operations such as push and pop. It discusses stack implementations, both static and dynamic, and highlights various applications of stacks, including reversing strings and checking the validity of expressions. Additionally, it covers the conversion of infix expressions to postfix notation.

Uploaded by

mabbaskhan771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

A

Data Structure
What is a Stack

Stack Operations

 Push Operation

 Pop Operation
stack
A stack is a
Last In, First
Out (LIFO) data
structure,
objects
inserted last
are the first to
come out of
the stack.
What is a stack?

Stores a set of elements in a particular


order
Stack principle: LAST IN FIRST OUT
= LIFO
It means: the last element inserted is the
first one to be removed
Example
What is a Stack?
Anything added to the stack goes on the “top” of the
stack

Anything removed from the stack is taken from the


“top” of the stack

Things are removed in the reverse order from that in


which they were inserted
2.Stack
A Stack is a list of elements in which
an element may be inserted or
deleted at one end which is known
as TOP of the stack.
Last In First Out

E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Operation
Performed on
Stack
Push: add an element in stack
Pop: remove an element in stack
A LIFO Stack
Push Pop

Stack Pointer Top

Bottom
Array-based Stack Implementation
Allocate an array of some size (pre-defined)
Maximum N elements in stack
Bottom stack element stored at element 0
last index in the array is the top
Increment top when one element is pushed,
decrement after pop
Stack implementation

Stacks

static dynamic arrays


array
Parenthesi Graphical
s checker region fill
Stack implementation in two way:
Static implementation: these
implementation uses arrays to create
stack. Static implementation though a
very simple technique but is not a flexible
way of creation as the size of stack has to
be declared during program design after
that can not be varied. Static
implementation is not too efficient with
respect to memory utilization.
Dynamic implementation: these
implementation is also called link list
implementation and uses pointer to
implement the stack type of data
of Stack
A[1] A[2] …. …. …. A[n]

Push X w y x

Stack pointer Stack pointer After Stack pointer


J=2 J=3 many J=n
Before Push After Push Push Ops Stack Full

Pop w y

After many Pop ops

Stack Pointer Stack Pointer Stack Pointer


J=0 J=2 J=3
Stack Empty After Pop Before Pop
#include<iostream.h>
#include<conio.h>
Void main();
{
clrscr();
int a[5];
cout<<“Enter 4 Values for Stack”<<endl;
for(int i=0; i<=4; i++)
cin>>a[i];
cout<<“Stack Overflow”<<endl;
getch();
}
#include<iostream.h>
#include<conio.h>
Void main();
{
clrscr();
int a[5];
cout<<“Enter 4 Values for Stack”<<endl;
for(int i=4; i>=0; i--)
cin>>a[i];
cout<<“Stack Underflow”<<endl;
getch();
}
ALGORITHM OF DISPLAY IN STACK:

1.Display(top,i,a[i])
2.If top=0 then
Print ‘STACK EMPTY’
Exit
Else
3.For i=0 to top
Print a[i]
End for
4.exit
Data Structures: A Pseudocode Approach with 22
Stack Applications
Real life
Pile of books
Plate trays
More applications related to computer
science
Evaluating expressions
APPLICATIONS OF STACKS ARE:

I. Reversing Strings:
• A simple application of stack is reversing strings.
To reverse a string , the characters of string are
pushed onto the stack one by one as the string
is read from left to right.
• Once all the characters
of string are pushed onto stack, they are
popped one by one. Since the character last
pushed in comes out first, subsequent pop
operation results in the reversal of the string.
For example:
To reverse the string ‘REVERSE’ the string is
read from left to right and its characters are
pushed . LIKE:
onto a stack.
II. Checking the validity of an expression
containing nested parenthesis:

• Stacks are also used to check whether a given


arithmetic expressions containing nested
parenthesis is properly parenthesized.
• The program for checking the validity of an
expression verifies that for each left parenthesis
braces or bracket ,there is a corresponding
closing symbol and symbols are appropriately
nested.
e.g. nested structure such as nested loop,
nested if, nested fuction
For example:
VALID INPUTS INVALID INPUTS
{} {(}
({[]}) ([(()])
{[]()} {}[])
[{({}[]( [{)}(]}]
{ })}]
III. Evaluating arithmetic expressions:

INFIX notation:
The general way of writing arithmetic expressions is known as infix notation.

e.g, (a+b)

PREFIX notation:
e.g, +AB

POSTFIX notation:
e.g: AB+

Order of Precedence of Operators


 Exponentiation
 Multiplication/Division
 Addition/Subtraction
Cont…
Conversion to Postfix Expression

( (AB+)*C-(DE-) ) $ (FG+)
( (AB+C*)-(DE-) ) $ (FG+)
(AB+C*DE--) $ (FG+)
AB+C*DE- -FG+$
Infix to Postfix
Example-1: A+B*C
( (A-(B+C) ) *D ) $ (E+F)
A trace of the algorithm that converts the infix expression
a - (b + c * d)/e to postfix form

You might also like