Uploaded by OPTIMUS JJJ

Jasurbek Umarov A85204920112 CSIT124-B.SC IT mid term DS

advertisement
Enrollment No A85204920112
MID-TERM EXAMINATION
SPRING SEMESTER
2020-21
Name of Programme – B.Sc (IT) Section
Course Title – Data Structures Using C
Course Code – CSIT 124
Name of Student: Jasurbek Umarov
Section 1
Semester: II
Max. Marks:10
SECTION-A (6 Marks)
Note: Attempt any two questions. Each question carries 3 marks.
Ques – 1 Convert the infix expression into prefix expression and show the proper steps
a. A + B * (C - D)
b. (A - B/C) * (A/K-L)
c. A * B + C / D
Ques – 2 Write a C function to implement push and pop functions?
Ques - 3 Write a program to insert a new element in the given unsorted array at kth position.
SECTION-B (4 Marks)
Ques – 4 Write an algorithm for converting Prefix expression into postfix expression and also
convert the given expression *+AB-CD into postfix expression with proper diagram of stack.
SECTION-A
Q1)
The two major operations of Stack are PUSH & POP. Push operation
inserts a stack element and deletes a stack element from the pop operation.
A stack is a linear data structure in which data are pushed on one side and
data removed from the same side as in the previous version (LIFO).
For PUSH
int push( int stack[max],int *top, int data)
{
if( *top == max -1 )
return(-1);
else
{
*top = *top + 1;
stack[*top] = data;
return(1);
}
}
For POP
int pop( int stack[max], int *top)
{
int data;
if( *top == -1 )
return(-1);
else
{
data = stack[*top];
*top = *top - 1;
return(data);
}
}
Q3)
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
for (i = 0; i < 10; i++)
arr[i] = i + 1;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
x = 50;
pos = 5;
n++;
for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
SECTION-B
Q4)
Algorithm for Prefix to Postfix:
 Read the Prefix expression in reverse order (from right to left)
 If the symbol is an operand, then push it onto the Stack
 If the symbol is an operator, then pop two operands from the Stack
 Create a string by concatenating the two operands and the operator after
them.
 string = operand1 + operand2 + operator
 Push the resultant string back to Stack
 Repeat the above steps until end of Prefix expression.
Input: Prefix: *+AB-CD
Output: Postfix: AB+CD-*
Explanation: Prefix to Infix: (A+B) * (C-D)
Infix to Postfix: AB+CD-*
Download