Data Structure
Data Structure
Q.1 Why to study data structures ? What are the major data structure used in the RDBMS, network
and hierarchical data model.
Studying data structures is essential because they are the foundation of efficient software
development. They help in:
1. Efficient Data Storage & Retrieval – Optimized storage and access to data.
2. Improved Algorithm Performance – Better performance and reduced complexity.
3. Scalability – Handling large datasets efficiently.
4. Memory Optimization – Managing memory effectively.
5. Problem Solving – Many real-world problems are easier to solve with proper data
structures.
6. Software Development & System Design – Used in databases, operating systems,
AI, and more.
RDBMS organizes data in tables with predefined schemas. The major data structures used
include:
Network databases use a graph-like structure with records connected by links. Major data
structures used:
Hierarchical databases follow a tree structure (like XML, LDAP). Major data structures
used:
Graph representation
1 ---- 2
|\
| \
4 -- 3
0111
1000
1011
1010
1→2→3→4
2→1
3→1→3→4
4→1→3
50
/ \
15 62
/ \ / \
5 20 58 91
/ \ \ /
3 8 37 60
/
24
Q.4 What is garbage collection ? who will run garbej collection program ? when it will be run ?
In languages like C and C++, memory management is manual, meaning developers must
allocate (malloc/new) and deallocate (free/delete) memory themselves.
Garbage Collection runs automatically, but the exact timing depends on the language and
implementation:
1. Correctness – It should produce the correct output for all valid inputs.
2. Efficiency – It should be optimized in terms of time complexity and space complexity.
3. Finiteness – It should terminate after a finite number of steps.
4. Definiteness – Every step should be well-defined and clear.
5. Input & Output – It should take valid inputs and generate expected outputs.
6. Generality – It should be applicable to different problem instances, not just a specific case.
7. Simplicity & Maintainability – The algorithm should be easy to understand and modify.
Algorithm analysis helps determine its performance and efficiency in terms of time and
space complexity.
Q.6a) What is a Sparse Matrix? Convert the following sparse matrix into a non-sparse
matrix 1 0 0 0
A sparse matrix is a matrix that has a majority of its elements as zero. It is stored
efficiently using techniques such as compressed row storage (CRS), linked lists, or
coordinate lists, reducing memory usage and improving performance in certain
computations.
A non-sparse matrix (or dense matrix) should contain meaningful values replacing the zero
elements. Since the given matrix contains mostly zeros, we need to replace zeros with
meaningful values based on the problem context.
Example of a possible non-sparse matrix (if replacing zeros with small random values):
1 2 3 4
or
1 1 1 1
or
sql
1 X X X (where X represents relevant nonzero values)
Q.7 Write a short note on dynamic storage management . Explain how it is done in C.
C provides four standard library functions for dynamic memory management, available in the
<stdlib.h> library:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
// Allocate memory for 5 integers
ptr = (int*) malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Initialize and print values
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
// Free allocated memory
free(ptr);
return 0;
}
Key Points:
Ans:-
Step-by-Step Execution:
✅ Popped Item: K
✅ Successful
✅ Popped Item: L
Situations Encountered:
Q.9What is queue? Write an algorithm to implement insert item into queue using singly
linked list?
Ans:-
What is a Queue?
A queue is a linear data structure that follows the FIFO (First In, First Out) principle.
Types of Queues:
C Implementation:
CopyEdit
#include <stdio.h>
#include <stdlib.h>
return 0;
}
Time Complexity:
Q.10 Write an algorithm to evaluate postfix expression using stack and execute your
algorithm with postfix expression 10,5,6,*,+,8,/. Show intermediate stack content after each
operation ?
Steps:
Final Result: 5
C Implementation:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Stack structure
struct Stack {
int top;
float items[MAX];
};
s->top = -1;
if (s->top == MAX - 1) {
printf("Stack Overflow!\n");
return;
s->items[++s->top] = value;
}
// Function to pop an element from stack
if (isEmpty(s)) {
printf("Stack Underflow!\n");
exit(1);
return s->items[s->top--];
initStack(&stack);
if (isdigit(token[0])) {
push(&stack, atof(token));
} else {
switch (token[0]) {
case '+': push(&stack, val1 + val2); break;
return pop(&stack);
// Driver Code
int main() {
char* postfix[] = {"10", "5", "6", "*", "+", "8", "/", NULL};
return 0;
Final Notes:
The algorithm correctly follows LIFO (Last In, First Out) order of the stack.
It handles multiple operators in left-to-right order.
Time Complexity: O(n) (linear traversal of expression).
Space Complexity: O(n) (due to stack usage).
Q.11Store the elements of the given binary tree using:
/\
b c
/\ \
d e f
A binary tree can be stored in an array using level-order indexing (0-based index):
Array Representation:
Index 0123456
[a, b, c, d, e, -, f]
#include <stdio.h>
#include <stdlib.h>
// Define a node structure
struct Node {
char data;
};
newNode->data = data;
return newNode;
// Main function
int main() {
// Creating nodes
root->left = createNode('b');
root->right = createNode('c');
root->left->left = createNode('d');
root->left->right = createNode('e');
root->right->right = createNode('f');
printf("Binary tree created using linked list!\n");
return 0;
Q.12What is abstract data type (ADT)? Explain ,why queue is called ADT?
ADTs focus on the functionality rather than the implementation details. They define:
Thus, Queue is an ADT because it provides an interface for operations but does not enforce
a specific way of storing data.
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;
int dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return -1;
}
return queue[front++];
}
2. Queue using a Linked List
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL, *rear = NULL;
Key Takeaways
Queue is an ADT because it defines what operations can be performed, not how.
It follows the FIFO principle.
It can be implemented in multiple ways while still adhering to the defined operations.
An undirected graph is a graph where edges have no direction. That is, if there is an edge
between two vertices A and B, you can move both ways (A → B and B → A).
✅ Example Figure:
A----B
| |
C----D
The edges (A, B), (A, C), (B, D), and (C, D) do not have directions.
3. Simple Path
1. No vertex is repeated.
2. No edge is traversed more than once.
✅ Example Figure:
A→B→C→D
4. Cycle
✅ Example Figure:
A----B
| |
D----C
Cycle: A → B → C → D → A
Summary Table