G. S.
Mandal’s
Maharashtra Institute of Technology, Aurangabad
(An Autonomous Institute)
Department of Computer Science and Engineering
Class: SYCSE
Course: Data Structures
Solution
Q. 1
Questions Marks
a) Linear: Snake Game, Movie ticket booking 2
Text editor cut copy paste, Line in ticket counter
Non Linear : t, file system, Travel Planner
b) Address of A[1700] = 1020 + 2 * (1700 – 1300) 2
= 1020 + 2 * (400)
= 1020 + 800
Address of A[1700] = 1820
Q. 2
a) 1. Insertion in Stack/Push Operation 5
The process of putting a new data element onto stack is known as a Push Operation.
Push operation involves a series of steps −
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, display “stack is FULL”and exit.
• Step 3 − If the stack is not full, increments top to point next empty space.
• Step 4 − Adds data element to the stack location, where top is pointing.
• Step 5 − Returns success.
Example
void push(int data) { if(!isFull())
{
top = top + 1; stack[top] = data;
}
else
{
printf("Could not insert data, Stack is full.\n");
}
}
2. Deletion/Pop Operation
Accessing the content while removing it from the stack, is known as a Pop Operation. In
an array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value.
A Pop operation may involve the following steps −
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
Example
int pop(int data) { if(!isempty()) {
data = stack[top]; top = top - 1;
return data;
}
else
{
printf("Could not retrieve data, Stack is empty.\n");
}
}
Algorithm
o Step 1: IF FRONT = -1 or FRONT > REAR Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT] SET FRONT = FRONT + 1 [END OF IF]
o Step 2: EXIT
b) • First get the element to be inserted, say x 5
• Then get the position at which this element is to be inserted, say pos
• Then shift the array elements from this position to one position
forward(towards right), and do this for all the other elements next to pos.
• Insert the element x now at the position pos, as this is now empty.
#include <stdio.h>
#include <stdlib.h>
int main() {
int size = 10;
int myArray[size];
int newElement = 5;
myArray[size] = newElement;
size++;
for(int i=0;i<size;i++)
printf("%d ",myArray[i]);
return 0;
}
•
Remove
Step 1: Input the size of the array arr[] using num, and then declare the pos variable to
define the position, and i represent the counter value.
Step 2: Use a loop to insert the elements in an array until (i < num) is satisfied.
Step 3: Now, input the position of the particular element that the user or programmer
wants to delete from an array.
Step 4: Compare the position of an element (pos) from the total no. of elements (num+1).
If the pos is greater than the num+1, the deletion of the element is not possible and jump
to step 7.
Step 5: Else removes the particular element and shift the rest elements' position to the left
side in an array.
Step 6: Display the resultant array after deletion or removal of the element from an array.
Q. 3
a) 1. Conversion of Infix to Postfix 6
Algorithm for Infix to Postfix
Step 1: Consider the next element in the input.
Step 2: If it is operand, display it.
Step 3: If it is opening parenthesis, insert it on stack.
Step 4: If it is an operator, then
• If stack is empty, insert operator on stack.
• If the top of stack is opening parenthesis, insert the operator on stack
• If it has higher priority than the top of stack, insert the operator on stack.
• Else, delete the operator from the stack and display it, repeat Step 4.
Step 5: If it is a closing parenthesis, delete the operator from stack and display them until
an opening parenthesis is encountered. Delete and discard the opening parenthesis.
Step 6: If there is more input, go to Step 1.
Step 7: If there is no more input, delete the remaining operators to output.
Expression Stack Output
3 Empty 3
* * 3
3 * 33
/ / 33*
( /( 33*
4 /( 33*4
- /(- 33*4
1 /(- 33*41
) - 33*41-
+ + 33*41-/
6 + 33*41-/6
* +* 33*41-/62
2 +* 33*41-/62
Empty 33*41-/62*+
So, the Postfix Expression is 33*41-/62*+