DEC 2022 Data Structure Solution
DEC 2022 Data Structure Solution
Q1)
Efficient use of memory: In a circular queue, when the rear pointer reaches the end of the queue, it wraps
around to the beginning, which allows for efficient use of memory compared to a linear queue.
Easier for insertion-deletion: In the circular queue, if the queue is not fully occupied, then the elements
can be inserted easily in the vacant locations. But in the case of a linear queue, insertion is not possible
once the rear pointer reaches the last index even if there are empty locations present in the queue.
Better performance: Circular queue offers better performance in situations where data is frequently
added and removed from the queue as compared to a linear queue.
Improved flexibility: With a circular queue, the front and rear pointers can move in either direction,
allowing for greater flexibility in implementing queue operations.
Reduced overhead: Since a circular queue requires a fixed amount of memory, it reduces the overhead
of memory allocation and de allocation as compared to a dynamic data structure like a linked list-based
linear queue.
Function to insert element in circular queue
A Binary Search Tree (BST) is a special type of tree where the value of the left child node’s always
less than the parent node and the value of the right child node is greater than the parent node.
Commonly performed operations on binary search trees are searching, insertion, and deletion.
Here, we see Delete operation in detail.
Delete Operation
In a binary search tree, the Delete function is used to delete the specified node.
But, delete a node from a binary search tree in such a way, that the property of the binary search tree
doesn't violate.
To achieve this there are three methods for deleting a node from a binary search tree are used.
In-order Traversal Sequence of the following Tree is 15, 25, 30, 60, 65, 75, 85, 90, 95.
A node with two children can be deleted from the BST in the following two ways:
Way 1:
Way 2:
Q.2
A) Construct AVL Tree for following67, 34, 90, 22, 45, 11, 2, 78, 38, 122
B) Postfix Evaluation
Algorithm
Q3.
A)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head,*tail;
int count = 1 ;
void createList(int n);
void insertEnd();
void del_from_beg();
void displayList();
void countEvenOdd();
int main()
{
int n, i;
printf("Enter total no of nodes\t");
scanf("%d",&n);
for (i=0; i<=n ; i++)
{
count++ ;
}
createList(n);
printf("\nThe nodes are\n");
displayList();
insertEnd();
printf("\nThe nodes are\n");
displayList();
del_from_beg();
printf("\nThe nodes after deleting first node\n");
displayList();
displayList();
countEvenOdd();
void createList(int n)
{
struct node *newnode;
int i;
head=0;
for(i=0;i<n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter new node");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0)
{
head=tail=newnode;
tail->next=head;
}
else
{
tail->next=newnode;
tail=newnode;
tail->next=head;
}
}
}
void insertEnd()
{
//Create new node
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\n Enter data to insert at end\n");
scanf("%d",&newNode->data);
tail->next=newNode;
tail=newNode;
tail->next=head;
}
void del_from_beg()
{
struct node* temp;
temp = head; // bringing temp at starting
head=head->next; //link between head and second node
free(temp); //relese memory
tail->next=head;
void del_from_end()
{
struct node* temp,*prev;
temp=head;
while(temp -> next != head)
{
prev = temp;
temp = temp -> next;
}
prev -> next = head;
free(temp);
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("\nList is empty.");
}
else
{
temp = head;
while(temp -> next != head)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("%d\t", temp->data);
}
printf("Circular Print %d",tail->next->data);
}
void countEvenOdd()
{
struct node *temp;
int count=0,evencount=0,oddcount=0;
temp = head ;
while (temp->next!= head)
{
if(temp->data%2==0)
{
printf("\n%d is Even Number",temp->data);
printf("\tStored at %u",temp);
evencount++;
}
else
{
printf("\n%d is Odd Number",temp->data);
printf("\tStored at %u",temp);
oddcount++;
}
temp = temp->next ;
count++ ;
}
if(temp->data%2==0)
{
printf("\n%d is Even Number",temp->data);
printf("\tStored at %u",temp);
evencount++;
}
else
{
printf("\n%d is Odd Number",temp->data);
printf("\tStored at %u",temp);
oddcount++;
}
count++;
printf("\n");
printf("\nEven COUNT IS %d",evencount);
printf("\nOdd COUNT IS %d",oddcount);
printf("\nCOUNT IS %d",count);
}
B) C program to simulate Queue as linked List
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
struct node *front=0,*rear=0;
int main()
{
int i=1,select,item;
while(i)
{
printf("\nMainMenu");
printf("\n1:ENQUEUE");
printf("\n2:DEQUEUE");
printf("\n3:DISPLAY");
printf("\n4:EXIT");
printf("\nEnteryourchoice");
scanf("\n%d",&select);
switch(select)
{
case 1:
printf("\nEnter the data to insert in the Queue from rear:");
scanf("\n%d",&item);
enqueue(item);
break;
case 2:
printf("\nDeletingfromthefront:");
dequeue();
break;
case 3:
printf("\nThelistis:");
display();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Choice");
break;
}
printf("\n Do u want to contineu, please enter 1 or 0\n");
scanf("%d", &i);
}
return 0;
}
void enqueue(int n)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=n;
newnode->next=0;
if(front==0&&rear==0)
{
front=rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
void dequeue()
{
struct node *temp;
if(front==0&&rear==0)
{
printf("\nUnderflow");
}
else
{
temp=front;
printf("\nDeleted item is %d",front->data);
front=front->next;
free(temp);
}
}
void display()
{
struct node *temp;
temp=front;
if(front==NULL)
{
printf("\nUnderflow");
}
else
{
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
}}
Q4)
A) Huffman Tree
Combine g and P with lowest frequency Now lowest frequencies are 25 and 33
for r and e combine them
r=00
e=01
g=100
p=101
i=11
B) Discuss various ways to represent graph in Memory
Q.6
Depth-first Search
- The depth-first search algorithm in following graph progresses by expanding the starting node A of
graph and then going deeper and deeper until the goal node is found, or until a node that has no children is
encountered.
- When a dead-end is reached, the algorithm backtracks, returning to the most recent node that has not
been completely explored.
- Depth-first search begins at a starting node A which becomes the current node.
A: B, C, D
B: C, E
C: E
D: C, F
E: A
F: C
G: D, F, H
H: C
Recursive Function:
if (!visited[*i])
DFSUtil(*i, visited);
void Graph::DFS(int v)
visited[i] = false;
DFSUtil(v, visited);