0% found this document useful (0 votes)
67 views18 pages

Stack Implementation & Expression Conversion

The document discusses stacks and their implementation using arrays. Stacks follow the LIFO principle and can perform push, pop, overflow and underflow operations. The key steps to implement a stack using an array include initializing the array, checking for empty and full conditions, and algorithms for push, pop and display functions. Dynamic allocation of arrays during runtime is also described. Expressions are defined and the types like infix, postfix and prefix are explained along with an algorithm to convert infix to postfix expressions.

Uploaded by

Aim Head
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)
67 views18 pages

Stack Implementation & Expression Conversion

The document discusses stacks and their implementation using arrays. Stacks follow the LIFO principle and can perform push, pop, overflow and underflow operations. The key steps to implement a stack using an array include initializing the array, checking for empty and full conditions, and algorithms for push, pop and display functions. Dynamic allocation of arrays during runtime is also described. Expressions are defined and the types like infix, postfix and prefix are explained along with an algorithm to convert infix to postfix expressions.

Uploaded by

Aim Head
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/ 18

STACKS USING DYNAMI

ARRAYS. DIFFERENT
BY:
REPRESENTATION OF
NIDHI PATKAR
NIDHI PRABHU
EXPRESSION
NISARGA
PRADHYUMNA
PRADEEP
OMKAR
STACKS:

A stack is a linear list of elements in


which an element may be inserted or
deleted at only one end I.e called at the
top of the shelf.
• Stack is a data structure which works
on the principle Last In First Out
(LIFO).
• Which means the last element inserted
will be the first element to be deleted.
OPERATIONS IN
STACK:

• PUSH – inserting an element into the stack.


• POP – removing/deleting an element from the stack.
• OVERFLOW – check whether stack is full or not.
• UNDERFLOW – check whether stack is empty or not.

*Representation of LIFO principle using push & pop.


IMPLEMENTATION OF
STACKS(USING
ARRAYS):
1. Create (): The create stack function can
implement as a 1D array i.e;
Syntax:

#define SIZE 5
Int S[size];
Int top=-1;
Here S[size] is an array, to hold the elements of the
stack.
2. Is Full () {check for overflow}: in order to perform a push operation we need to check whether the
stack is completely filled with elements or not.
i.e;
if(top==size -1)
{
printf(“stack FULL- Overflow”);
return;
}

*PUSH OPERATION IN STACK


3. Push():
• Inserts an element onto the stack.
• First checks whether sufficient space is available in the stack.
• If available then increments the value of top by 1 and the inserts
the item into stack i.e;
• Void push()
{
int item;
if (top== size -1)
{
printf(“stack overflow\n”);
return;
}
printf(“ Enter an item \n”);
scanf(“%d”, &item);
s[++top] =item;
}
4. Is Empty():

• Before we delete an item we must first check whether there are any element
in the stack i.e;
• If (top == -1)

{
printf(“stack is empty – underflow\n”);
return;
}
• top=-1 indicates that the stack is empty.
5. Pop():

• To delete an item from the stack.


void pop()
{
if(top == -1)
{
printf(“Stack underflow\n”);
return;
}
printf(“ deleted item = %d”, s[top--]);
}
6. Display ():
• It prints the content of the stack.
• void display()
{
int I;
if (top == -1)
{
printf(“stack is empty – underflow”);
return;
}
printf(“Contents of the stack are:\n”);
for(i=top; i>=0;i--)
printf(“%d\n”, s[i]);
}
ARRAYS:
• We can allocate or create a stack dynamically during run time when we are not sure about the size of the stack, as follows:
• Assume that the stack size is 1 i.e;
int stackSize=1;
int *S;
S=int * malloc (stackSize*sizeof(int));
void push(){
if(top==size-1){
printf(“stack is full: Increase by 1”);
stackSize++;
S=(int*) realloc(S,stackSize*sizeof(int));
}
S[++top]=item;
}
void pop(){
if(top==-1){
printf(“Stack underflow”);
return;
}
printf(“item deleted =%d\n, S[top--]);
printf(“ Stacks size decreased by 1\n”);
stackSize--;
S=(int*)relloc(S,stackSize*sizeof(int));
}
main(){ …
}
EXPRESSIONS:
What is an Expression?
In any programming language, if we want to perform
any calculation or to frame a condition etc., we use a
set of symbols to perform the task. These set of
symbols makes an expression.
It can be defined as: An expression is a collection of
operators that represents a specific value.
In the above definition, OPERATOR is a symbol
which performs a particular task like arithmetic
operation etc.,
OPERANDS are the values on which the operator can
perform the task. Here operand can be direct value or
variable or address of memory location.
EXPRESSION
TYPES:
Based on the operator position,
expressions are divided into 3 types.
They are:
1. Infix Expression
2. Postfix expression
3. Prefix expression
1. INFIX EXPRESSION
In infix expression, operator is used in
between the operands.
The general structure of an infix expression
is as follows.
Operand1 Operator Operand2
2. POSTFIX
EXPRESSION
• In postfix expression, operator is used after
operands. We can say that “Operator follows
the Operands”
• The general structure of postfix expression is
as follows..
Operand1 Operand2 Operator
3. PREFIX
EXPRESSION
• Here operator is used before operands. We
can say that “ Operands follows the
Operators”
• The general structure of prefix expression is
as follows..
Operator Operand1 Operand2
CONVERSION/
Algorithm :- TRANSFORM INFIX EXPRESSION TO POSTFIX EXPRESSION:

Step 1: push # onto the stack


Step 2: scan the expression from left to write and repeat steps 3 to 6 for each element
Step 3: if a ‘(‘ parenthesis is encountered, push it onto the stack
Step 4: if an operand is encountered, add it to postfix P
Step 5: if an operator is encountered, then:
a) Check whether the precedence of the current operator on top of the stack is greater than or equal to the
precedence of the scanned operator, then go on popping all the operators from the stack and place them in the
postfix P
b) Otherwise(else) push the scanned operator onto the stack
Step 6: if an ‘)’ parenthesis is encountered, pop all the items from the stack and place them in the postfix expression
until we get the matching left ‘(’ parenthesis.

Note: do not push the ‘(‘ parenthesis onto the postfix P

Step 7: Check whether any operator or symbol is present in the stack. If so, then pop them and place them onto the
postfix P.
EXAMPLE: Convert the following expressions into postfix expressions using the above algorithm.
1. (a*b)+c (initial: top=-1 and pos =0
STEP SYMBOL POSTFIX STACK VARIABLE
(optional)
Initial - # Top=0, post=0
-
i=0 ( - #,( Top=1, post=0

1 a a #,( Top=1, post=1

2 * a #,(,* Top=2, post=1

3 b ab #,(,* Top=2, post=2

4 ) ab* #, Top=0, post=3

5 + ab* #,+ Top=1, post=3

6 c ab*c #,+ Top=1, post4

The postfix expression we get is ab*c+


THANK
YOU

You might also like