Professional Documents
Culture Documents
Ans :-
Its examples are: array, stack, While its examples are: trees and
6. queue, linked list, etc. graphs.
Linear data structures are useful for Non-linear data structures are useful
simple data storage and for representing complex relationships
8. manipulation. and data hierarchies.
To find the address of any element in a 2-Dimensional array there are the
following two ways-
1. Row Major Order
2. Column Major Order
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("Enter a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
a) ( A – B ) * ( D / E)
b) ( A + B ^ D) / ( E – F ) + G
c) ( A * ( B + D ) / E – F * ( G + H / K)
b) Infix expression: (A + B ^ D) / (E – F) + G
We can convert this infix expression to prefix notation
using the following steps:
Reverse the order of the operands and operators: + / ^ + A
BD-EFG
Move the operators before their corresponding operands:
+/^+ABD-EFG
The prefix expression equivalent to the given infix
expression is: + / ^ + A B D - E F G
Stack: []
Output:
Token: ( ( A + B ) * D ) ^ ( E – F )
Action: A B + D * E F – ^
Token: ( ( A + B ) * D ) ^ ( E – F )
Output: A B + D * E F – ^
Token: ( ( A + B ) * D ) ^ ( E – F )
Stack: (
Output: A B + D * E F – ^
Token: ( ( A + B ) * D ) ^ ( E – F )
Stack:
Output: A B + D * E F – ^
Token: ( ( A + B ) * D ) ^ ( E – F )
Stack: *
Output: A B + D * E F – ^
Token: ( ( A + B ) * D ) ^ ( E – F )
Stack:
Output: A B + D * E F – ^ *
Pop 3 and 7 from the stack, subtract 3 from 7, and push the
result (4) onto the stack.
Stack: [12, 4]
Pop 5 and 1 from the stack, add them together, and push the
result (6) onto the stack.
Stack: [3, 2, 6]
Pop 12 and 3 from the stack, add them together, and push
the result (15) onto the stack.
Stack: [15]
Q 19. Execute the following Infix expression Q to
equivalent prefix expression P using algorithm:
Q: (m+n) * (k+p) / (g/h)^(a^b/c).
Ans:- To convert an infix expression to prefix notation, we
can use the following steps:
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
def is_empty(self):
return len(self.items) == 0
Inserting an element:
Algorithm: insert(queue, element)
if queue is full:
return "Overflow"
else if queue is empty:
front = 0
rear = 0
queue[rear] = element
else:
Algorithm: delete(queue)
if queue is empty:
return "Underflow"
else if front == rear:
element = queue[front]
front = -1
rear = -1
else:
element = queue[front]
front = (front + 1) % max_size
return element