0% found this document useful (0 votes)
10 views9 pages

DSU - Solution CT2

The document contains solutions for a Class Test-I paper for the Rasiklal M. Dhariwal Institute of Technology for the academic year 2024-2025, focusing on data structures and algorithms. It includes various programming tasks, explanations of data structures like circular queues, stacks, and heaps, as well as conversion of expressions between infix, postfix, and prefix notations. The solutions are structured as answers to specific questions, demonstrating practical coding skills and theoretical knowledge in computer science.

Uploaded by

nikitamahadik182
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)
10 views9 pages

DSU - Solution CT2

The document contains solutions for a Class Test-I paper for the Rasiklal M. Dhariwal Institute of Technology for the academic year 2024-2025, focusing on data structures and algorithms. It includes various programming tasks, explanations of data structures like circular queues, stacks, and heaps, as well as conversion of expressions between infix, postfix, and prefix notations. The solutions are structured as answers to specific questions, demonstrating practical coding skills and theoretical knowledge in computer science.

Uploaded by

nikitamahadik182
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/ 9

Shri Jain Vidya Prasarak Mandal’s

Rasiklal M. Dhariwal Institute of Technology


Class Test –I paper Solution
2024-2025
Year/Course : CO3K Time : 1.30 Hr
Subject : DSU Marks : 30

Q. QUESTION MARK
NO. S
Q.1) Attempt any four of the following Marks
10
(5*2)
2
a) Evaluate prefix expression + - 2 7 * 8 / 12 4
Ans- convert the expression into postfix:- 4 12 / 8 * 7 2 - +
Token Operand1 Operand2 output Stack
4 4
12 4,12
/ 12 4 12/4=3 3
8 3,8
* 8 3 8*3=24 24
7 24,7
2 24,7,2
- 2 7 2-7= -5 24,-5
+ -5 24 19
Result of the above prefix expression evaluation = 19.

2
b) Write a program of factorial using Recursion.
Ans- Program –
#include<stdio.h>
#include<conio.h>
Int fact (int n)
{
If(n==0)
return 1;
else
return (n*fact(n-1));
}
Void main()
{
Int n;
Clrscr();
Printf(“enter a number: “);
Scanf(“%d”, &n);
Printf (“\n The factorial of % d = %d”, n, fact(n));
Getch();
}
c) Draw and explain circular queue. 2
Ans:-

- A circular queue is a type of data structure that follows the First-In-FirstOut (FIFO) principle, where
elements are added and removed in a circular manner.
- In circular queue, the elements are not only physically arranged in the circular manner but logically
they are treated as circularly arranged.
- In circular queue, the Last position of queue is connected to first position hence it is called as circular
queue.
- Circular queue has front and rear pointers and they move about in clockwise direction.
- The queue is implemented using an array. It performs operation such as Enqueue, Dequeue, IsFull,
IsEmpty, display, etc.

d) Differentiate between stack and queue. 2

Sr.no Stack Queue


1. Stack is a data structure in which Queue is a data structure in which insertion
insertion and deletion operations are and deletion operations are performed at
performed at same end called top of different ends called rear and front end
the stack. respectively.
2. In stack an element inserted last is In Queue an element inserted first is deleted
deleted first so it is called Last In First first so it is called First In First Out list.
Out list.
3. In stack only one pointer is used called In Queue two pointers are used called as
as stack top. front and rear.
4. Initial condition for stack is top=-1. Initial condition for queue is rear=-1, front=-
1.
5. In computer system, stack used for In computer system, queue is used for the
procedure call. time-sharing system.
6. Example: Stack of books. Example: Students standing in a line at fees
counter.
7. 6. Representation: Using array: Representation: Using array

Top --->
e) Draw expressio n tree for the expression. (A-B)/((C*D) +E).

f) Define heap data structure.

Ans- A heap is a specialized tree-based data structure that satisfies the heap property.
- The heap is complete binary i.e, all levels and completely filled accept possibly the lowest one, which is
filled form the left. 2
-A heap is a tree based data structure in which all the nodes of the tree are in a specific order each
internal node V satisfies the heap property: the key of V is smaller then or equal to the key of either of its
two childrens.
-A binary heap is a complete binary tree which satisfies the heap ordering property. -The ordering can be
one of two types:
a) Min heap property: the value of each node is greater than or equal to the value of its parent, with the
minimum value element at the root.
b) Max heap property: the value of each node is less than or equal to value of its parents with the
maximum value element at the root. -since, a heap is a complete binary tree, it has a smallest possible
hight- a heap with ‘n’ nodes always has 0(log n) height.
g) Write a C function for preorder traversal.
Ans:- C function –
void preorderTraversal(Node* root)
{
if (root == NULL)
return;
printf ("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
Q.2) Attempt any five of the following. Marks
20
(5*4)
a) Convert infix to post fix. (A*B+C)/(D+E*F/G)
Token Stack Queue
( ( Empty
A ( A
* (* A
B (* AB
+ (+ AB*
C (+ AB*C
) Empty AB*C+
/ / AB+C+
( /( AB*C+
D /( AB*C+D 4
+ /(+ AB*C+D
E /(+ AB*C+DE
* /(+* AB*C+DE
F /(+* AB*C+DEF
/ /(+/ AB*C+DEF*
G /(+/ AB*C+DEF*G
) / AB*C+DEF*G/+
Empty Empty AB*C+DEF*G/+/

The postfix expression is AB*C+DEF*G/+/

b) Write a program of stack implementation using array. 4


Ans- Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX=10
Int stack [MAX], top= -1, i=0, choice, n;
Char ans;
Void push ();
Void pop ();
Void display ();
Void push ()
{ if (top==MAX-1)
Printf(“\n stack is full”);
Scanf(“%d”, &n);
top++;
stack[top]=n;
}
}
Void pop ()
{
if(top==-1)
{
Printf(“\n stack is empty”);
}
else
{
n=stack[top];
top--;
}
}
void display ()
{
if(top==-1)
{
Printf(“\n stack is empty”);
}
else
{
For(i=top;i>=0;i--)
Printf(“\n %d”, stack[i]);
}
}
Void main ()
{
Clrscr ();
do
{
Printf(“\n 1.push \n 2.pop \n 3.display \n 4.exit”);
Printf(“\n Enter your choice:”);
Scanf(“%d”, &choice);
Switch(choice)
{
case1: push ();
break;
case2: pop ();
break;
case3: display ();
break;
case4: exit(0);
break;
default:
printf(“\n Invalid choice”);
break;
}
Printf(“\n do you want to continue [y/n]”);
Ans= getch ();
} While (ans==’y’||ans==’Y’);
getch();
}
4

c) Convert Infix to Prefix (A+B)*C-D/E*(F/G)


Ans:-

The prefix expression is:-


-*+AB C/D*E/FG
d) Write an algorithm and snippet code to insert an element into linear queue. 4
Ans-
Algorithm:
1. Start.
2. Check if queue is full or not by checking condition :
if(rear>=size-1)
printf(“Queue is full”);
3.Else except number to insert into the queue in variable no.
4.Increment rear pointer as rear= rear+1
5.Assign number to q[rear] as q[rear]= no
6.Check if(front==-1) if yes then increment front pointer by one as front= front+1
7.End.

Code:
Void insert (int no)
{
If(rear>=size-1)
{
Printf(“\n Queue is full.”);
}
Else
{
Printf(“Enter no to insert into Queue”);
Scanf(“%d”, &no);
rear++;
q[rear]=no;
if(front==-1)
front++;
}
4

Write an algorithm and snippet code to delete an element into circular queue.
e)
Ans-
Algorithm:
1.Start.
2.Check if queue is empty or not , if yes then print circular queue is empty.
3.Else assign front value to n i.e. n=queue[front].
4.Check if(front==rear). If yes then assign front= rear= -1.
5.Else increment front pointer as front=(front+1)%size.
6.Display deleted element.
7.Stop.

Code:
Void delete ()
{
Int n;
If ( qempty() )
{
Printf(“\n circular queue is empty”);
}
Else
{ 4
n=queue[front];
if(front==rear)
{
front=rear=-1; }
else
{
front=(front+1)%size;
}
printf(“\n deleted element is %d”, n);
}
}

f) Construct a heap for the list using bottom up approach.


2,9,7,6,5,8.
Ans:-
1.

2. Heapify:
a) compare 7 with its child:

b) compare 9 with its children:

c) compare 2 with its children:


g) Construct the BST using data E, B, D, G, A, F, C & perform all tree traversal technique on it.
E
/ \
B G 4
/ \ /
A DF
/
C
 Pre-order: E → B → A → D → C → G → F
 In-order: A → B → C → D → E → F → G
 Post-order: A → C → D → B → F → G → E

You might also like