0% found this document useful (0 votes)
50 views30 pages

DS - Module-1-I

Uploaded by

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

DS - Module-1-I

Uploaded by

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

Data Structures with 20MCA

Algorithms

Module-I: Classification ofData Structures

• Classification of Data Structures:

Primitive and Non- Primitive

Linear and Nonlinear

Data structure Operations

• Stack: Definition

Representation

- Stack as ADT

- Operations and Applications:

o Polish and reverse polish expressions

o Infix to postfix conversion

o evaluation of postfix expression

o infix to prefix conversion

o postfix to infix conversion

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

1. Classification of Data Structures

• Data-type Definition:

<Data type>= Permitted Collection of data+ Allowed operations on this data.

A data type is a collection of data together with a set of operations for creating
and manipulating them.

• Data Structure Definition:

<Data type>= organized data+ Allowed operations on this data.

Data Structures in C are used to store data in an organised and efficient manner. The
C Programming language has many data structures like an array, stack, queue, linked list,
tree, etc. A programmer selects an appropriate data structure and uses it according to their
convenience.

Data structure

Primitive data-structure Non-Primitive data-structure

int
Linear data-structure Non-Linear data-structure
char
Array Tree
float
structure Graph

Pointer
union Sets

stack Tables

Queue

Linked List

1. Primitive Data Structures


The primitive data types are the basic data types that are available in the
system. Example: int, float, double, char

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

2. Non- Primitive Data Structures: classified as Linear and Nonlinear.

Linear Data structure: A Linear data structure have data elements arranged in sequential
manner and each member element is connected to its previous and next element. Such
data structures are easy to implement as computer memory is also sequential.
Example: Array, Stack, Queue, Linked List etc.

Non-Linear Data structure: Data structures where data elements are not arranged
sequentially or linearly are called non-linear data structures.
Example: Tree, Graph

Following are the important differences between Linear Data Structures and Non-linear Data
Structures.

Sr. No. Key Linear Data Structures Non-linear Data


Structures
Data elements are
Data elements are sequentially
Data Element hierarchically connected
1 connected, and each element is
Arrangement and are present at various
traversable through a single run.
levels.

All data elements are present at a Data elements are present


2 Levels
single level. at multiple levels.

Non-linear data structures


are difficult to understand
Implementation Linear data structures are easier to
3 and implement as
complexity implement.
compared to linear data
structures.

Non-linear data
Linear data structures can be traversed structures are not easy to
4 Traversal
completely in a single run. traverse
and needs multiple runs
to
be traversed completely.

Linear data structures are not very Non-linear data


Memory
5 utilization memory friendly and are not structures use memory
utilizing efficiently.
memory very
efficiently.

6 Examples Array, List, Queue, Stack. Graph, Map, Tree.

Prof NANDAN GP, MCA, 3


Data Structures with 20MCA
Algorithms

• Data structure Operations


Basic operations can be performed on the data structures:
- Traversing-
It is used to access each data item exactly once so that it can be processed.
- Searching-
It is used to find out the location of the data item if it exists in the given collection of
data items.
- Inserting-
It is used to add a new data item in the given collection of data items.
- Deleting-
It is used to delete an existing data item from the given collection of data items.
- Sorting-
It is used to arrange the data items in some order i.e., in ascending or descending order in
case of numerical data and in dictionary order in case of alphanumeric data.
- Merging-
It is used to combine the data items of two sorted files into single file in the sorted form.

Prof NANDAN GP, MCA, 4


Data Structures with 20MCA
Algorithms

2. Stack

• Definition:

"A stack is a Linear collection of items in which all additions and deletions are restricted to
one end, called the top".

• Stack Representation and Operations on Stack

push{)
�-�-

-
I
- I
z-f-top

pop () I I
I I
I I
I I
I I
I I
I I
L J

Figure 2.1: Operations on stack

• Properties of stack:

- Stack is an abstract data type with a predefined capacity.


- Stack uses LIFO structure (Last in- First out)
- Stack is an ordered list of similar data type.

• Basic stack operations

o Push
- Insert new elements onto the top of Stack.
o Pop
- Remove an element from the top of stack.
o isEmpty
- True if stack is empty.
o Display
- Display the contents of the stack.

Prof NANDAN GP, MCA, 5


Data Structures with 20MCA
Algorithms

Ql. What is stack? What are the basic operations on stack?

Q2. Write a program to implement stack operation using array as a data structure
(without using Structure).
Implement following conditions on stack.
1. Stack overflow
2. Stack underflow
3. Stack empty
4. Stack full
/* Stack implementation */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/* Program 1: Stack of tntegers to perform the fottowing */

void push(int *, int);


int pop(int *);
void display(int *);

#define MAX 3
int top = -1;

void main()
{
int stack[MAX];
int choice, item;

while(l)
{
printf("\n\n Stack Operations : \n");
printf("\n 1. Push.");
printf("\n 2. Pop.");
printf("\n 3. Display.");
printf("\n 4. ( or any other) Exit ... ");

printf("\n\n Enter your choice: ");


scanf("%d", &choice);

Prof NANDAN GP, MCA, 6


Data Structures with 20MCA
Algorithms

switch(choice)
{
case 1:
printf("\n Enter the element to be pushed: ");
scanf( "%d", &item);
push(stack, item);
break;

case 2:
item= pop(stack);
if( item)
printf("\n Popped item is: %d .",item);
break;

case 3:
display( stack);
break;

default: exit(0);
}
}
}
II end of main function

I* function definition section *I


void push(int *stack, int item)
{
if(top == MAX-1)
{
printf("\n .. STACK OVERFLOW .. ");
return;
}
stack[++top] = item;
printf( "\n .. Element pushed Successfully .. ");

if(top == MAX-1)
printf("\n .. Now Stack Is Full .. ");
}

Prof NANDAN GP, MCA, 7


Data Structures with 20MCA
Algorithms

int pop(int *stack)


{
int item;
if(top == -1)
{
printf("\n .. STACK UNDERFLOW .. ");
return 0;
}
item= stack[top--];
if(top == -1)
printf("\n .. After pop Stack Is Empty .. \n");
return i tern;
}
void display(int *stack)
{
int temp;
if(top == -1)
{
printf("\n .. Stack Is Empty .. ");
return;
}
printf("\n The contents of the stack are:\n");
for(temp = top; temp >=0; temp--)
printf(" %d", stack [temp]);
return;
}

Q3. Write a program to implement stack operation using a structure. Implement following
conditions on stack. Push(), Pop(), Display(), Overflow() & Underflow().

Note: Implement the above question number Q3.

Prof NANDAN GP, MCA, 8


Data Structures with 20MCA
Algorithms

12.1. Applications of Stack

1. Infix to postfix conversion


2. Evaluation of postfix expression
3. Infix to prefix conversion
4. Postfix to infix conversion
5. Recursion
6. Reversing data
7. Subroutine call
8. Parsing data
9. Backtracking steps
10. Eight Queens problem.

2.2. Polish and reverse polish expressions

• Arithmetic Expression:
- An expression is defined as a number of operands combined using several operators.

• Notations (Types/Forms) of Arithmetic Expression:

1. Infix Notation

2. Polish Notation (Prefix expression)

3. Reverse Polish Notation (Postfix expression)

1. Infix Notation
o Form: "operator is placed in-between the two operands"
o Example: A+ B, 5 - 6

2. Polish Notation (Prefix expression)


o Prefix notation was introduced by the "Polish logician Lukasiewicz", and is

sometimes called "Polish notation".

o Form: "operator is placed in beginning of the two operands"

o Example: +AB, - 5 6

Prof NANDAN GP, MCA, 9


Data Structures with 20MCA
Algorithms

3. Reverse Polish Notation or Suffix (Postfix expression)


o Postfix notation is also called "Reverse Polish Notation -

RPN". o Form: "operator is placed in end of the two

operands"

o Example: AB+, 56-

2.3. Why we need prefix or postfix expression?

• Infix Notation:

Infix notation needs extra information to evaluate the expression as

o Operator precedence

o Associativity.

Brackets () allow users to override these rules.

- For example: ( expression: a+ b + c * d)

o Multiplication and division are done before addition and

subtraction. o Associativity for arithmetic operator is from left to right.

• Prefix and Postfix Notation:


In prefix/postfix notations does not require extra information for Operator

precedence and Associativity, as this information is included in expression itself.

No Brackets () are required to override these rules.

• Priority table from highest to lowest

No Operator Meaning

1 $ or' Exponentiation

2 -, I Multiplication and division

3 +, - Addition and subtraction

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

• Conversion between expression notations

Steps to Convert from Infix to prefix and postfix:


1. Step by step Parenthesize the expression using the precedence and Associativity rules.

2. At each step convert the parenthesized infix expression to prefix or postfix as needed.

3. Repeat step 1 and 2 till all the operators are transformed.

Convert the following expressions from Infix to prefix and postfix:

Infix Prefix Postfix


A + B * C + D 7 A+ ( B * C ) + D 7 A+ (B * C) + D

7 ( A + (*BC) ) + D 7 ( (A+ (BC*)) + D

7 ( (+A*BC) + D ) 7 ( (ABC*+) + D )

7 + + A * B c D 7 A B C * + D +
A*B+C*D 7 7

A+B/C$D-E*(F+ G) 7 7

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

2.4. Application of Stack: 1. Infix-to-Postfix Conversion

• Algorithm
1. Set operator stack to empty.
2. Symbol= ( Read the infix expression from left to right one character at a time).

a) If ( Symbol = operand )
Add symbol to postfix

b) If (Symbol= open brace)


Push symbol to stack

c) If ( Symbol = closing brace)


while ( StackTop != open brace)
Pop stack and add to postfix
Remove open brace from stack and discard

d) Otherwise If ( Symbol = operator)


while (precedence(Symbol) <= precedence(StackTop))
Pop top operator and add it to postfix string

Push symbol to stack. // as precedence of Symbol is High

4. Repeat step-2 till no more symbols in string. ( till end of string)

5. Pop all elements from stack till stack becomes empty and add to the Postfix Expression.

Priority Table:
case '#':
return 0· case 1 ( 1 :

'
return 1 · case '+':
'
case • - 1 :
return 2.
* '
case 1 1 0

case , I,: return 3.


'
case '$':
return 4•
I\
case
1 1 0

'

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

Evaluate the foll°',ing Infix Expression to Postfix Expression using Stack.

Convert the Infix expression to Postfix Expression:


(A + B) • c - (D - E)

Steps Symbol Action Postfix string Operator stack


#
I ( Push #, (
2 A Add to postfix A #, (
3 + Push A #, (, +
4 B Add to postfix AB #, (, +
5 ) Pop AB+ #, (
6 ) Pop AB+ #
7 • Push AB+ #, •
8 c Add to postfix AB+C #, •
9 - Pop AB+C* ' k ,, #
-
10 - Push AB+C* ' #, -
11 ( Push AB+C* #, -, (
12 D Add to postfix AB+C*D #, -, (
13 - Push AB+C*D #, -, (, -
14 E Add to postfix AB+C * DE #, -, (, -
15 ) Pop AB+C * DE- #, -, (
16 ) Pop AB+C * DE- #, -
17 Null Pop AB+ C * DE- - # -Empty

The Postfix Expression is: AB+ C *DE - -

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

Q4. Write a function that convert the infix expression to postfix expression that also includes
braces (Parenthesis)

r: infixToPostfixO f1111ctio11 */
void infixToPostfix(char *infix, char* postfix)
{
char symbol, brace;
inti= 0, k = 0;

push('#');

while ((symbol= infix[i++]) != '\0')


{
if (isalnum(symbol)) /* Buttt-tnfunctton i11 ctype.h */
postfix[k++] = symbol;
else if (symbol -- '(')
push(symbol);

else if (symbol -- ')')


{
while (stack[top] != '(')
postfix[k++] = pop();
brace = pop (); /* Remove open brace'(' */
}
else
{ /* Operator */
while (priority(symbol) <= priority(stack[top]))
postfix[k++] = pop();
push(symbol);
}
}
while (stack[top] != '#') /" Pop from stack till empty */
postfix[k++] = pop();

postfix[k] = '\0';
}

Output:

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

Rest of C program to convert infix expression to postfix using function


#define SIZE 10 /* Size of Stack*/
#include <ctype.h>

char stack[SIZE]; /* Global declarations */


int top = -1;

void infixToPostfix(char * char*);


'

void push(char symbol)


{
stack[++top] = symbol;
}

char pop()
{
return (stack[top--]);
}
int priority(char op)
{ /* F1111ctio11 for precedence */
switch (op)
{
case '#': return 0;
case 1 ( 1 : return 1;
case '+':
case • - 1 : return 2;
case 1 * 1 0

case , I,: return 3;


}
}
void main()
{
char infix[50], postfix[50];
printf("\n Read the Infix Expression ? ");
scanf( "xs", infix);
infixToPostfix(infix, postfix);
printf( "\n Postfix Expression %s", postfix);
}

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

2.5. Application of Stack: 2. Evaluation of Postfix Expression

• Algorithm

1. Set Operand Stack to empty

2. Symbol= (Read the postfix input from left to right one character at a
time). a. If (symbol = operand}

- Push symbol to

stack. b. If (symbol= operator}


- Pop first operand2 and then operandl

- Find result by Applying the operator on operandl operator operand2.

- Push the result back to stack.

3. Repeat step-2 till end of input string.

4. Pop the final result and Return

Evaluate the foll°',ing Postfix Expression using Stack.

Evaluate the Postfix Expression: 6 2 3 * + 5 -


Steps Symbol Action Opl Op2 result Operand stack
1 6 Push 6
2 2 Push 6.2
3 3 Push 6,2,3
4 * Pop op2 2 3 6 6,6
pop opl
5 + Pop op2 6 6 12 12
pop opl
6 5 Push 12,5
7 - Pop op2 12 5 7 7
pop opl
8 Null Retum 7

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

Steps
Evaluate Postfix Expression: 5 4 * 6 5
Symbol Action Opl Op2 result
. -
Operand stack
1 5 Push 5
2 4 Push 5,4
3 * Pop op2 5 4 20 20
pop opl
4 6 Push 20,6
5 5 Push 20,6,5
6 * Pop op2 6 5 30 20,30
pop opl
7 - Pop op2 20 30 -10 -10
pop opl
8 Null Retum -10

QS. Write a C program to evaluate postfix expression.

/* evaluation of postfix expression */

#include <stdio.h>
#include <conio.h>
#include <ctype.h>

#define MAX 20

float stack[MAX];
int top;

void push(float item)


{
stack[++top] = item;
}
float pop()
{
return(stack[top--]);
}

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

float getResult(float opl, float op2, char symbol)


{
float result;
switch(symbol) /* perform operation */
{
case '+' result = opl + op2;
break;
case result = opl op2;
break;
case ''' result = opl ' op2;
break;
case '/' if(op2)
result= opl I op2;
else
{ printf ("Exception: Divide by =era");
exit(0);
}
break;
}
return (result);
}
float evalPost(char *postfix)
{
int i=0;
char symbol;
float opl, op2, result;
top=-1;

while ( (symbol= postfix[i++]) != '\0')


{
if(isdigit(symbol)) /* Buttt-tnfunctton i11 ctype.h */
{
result= symbol - '0';
push(result);
}
else
{
op2 = pop();
opl = pop();
result= getResult(opl, op2, symbol);
push(result);
}
}
return pop();
}

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

int main()
{
char postfix[MAX];
float result;
printf("\n Enter a valid postfix expression: ");
scanf( "%s", postfix);

result = evalPost(postfix);

printf("\n The result is: %f", result);


get ch();
}

Output:

Prof NANDAN GP, MCA, 1


Data Structures with 20MCA
Algorithms

2.6. Application of Stack: 3. Infix-to-Prefix Conversion

• Algorithm

1. Set operator Stack to'#' indicating empty.


2. Read infix expression.
3. Reverse infix expression
4. Symbol= (Read the reversed infix expression from left to right one character at a

time). a} If (symbol = open brace)


Pop each operator while closing brace is encountered and add to Output string.

Discard both the braces if popped symbol is closing

brace. b) If (symbol= closing brace)


Push to stack

c) if (Symbol= operand)
Add to Output string

d) if (Symbol= operator)
if precedence(symbol) > precedence(stackTop)
./ push symbol to stack

while precedence(symbol) <= precedence(stackTop)


./ pop and add to postfix

5. Repeat step-4 till no more symbols in string. ( till end of string)

6. Pop all elements from stack till stack becomes empty and add to the output string.

7. Reverse the Output string and return as prefix String.

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

Convert the Infix Expression to Prefix: A + B * C - ( D - E}


Reverse the Infix Expression: ) E - D ( - C * B + A
Steps Symbol Operand or Action Stack Output string
Input
Precedence
1 #

2 ) Push #' )
3 E Operand Add to output #, ) E
4 > Push #, ), - E

5 D Operand Add to prefix #, ), - E D

6 ( < Pop to prefix #, ) E D -

7 ( Closing Discard braces # E D -


brace
8 > Push #, - E D -

9 c Operand Add to output #,- ED-C


JO * > Push #,-,* ED-C
11 B Operand Add to output #,-,* ED-CB
12 + < Pop #,- ED-CB*
13 Push #, -, + E D - C B *
14 A Operand Add to output #, -, + E D + C B * A

15 Null Pop to prefix #, - ED-CB*A+


16 Null Pop to prefix # ED-CB*A+-

Prefix string = reverse output = - + A * B C - D E

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

2.7. Application of Stack: 4. Postfix to Infix Conversion

• Algorithm

1. Scan operand stack to empty

2. Symbol= (scan Postfix String from Leh to Right till null).

a. If (Symbol = Operand) then


i. Push it on to the Stack.

b. If (Symbol = Operator) then


i. Pop Operand 1 and Operand 2

ii. Concatenate them with operator using Infix notation.

iii. Use parentheses properly to ensure correct order of operators.

iv. Push the resultant expression on to the Stack.

3. Repeat the above steps till the Postfix string is not scanned completely.

4. Pop the stack and return as infix expression.

Convert Postfix expression to Infix: AB* C D * -

Steps Symbol Action Stack


1 A Push A

2 B Push A, B

3 • Pop opl, op2 and concatenate with* (A*B)


4 c Push (A*B), c
5 D Push (A*B), c, D

6 • Pop opl, op2 and concatenate with* (A*B), (C*D)


7 - Pop opl, op2 and concatenate with* ((A*B)-(C*D))
8 Null Return stackop ((A*B)-(C*D))

Infix string= ( ( A * B ) - ( C * D ) )

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

Additional topics
• Array as ADT

ADT Array
Abstract typedef<< eltype, ub >>
ARRTYPE Condition type(ub) == int;
Abstract eltype extract (a, ; ) /" reading elt = a[i] */
ARRTYPE a;
inti;
Precondition:
O <= i < ub;
Postcondition:
extract== a[i];
Abstract store ( a, ;, val) /" written a[i] = elt */
ARRTYPE a;
inti;
eltype val;
Precondition:
O <= i < ub;
Postcondition:
a[i] == val;
End Array

• Operations on Array :
- Create - A operation that creates an Array

Insert- A operation that inserts a given element into a given location in an array

Delete- A operation that removes an element from a given position in an array

- Search - A operation that returns the first occurrence of element in a array, else return -1.

- Sort -A operation that sorts a given array.

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

• Stack as ADT

ADT Stack
abstract typedef <<eltype>> STACK (eltype)
abstract empty(s)
STACK (eltype) s·'
Postcondition empty ( len(S) -- 0);
abstract eltype pop(S)
STACK (eltype)
s·'
Precondition :
empty(S) == FALSE;
Postcondition: pop -- first(S');
s sub(S' , 1, len(S') - 1);
abstract eltype push( S, elt )
STACK (eltype) S;
eltype elt;
Postcondition S -- < elt> + S';
End ADT

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

2.8. Application of Stack: 5. Parentheses Matching

The objective of this problem is to check the matching of parentheses in an


expression.

For example: ( (a+ b) * c) is a valid expression, because the left parentheses at position 1st
has a matching right parentheses at 9th position and similarly, left parentheses at 2nd position
has a matching right parentheses at position 6th.

The expression ( (a+ b) and (a+ b)) are invalid, since there is a ')' missing for a '(' at position
1 and '(' missing for a ')' at position 6 respectively.

So, to check whether an Infix expression has matching parentheses or not. Stack is
useful because, if we scan the input expression from left to right, each right parentheses is
matched to the most recently seen unmatched left parenthesis. We can simply save the left
parentheses in stack until we encounter a matching right parentheses. The following C code that
does this job

Q6. Write C program to check Parentheses matching or not in the Infix Expression.

Note: Implement the above question number Q6.

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

2.9. Application of Stack: 6. Reversing String

• Algorithm
1. Set operand Stack to empty.
2. Read next character from input String.
a. Add character to stack.
3. Repeat step 2 till end of string.
4. Pop character from stack
a. Add popped character to string rev.
5. Repeat step 4 till stack is empty.
6. Add null character to string rev.
7. Display rev.

Q7.Write C program to reverse a given string using stack

#include <stdio.h>
#define MAX 20
char stack[MAX];
int top;
void push(char item)
{
stack[++top]=item;
}
char pop()
{
return(stack[top--]);
}
int main()
{
char input[MAX], rev[MAX];
inti;
top= -1;

printf("\n Enter a valid postfix expression: ");


scanf("%s", input);

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

for(i = 0; i < input[i] != '\0' , i++)


push(input[i]);

for(i=0; top != -1 , i++)


rev[i] = pop();

rev[i] = '\0';

printf("\n The result is: %s", rev);

getch();
}

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

Module- 1: Question Bank Marks

1
Define Data structures. Explain different types of data structures. 8

2 What is a stack? List and Explain the basic operations on stack. 4

3. What is a stack? List and implement basic operations on stack using C. 8

4 What is a stack? Explain with diagram. Write C representation of stack. 6

5 Define stack with diagram? Write a C program to reverse a string using stack. 8

6 Write an algorithm to implement stack using array. 8

7 Transfer each of the following infix expression to postfix expression. 6


e) (A+B)'(C'( D-E)+F)-G

n (A+B)'(C-D)'E'F
g) A+( ( ( B-C)' ( D-E )+F )/G )+H

8 Convert any two of the following into its prefix and postfix form. 6
i) (A+B)'C-D$E'F
ii) A-B/C'D$E
iii) (A+B)$(c+D-E)' F

9 Show the detailed contents of stack for a expression: 6


"623 + - 382 / + * 2 - 3 +" and evaluate the expression.

10 Write an algorithm to evaluate a postfix expression. Trace the algorithm with 6+2
stack contents for the following expression.
a) ABC+*CBA-+* with A=l, 8=2, C=3.

11 Write an algorithm to convert from infix to reverse Polish notation. 8

12 Write a C program to check whether a string is palindrome or not using stack. 8

13 Explain with algorithm how stack is applied for evaluating a postfix arithmetic 8
expression.

14 Write an algorithm to evaluate postfix expression. 8

15 Write a short note on applications of stack. 5

16 Define Prefix and Postfix expressions. Write a program to convert infix to postfix
8
expressions.

17 Write an algorithm to convert an infix expression to postfix. Trace the algorithm 10


for following infix expression ( I A- I B +CI I ' DI$ IE+ FI

18 Write an algorithm to evaluate a Postfix expression. Trave the algorithm for 10


following postfix expression showing contents of stack: 6 2 3 + - 3 8 2 I+* 2 $ 3 +

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

Convert the following expressions from Infix to prefix and postfix:

Infix Prefix Postfix


A + B + A B A B +
A + B * c 7 A+ ( B * c ) 7 A+ ( B * c )
7 A+ ( * B c ) 7 A+ ( B c * )
7 + A * B c 7 A B c * +
( A+ B ) * c * + A B C A B + C *
A + B * c + D 7 A+ ( B * c ) + D 7 A + (B * C) + D

7 ( A + (*BC) ) + D 7 ( (A + (BC*)) + D
7 ( (+A*BC) + D ) 7 ( (ABC*+) + D )

7 + + A * B c D 7 A B C * + D +
A*B+C*D
A+B/C$D-E*(F+ G) • '
.-
t
,
(A+B)*(C-D)$E*F

A+((B$C)*D)-(E+F)/(G*H))+I

(A+B'C)/D+E

A*B/C+(B+C)*D

Prof NANDAN GP, MCA, 2


Data Structures with 20MCA
Algorithms

Some practice problems on Converting expressions.


Priority table from highest to lowest

No Operator Meaning

1 $ or' Exponentiation

2 ',/ Multiplication and division

3 +, - Addition and subtraction

Convert the following infix expression from infix to prefix and postfix.

(Solution on page 95: "Data structures using C" by Aaron tanenbaum)

Infix Prefix postfix

A+B-C

(A+B) ' (C-D)


I
A$ B ' C- D + E / F / (G + H)

A-B/(C'D$E)
� -
'
, •\

- What are the major data structures used in the following areas: RDBMS, Network data
model & Hierarchical data model?
Ans
The major data structures used are as follows:
o RDBMS -Array (i.e., Array of structures)
o Network data model -Graph
o Hierarchical data model -Trees

- What is the data structures used to perform recursion?


Ans
Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom
to return when the function has to return. Recursion makes use of system stack for storing the
return addresses of the function calls. Every recursive function has its equivalent iterative (non•
recursive) function. Even when such equivalent iterative procedures are written, explicit stack is
to be used.

Prof NANDAN GP, MCA, 3

You might also like