0% found this document useful (0 votes)
6 views33 pages

Module 2 (Array Polynomial, Stack)

Uploaded by

u2209008
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)
6 views33 pages

Module 2 (Array Polynomial, Stack)

Uploaded by

u2209008
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/ 33

Data Structures & Algorithms

101009/IT200C (Module 2)
S2 CU
Mr. Tinku Soman Jacob
Asst. Prof IT
RSET

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 1


Index
• Arrays
• Polynomial representation using Arrays
• Stack using array
• Infix to Postfix conversion
• Postfix expression Evaluation

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 2


Array
• An array is a collection of similar data items which may be int type, char
type or any user defined type such as a structure or class.
• Array elements are stored in consecutive memory locations. ie, linear data
structure.
• An array is a finite, ordered and collection of homogeneous data elements.
• It’s a collection of similar items which are referred by a common name, ie,
array name.
• 2 types of array
• One – dimensional array
• Multi- dimensional array

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 3


Structure
• Structure is a collection of similar or different data items
• Syntax
struct structure_name
{
data_type field 1;
data_type field 2;
……..
};
struct structure_name variable_name;
• Each members of a structure is accessed by using dot operator /
member selection operator
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 4
Polynomial
• In mathematics, a polynomial is an expression consisting of variables
and coefficients, that involves only the operations of addition,
subtraction, multiplication, and non-negative integer exponentiation of
variables.
• Eg: A(x) = 3x20 + 2x5+4
• The largest or leading exponent of a polynomial is called its degree.
• Coefficients that are zero are not displayed.
• Term with exponent equal to zero does not show the variable since x
raised to a power of zero is 1.
• Eg: A(x) =Σaixi & B(x) =Σbixi
• A(x) + B(x) = Σ (ai + bi ) xi
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 5
Polynomial Representation
• Eg: 10x5+7x4+11x3+9x2+5x+25
Coeff 10 7 11 9 5 25
expo 5 4 3 2 1 0

• Array of structures
struct poly
{
int coeff;
int exp;
}term[max_term];

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 6


Polynomial Addition
a 2x1000+5x3+1 +
b x4+10x3+3x2+1
2x1000+ x4+15x3+3x2+2

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 7


Polynomial Addition Pseudo-code
case ‘ < ‘ //term[p].exp < term[q].exp
Read two polynomials into an array
newterm(term[q].coeff,term[q].exp);
Polyadd(int af, int al, int bf, int bl)
q++;
{
break;
int p,q,c;
case ‘ > ‘ // //term[p].exp > term[q].exp
p = af; q = bf;
newterm(term[p].coeff,term[p].exp);
while ((p<=al) && (q<=bl)) p++;
{ break;
switch (compare (term[p].exp, term[q].exp)) }//end of switch } //end of while
{ for( p<=al )
case ‘=‘ {
c = term[p].coeff + term[q].coef; newterm(term[p].coeff,term[p].exp); p++;
if (c!=0) }
newterm(c,term[p].exp); for( q<=bl )

p++; {

q++; newterm(term[q].coeff,term[q].exp); q++;


}
break;
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 8
}//end of function
Polynomial Addition Pseudo-code
newterm( int a, int b)
{
if( free > = maxterm)
print “No enough space”
otherwise
term[free].coeff = a;
term[free].exp = b;
free++;
}
p q
af al bf bl free

2 5 1 1 10 3 1 2 1 15 3 2
1000 3 0 4 3 2 0 1000 4 3 2 0

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 9


Polynomial Addition Analysis
• Let m and n be the number of non-zero terms in A & B respectively.

• If m>0 and n>0, while loop is entered.

• Each iteration of the loop requires O(1) time.

• At each iteration, we increment the values of p or q or both.

• Since iteration terminates when either af or bf exceeds al or bl,


respectively, the number of iterations is bounded by m+n-1.

• Asymptotic computing time of this algorithm is O(n+m).


101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 10
Stack
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages.
• It is named stack as it behaves like a real-world stack.
• Example – a deck of cards or a pile of plates.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 11


Stack
• A stack is an ordered list in which insertion (pushes or adds) and
deletion (pops or removes) are made at one end called the top.
• It’s a linear data structure.
• Since the last element inserted into a stack is the first element
removed; a stack is also known as last-in-first-out (LIFO) list.
• Its also called FILO list.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 12


Stack
• Push

• Pop

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 13


Stack
• Implementation of stack using 1-D array; stack[size]
• First or bottom element of stack is stored stack[0]
• Second element in stack[1]
• i-th element in stack[i-1]
• Initially top = -1 index, means stack is empty
• If top reaches its maximum index size, then stack is full
• Pop operation on an empty stack is an error called underflow error
• Push operation to a full stack is an error called overflow error

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 14


Stack – Pseudocode
Create an empty stack with maximum size ‘n’
top =-1
push(int item)
{
if top = n-1
print “Overflow error”
otherwise
top ++
stack[top]=item
} 101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 15
Stack – Pseudocode
pop()
{
if top = -1
print “Underflow error”
otherwise
item = stack[top]
top --
print “Item deleted is xxx”
}
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 16
Stack – Pseudocode
display()
{
if top = -1
print “Stack empty”
otherwise
for i=top to 0
print stack[i]
}

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 17


Stack – Applications
1. Expression conversion, evaluation (infix, postfix, prefix)
2. Validity of expression or balancing parenthesis
3. Reverse a string
4. Stack frame or activation record
5. Recursion or function call (uses system stack)

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 18


Stack – Applications(System Stack)
• A special stack called system stack, used by a program to run-time to
process function calls.
• Whenever a function is invoked, the program creates a structure,
referred as activation record or stack frame, and places it on top of the
system stack.
• Initially, the activation record for the invoked function contains only a
pointer to the previous stack frame and a return address.
• Previous stack frame pointer points to the stack frame of the invoking
function, while return address contains the location of the statement to
be executed after the function terminates.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 19


Stack – Applications(System Stack)

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 20


Multiple Stack
• This is multiple stack in a single array.

• When we need m stacks, we can divide the available memory into m


segments.
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 21
Evaluation of Expressions(Appln. of Stack)

• Evaluation of arithmetic Expressions.


• Arithmetic expressions consists of operands and operators.
• Operands are variables or constants.
• Operators are various arithmetic unary or binary operators.
• The problem with the evaluation of expressions is the order of
evaluation. Its fixed by precedence and associativity.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 22


Evaluation of Expressions

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 23


Notations of Expressions
• 3 notations to represent arithmetic expressions
1. Infix – “a + b”
2. Prefix – “+ a b” (Polish Notation)
3. Postfix(Suffix) – “a b +” (Reverse polish notation)
• Polish notation introduced by Polish mathematician Jan Luksiewiez
• The fundamental property of polish notation is that the order in which the
operations are performed is determined by the position of the operators and
operands in the expression.
• Parenthesis is not required in polish & reverse polish notation.
• Any infix expression can convert to polish & reverse polish notation
• To determine the final value of the expression, the precedence of operators
are determined by the BODMAS rule.
101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 24
Infix to Postfix Expressions conversion
• Rule – Operators are taken out of the stack as long as their instack
priority is greater than or equal to the incoming priority of the new
operator.
• Eg: A+B*C

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 25


Infix to Postfix Expressions conversion
• Eg: A-B+C*D/E

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 26


Infix to Postfix Expressions conversion
• Eg: A-(B+C)*D/E =

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 27


Infix to Postfix Expressions conversion
• Eg: ((A+(B+C))/D) =

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 28


Infix to Postfix Expressions conversion
1. Scan the input string from left to right.
2. The operands are copied to output.
3. Left parenthesis are always pushed into the stack.
4. When a right parenthesis is encountered the symbol at the top of the stack is
popped of the stack and copied to the output until the symbol at the top of the
stack is a left parenthesis. When that occurs both parenthesis are discarded.
5. If the symbol being scanned has a higher precedence than the symbol at the
top of the stack, the symbol being scanned is pushed onto the stack
6. If the precedence of the symbol being scanned is lower than or equal to the
precedence of the symbol at the top of the stack, the stack is popped to the
output.
7. When the end of the expression is reached , all the elements in the stack is
popped to the output string. Mr. Tinku Soman Jacob, DIT, RSET(Autonomous)
101009/IT200C _S2 CU_Module-2 29
Conversion of infix expression to postfix
• Start
• Read expression to exp.
• Repeat following steps until exp[i]==‘\0’
• If exp[i]==operand, print exp[i].
• Else
• If stack is empty or contains a left parenthesis’(‘, push the incoming operator onto stack
• If exp[i]== ‘(‘, push to stack.
• If exp[i]== ‘)’ pop from stack till ‘(‘ and print operators.
• If prec(exp[i]) > prec(stack[top]), push onto stack.
• If prec(exp[i]) < prec(stack[top]), pop and print stack[top] until condition false, then push(exp[i])
• If equal precedence, use associativity rules
• L to R pop and print stack[top] until condition false, then push(exp[i])
• R to L then push incoming operator.
• If exp[i]==‘\0’, pop and print all operators in the stack.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 30


Evaluation of Expression
• Eg: 5 6 2 + * 12 4 / -
Step Input Operation Stack O/P

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 31


Evaluation of Expression

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 32


Evaluation of Expression(Algorithm)

1. Create a stack to store operands.


2. Scan the given expression from left to right.
3. a) If the scanned character is an operand, push it into the stack.
b) If the scanned character is an operator, POP 2 operands from stack and perform
operation and PUSH the result back to the stack.
4. Repeat step 3 till all the characters are scanned.
5. When the expression is ended, the number in the stack is the final result.

101009/IT200C _S2 CU_Module-2 Mr. Tinku Soman Jacob, DIT, RSET(Autonomous) 33

You might also like