0% found this document useful (0 votes)
2 views

General Questions on Data Structures

The document provides a comprehensive overview of data structures, including definitions, types (linear and non-linear), and examples. It covers memory allocation in C/C++, operations on arrays, strings, linked lists, stacks, queues, trees, graphs, hashing, sorting, searching algorithms, and complexity analysis. Additionally, it discusses applications of data structures and advanced topics like graph algorithms and heaps.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

General Questions on Data Structures

The document provides a comprehensive overview of data structures, including definitions, types (linear and non-linear), and examples. It covers memory allocation in C/C++, operations on arrays, strings, linked lists, stacks, queues, trees, graphs, hashing, sorting, searching algorithms, and complexity analysis. Additionally, it discusses applications of data structures and advanced topics like graph algorithms and heaps.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

General Questions on Data Structures:

1. Define Data Structures


o What is a data structure? Explain the types of data structures
(linear and non-linear) with examples.
2. Memory Allocation in C/C++
o How memory is allocated dynamically in C/C++? Explain malloc(),
calloc(), and new operators.
o What is the difference between stack and heap memory
allocation?

Array and Strings:


3. Arrays
o Write a C/C++ program to implement basic operations on arrays:
insertion, deletion, and searching.
o What are the advantages and disadvantages of arrays over linked
lists?
4. String Manipulation
o Write a C program to reverse a string without using any library
function.
o How do you implement string concatenation in C/C++ without
using library functions?

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.

Stacks and Queues:


7. Stack Implementation
o Write a C/C++ program to implement a stack using arrays.
Implement the operations push, pop, and peek.
o How does a stack help in recursion? Write a program to reverse a
string using a stack.
8. Queue Implementation
o Write a program to implement a queue using arrays. Implement
enqueue and dequeue operations.
o Explain the concept of a circular queue. Write a C program for its
implementation.
9. Priority Queue
o What is a priority queue? How is it different from a regular queue?
Write a program to implement a priority queue using arrays.

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.

Sorting and Searching Algorithms:


16.Sorting Algorithms
o Explain and implement sorting algorithms: Bubble Sort, Selection
Sort, Merge Sort, Quick Sort.
o Write a C program to implement Merge Sort.
17.Searching Algorithms
o Write a C program to implement Binary Search and explain when it
is used.

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.

Applications of Data Structures:


19.Applications
o How are stacks used in expression evaluation (e.g., converting infix
expressions to postfix)?
o How are queues used in CPU scheduling or resource allocation?
o Discuss the use of trees in hierarchical data representation (e.g.,
file system structure).

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).

Advanced Topics (If applicable):


21.Graph Algorithms
o Implement a program in C/C++ for finding the shortest path using
the Bellman-Ford algorithm.
o Write a program to detect a cycle in a graph using DFS.
22.Heaps
o What is a heap? Implement a Max-Heap and Min-Heap in C/C++.
o Explain the heapify process with an example.
ANSWERS
1. Define Data Structures
Answer:
A data structure is a way of organizing and storing data to perform operations
efficiently. It is a collection of data values and the relationships among them.
Data structures are classified into two types:
 Primitive Data Structures: Integer, Float, Char, etc.
 Non-Primitive Data Structures: Arrays, Linked Lists, Stacks, Queues,
Trees, Graphs.

2. Write a C program to implement an array and perform insertion and


deletion.
Answer:
c
Copy code
#include <stdio.h>

#define MAX 5
int arr[MAX], n = 0;

void insert(int val) {


if (n == MAX) {
printf("Array is full\n");
} else {
arr[n++] = val;
}
}

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;
}

3. What is a Singly Linked List? Write a C program to perform insertion and


deletion in a singly linked list.
Answer:
A singly linked list is a linear data structure where each element (node)
contains two parts: a data field and a pointer to the next node in the list.
Insertion and deletion can be done at the beginning, end, or at a specific
position.
c
Copy code
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

void insertAtEnd(struct Node** head, int val) {


struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
struct Node* temp = *head;
newNode->data = val;
newNode->next = NULL;

if (*head == NULL) {
*head = newNode;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

void deleteAtBegin(struct Node** head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
*head = temp->next;
free(temp);
}

void display(struct Node* head) {


while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;

insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtEnd(&head, 30);
display(head);

deleteAtBegin(&head);
display(head);

return 0;
}

4. Write a C program to implement a stack using arrays.


Answer:
A stack is a linear data structure that follows the LIFO (Last In First Out)
principle. The operations are push (to add an element) and pop (to remove the
top element).
c
Copy code
#include <stdio.h>
#include <stdlib.h>

#define MAX 5
int stack[MAX];
int top = -1;

void push(int val) {


if (top == MAX - 1) {
printf("Stack Overflow\n");
} else {
stack[++top] = val;
printf("%d pushed to stack\n", val);
}
}

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;
}

5. Write a C program to implement a queue using arrays.


Answer:
A queue is a linear data structure that follows the FIFO (First In First Out)
principle. The operations are enqueue (to add an element) and dequeue (to
remove the front element).
c
Copy code
#include <stdio.h>
#include <stdlib.h>

#define MAX 5
int queue[MAX];
int front = -1, rear = -1;

void enqueue(int val) {


if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1) {
front = 0;
}
queue[++rear] = val;
printf("%d enqueued to queue\n", val);
}
}

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;
}

6. Write a C program to implement a binary tree and perform inorder


traversal.
Answer:
A binary tree is a tree data structure where each node has at most two
children, referred to as the left and right child.
c
Copy code
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* newNode(int data) {


struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}

void inorder(struct Node* root) {


if (root == NULL) {
return;
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->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);

printf("Inorder Traversal: ");


inorder(root);
return 0;
}

7. Write a C program to implement Binary Search.


Answer:
Binary Search is a divide-and-conquer algorithm used to find the position of a
target value within a sorted array.
c
Copy code
#include <stdio.h>
int binarySearch(int arr[], int low, int high, int key) {
if (low <= high) {
int mid = (low + high) / 2;

if (arr[mid] == key) {
return mid;
}

if (arr[mid] > key) {


return binarySearch(arr, low, mid - 1, key);
} else {
return binarySearch(arr, mid + 1, high, key);
}
}
return -1;
}

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;
}

What is a Data Structure?


A data structure is a specialized format for organizing, storing, and managing
data in a computer system. It provides a way to efficiently perform operations
such as accessing, inserting, deleting, and updating data. Data structures are
fundamental for managing large amounts of data in a way that ensures
efficiency in operations such as searching, sorting, and updating data.
In simple terms, a data structure allows us to organize data in such a way that
we can manipulate it in the most efficient way possible.
Types of Data Structures
Data structures can be broadly classified into two categories:
1. Linear Data Structures
2. Non-Linear Data Structures

1. Linear Data Structures


A linear data structure is a data structure in which data elements are arranged
in a sequential order. In this structure, each element has a unique predecessor
and successor. Linear data structures typically allow sequential access to the
data, meaning that you need to traverse the data in order (from start to end or
vice versa).
Types of Linear Data Structures:
 Arrays
An array is a collection of elements of the same data type stored in
contiguous memory locations. Elements are accessed using an index.
Example:
c
Copy code
int arr[5] = {10, 20, 30, 40, 50}; // Array of integers
Operations:
o Insertion
o Deletion
o Searching (using an index)
o Traversal (iterating over each element)
 Linked Lists
A linked list is a linear data structure where each element (node)
contains data and a reference (or link) to the next node in the sequence.
Unlike arrays, linked lists do not require contiguous memory locations.
Example:
A linked list can have nodes like:
arduino
Copy code
Node1 → Node2 → Node3 → NULL
Types:
o Singly Linked List
o Doubly Linked List
o Circular Linked List
Operations:
o Insertion
o Deletion
o Traversal (starting from the head node)
 Stacks
A stack is a linear data structure that follows the LIFO (Last In First Out)
principle, where the last element added is the first to be removed.
Operations:
o push() (to add an element)
o pop() (to remove the top element)
o peek() (to view the top element)
Example:
scss
Copy code
push(10) → push(20) → push(30) → pop() → pop() → stack becomes empty
 Queues
A queue is a linear data structure that follows the FIFO (First In First Out)
principle, where the first element added is the first to be removed.
Operations:
o enqueue() (to add an element)
o dequeue() (to remove an element)
o front() (to view the front element)
Example:
scss
Copy code
enqueue(10) → enqueue(20) → dequeue() → front()

2. Non-Linear Data Structures


A non-linear data structure is a data structure where data elements are not
arranged sequentially. In these structures, each element can have more than
one predecessor or successor. This allows for more complex relationships
between elements, making these structures suitable for representing
hierarchical and interconnected data.
Types of Non-Linear Data Structures:
 Trees
A tree is a hierarchical data structure where each node has a value and a
list of references (or pointers) to other nodes (its children). The topmost
node is called the root, and nodes without children are called leaves.
Example:
mathematica
Copy code
A
/\
B C
/\ \
D E F
Types:
o Binary Tree
o Binary Search Tree (BST)
o AVL Tree
o Heap
Operations:
o Insertion
o Deletion
o Traversal (in-order, pre-order, post-order)
 Graphs
A graph is a collection of nodes (vertices) and edges (connections
between nodes) where nodes may or may not be connected to each
other. Graphs are used to represent networks, like social networks or
computer networks.
Example: A graph can be represented like:
mathematica
Copy code
A-B-C
| |
D-E
Types:
o Directed Graph (edges have direction)
o Undirected Graph (edges have no direction)
o Weighted Graph (edges have weights)
Operations:
o Traversal (DFS, BFS)
o Shortest Path (Dijkstra’s Algorithm)
o Cycle detection

Comparison of Linear and Non-Linear Data Structures


Aspect Linear Data Structures Non-Linear Data Structures
Order of Elements are stored
Elements are not stored sequentially.
elements sequentially.

Memory Contiguous memory (in Non-contiguous memory (in trees,


allocation arrays). graphs).
Simple, one-to-one
Complex relationships, e.g., parent-child
Relationships relationship between
elements. in trees, interconnected nodes in graphs.

Arrays, Linked Lists, Stacks,


Examples Trees, Graphs.
Queues.

Direct access (e.g., array


Access time Indirect access, requires traversal.
index

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

You might also like