0% found this document useful (0 votes)
2 views20 pages

Stacks

The document explains the concept of stacks, which are data structures that operate on a Last In, First Out (LIFO) principle. It details operations such as PUSH and POP, and discusses static and dynamic implementations of stacks, including their advantages and disadvantages. Additionally, it covers applications of stacks, conversion between infix, postfix, and prefix notations, and the evaluation of postfix expressions.

Uploaded by

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

Stacks

The document explains the concept of stacks, which are data structures that operate on a Last In, First Out (LIFO) principle. It details operations such as PUSH and POP, and discusses static and dynamic implementations of stacks, including their advantages and disadvantages. Additionally, it covers applications of stacks, conversion between infix, postfix, and prefix notations, and the evaluation of postfix expressions.

Uploaded by

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

Prepared by: Ms.

Martinez
Stacks
y A stack is a list in which insertion and deletion take place at
the same end
y This end is called top
y The other end is called bottom

y Stacks are known as LIFO (Last In, First Out) lists.


y The last element inserted will be the first to be retrieved

y E.g. a stack of Plates, books, boxes etc.


Insertion and deletion on stack
Operation On Stack
y Creating a stack
y Checking stack‐‐‐‐ either empty or full
y Insert (PUSH) an element in the stack
y Delete (POP) an element from the stack
y Access the top element
y Display the elements of stack
Push and Pop
y Primary operations: Push and Pop
y Push
y Add an element to the top of the stack.
y Pop
y Remove the element at the top of the stack.
Stack‐Related Terms
y Top
y A pointer that points the top element in the stack.
y Stack Underflow
y When there is no element in the stack, the status of stack is
known as stack underflow.
y Stack Overflow
y When the stack contains equal number of elements as per its
capacity and no more elements can be added, the status of
stack is known as stack overflow
Stack Implementation
y Implementation can be done in two ways
y Static implementation
y Dynamic Implementation
y Static Implementation
y Stacks have fixed size, and are implemented as arrays
y It is also inefficient for utilization of memory
y Dynamic Implementation
y Stack grow in size as needed, and implemented as linked lists
y Dynamic Implementation is done through pointers
y The memory is efficiently utilize with Dynamic Implementations
Static Implementation
ƒ Elements are stored in contiguous cells of an array.
ƒ New elements can be inserted to the top of the list.

top First Element


Second Element
List
Last Element

Empty
maxlength
Static Implementation
1
3
2

2
1

Problem with this implementation


ƒ Every PUSH and POP requires moving the entire
array up and down.
9
Static Implementation
Since, in a stack the insertion and deletion take place
only at the top, so…

A better Implementation:
ƒ Anchor the bottom of the stack at the
bottom of the array
ƒ Let the stack grow towards the top of the
array
ƒ Top indicates the current position of the
first stack element.
10
Static Implementation
A better Implementation:

top
1 First Element
Second Element
2 .
.
maxlength Last Element

11
Dynamic Implementation of Stacks
y As we know that dynamic stack is implemented using
linked‐list.
y In dynamic implementation stack can expand or shrink
with each PUSH or POP operation.
y PUSH and POP operate only on the first/top cell on the list.

Top
x y z NULL
Stack applications
y “Back” button of Web Browser
y History of visited web pages is pushed onto the stack
and popped when “back” button is clicked

y “Undo” functionality of a text editor

y Reversing the order of elements in an array

y Saving local variables when one function calls another, and


this one calls another, and so on.
Conversion of Decimal to Binary
#include<iostream.h> void push(int value)
#include<conio.h> {
void push(int value); top++;
int pop(void); A[top]= value;
int top=‐1, A[100]; return;
int main ()
}
{
int no, r, x;
cout<<"Enter a decimal number: "; int pop(void)
cin>>no; {
do { int b;
r = no%2; b = A[top];
push(r); top‐‐;
no = no/2; return b;
}while(no!=0);
}
cout<<"the binary value is : ";
for(x=top;x>=0;x‐‐) {
cout<<pop(); }
getch();
return 0; }
Infix, Postfix, Prefix Form
Infix – the operator is between the two operands.
‐ also known as in –order

Prefix ‐ the operator precedes the two operands.


‐ Also known as pre‐order.

Postfix ‐ the operand precedes the operator.


Infix, Postfix, Prefix Form
Infix Notation ‐ ordinary notation for writing expressions
where the operator separate arguments.

Polish Notation ‐ also known as prefix notation where the


operator comes after arguments.

Reversal Polish Notation – called postfix notation where the


operator comes before the arguments.

Operator refers to mathematical or logical operation.


Operand refers to the variables & numbers.
Infix, Postfix, Prefix Form
Steps in Conversion
1. Convert the given using mathematical or logical operation
thus evaluation determines the infix form.
2. Create a table to separate the operands (oprd) from the
operator (optr).
3. To determine the prefix from the right of the infix write the
corresponding symbol to their respective column. Rewrite
the last operation to the operand column for every open
parenthesis[(] encountered in the operation column. Read
from top to bottom to get the prefix form.
Infix, Postfix, Prefix Form
4. To determine the postfix form, start to the left of
the infix. Write the corresponding symbol to their
respective column. Rewrite the last operation to
the operand column for every close parenthesis [)]
encountered in the optr column. Read from the
bottom to get the postfix form.
Infix, Postfix, Prefix Form
Example:
1. (a+b)1/2
2. x2 + b2
3. 3√a‐b + (4x/y2)
4. (a/b3)2 / xy4
Infix, Postfix, Prefix Form
Evaluation of Postfix:
1. Each time we read an operand we push it into a stack.
2. When we reach an operator its operand will be the top
two elements, perform the indicated operation on
them and push the result on the stack.
Ex. 623+‐382/+*2^3+

You might also like