DSA_Detailed_Questions_Answers
DSA_Detailed_Questions_Answers
**1. Arrays**
**Q: Write algorithms for searching an element in an array (Linear Search and Binary Search).**
- **Linear Search:**
- **Algorithm:**
- **Code:**
```c
```
- **Algorithm:**
4. If `arr[mid] < target`, search in the right half (`low = mid + 1`).
5. If `arr[mid] > target`, search in the left half (`high = mid - 1`).
- **Code:**
```c
```
- **Singly Linked List:** Each node contains data and a pointer to the next node. Traversal is unidirectional.
- **Circular Linked List:** The last node points to the first node, forming a circular structure.
**3. Stack**
- **Code:**
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char stack[SIZE];
if ((c == ')' && topChar == '(') || (c == '}' && topChar == '{') || (c == ']' && topChar == '[')) {
pop();
} else {
return 0;
int main() {
printf(isBalanced(expr) ? "Balanced
");
return 0;
```
**4. Queue**
```c
#include <stdio.h>
int queue[SIZE];
if (rear == SIZE - 1) {
printf("Queue Overflow
");
return;
queue[++rear] = value;
int dequeue() {
printf("Queue Underflow
");
return -1;
return queue[front++];
}
int main() {
enqueue(10);
enqueue(20);
printf("Dequeued: %d
", dequeue());
return 0;
```
**7. Graphs**
**Q: Explain the representations of graphs (Adjacency Matrix and Adjacency List).**
1. **Adjacency Matrix:** A 2D array where element `matrix[i][j] = 1` indicates an edge from `i` to `j`.
```c
visited[vertex] = 1;
```
```c
visited[start] = 1;
queue[rear++] = start;
visited[i] = 1;
queue[rear++] = i;
```
**10. Hashing**
**Q: Write a program for Chaining (collision handling).**
- **Code:**
```c
struct Node {
int data;
};
newNode->data = key;
newNode->next = hashTable[index];
hashTable[index] = newNode;
void display() {
temp = temp->next;
printf("NULL
");
```