General Questions on Data Structures
General Questions on Data Structures
Linked Lists:
5. Singly Linked List
o Write a C program to implement a singly linked list with operations
like insertion, deletion, and display.
o What is the difference between singly linked lists and doubly
linked lists? Write a program for doubly linked list insertion and
deletion.
6. Circular Linked List
o Explain the concept of a circular linked list. Write a C program to
traverse a circular linked list.
Trees:
10.Binary Tree
o What is a binary tree? Explain different types of binary trees (full,
complete, perfect).
o Write a C program to implement in-order, pre-order, and post-
order tree traversals.
11.Binary Search Tree (BST)
o What is a Binary Search Tree (BST)? Write a C program to search
an element in a BST.
o Write a C program to insert a new node in a BST.
12.AVL Tree
o What is an AVL tree? Explain how balancing is done in an AVL tree.
Graphs:
13.Graph Representation
o Explain the different methods of graph representation (adjacency
matrix and adjacency list).
o Write a C/C++ program to perform Depth-First Search (DFS) and
Breadth-First Search (BFS) on a graph.
14.Shortest Path Algorithm
o Write a C program to implement Dijkstra’s algorithm for finding
the shortest path in a graph.
Hashing:
15.Hash Tables
o What is a hash table? Explain how collision resolution is handled in
hash tables (chaining and open addressing).
o Write a C program to implement a hash table using separate
chaining.
Complexity Analysis:
18.Time and Space Complexity
o What is Big O notation? Explain the time and space complexity of
basic data structure operations (insertion, deletion, searching).
o Compare the time complexity of various sorting algorithms.
Miscellaneous:
20.Dynamic Data Structures
o What are dynamic data structures? Explain how they are different
from static data structures.
o Implement a dynamic array in C/C++ (e.g., resizing the array when
it is full).
#define MAX 5
int arr[MAX], n = 0;
void delete() {
if (n == 0) {
printf("Array is empty\n");
} else {
n--;
printf("Deleted value: %d\n", arr[n]);
}
}
void display() {
if (n == 0) {
printf("Array is empty\n");
} else {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
}
int main() {
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
display();
delete();
display();
return 0;
}
struct Node {
int data;
struct Node* next;
};
if (*head == NULL) {
*head = newNode;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
display(head);
deleteAtBegin(&head);
display(head);
return 0;
}
#define MAX 5
int stack[MAX];
int top = -1;
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
} else {
printf("%d popped from stack\n", stack[top--]);
}
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
display();
pop();
display();
return 0;
}
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
void dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
} else {
printf("%d dequeued from queue\n", queue[front]);
if (front == rear) {
front = rear = -1;
} else {
front++;
}
}
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
} else {
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}
struct Node {
int data;
struct Node* left;
struct Node* right;
};
int main() {
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
if (arr[mid] == key) {
return mid;
}
int main() {
int arr[] = {2, 4, 7, 10, 14, 18, 25};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 10;
int result = binarySearch(arr, 0, n - 1, key);
if (result == -1) {
printf("Element not found\n");
} else {
printf("Element found at index %d\n", result);
}
return 0;
}
Conclusion
Linear Data Structures are simpler to implement and use when you need
sequential access to elements. They are best suited for problems that
involve direct access to elements in a fixed order.
Non-Linear Data Structures are more complex but allow you to
represent hierarchical or interconnected data. They are used in
situations where relationships between elements are complex, such as
representing networks or hierarchical data.
Both types of data structures are foundational in computer science and
essential for building efficient algorithms and applications.
4o mini