Data Structures Lab
Data Structures Lab
1. Write a program to search for an element in an array using binary and linear search.
#include <stdio.h>
if (arr[mid] == key)
return mid; // Element found, return index
else if (arr[mid] < key)
left = mid + 1; // Search in right half
else
right = mid - 1; // Search in left half
}
return -1; // Element not found
}
int main() {
int n, key, choice;
int arr[n];
By Prof. Amogha A R
printf("Enter the element to search: ");
scanf("%d", &key);
return 0;
}
How it Works?
2. The user enters the array size and elements (in sorted order for binary search).
3. The program asks for the element to search.
4. Linear Search scans each element one by one.
5. Binary Search efficiently finds the element in a sorted array.
6. The program displays the index if found or a "not found" message.
By Prof. Amogha A R
By Prof. Amogha A R
By Prof. Amogha A R
2. Write a program to sort list of n numbers using Bubble Sort algorithms.
#include <stdio.h>
int main() {
int n;
int arr[n];
By Prof. Amogha A R
return 0;
}
3. Perform the Insertion and Selection Sort on the input {75,8,1,16,48,3,7,0} and display the
output in descending order.
By Prof. Amogha A R
#include <stdio.h>
// Move elements that are smaller than key one position ahead
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int arr1[] = {75, 8, 1, 16, 48, 3, 7, 0};
int arr2[] = {75, 8, 1, 16, 48, 3, 7, 0};
int n = sizeof(arr1) / sizeof(arr1[0]);
By Prof. Amogha A R
// Sorting using Insertion Sort
insertionSort(arr1, n);
printf("Sorted array using Insertion Sort (Descending Order): ");
printArray(arr1, n);
return 0;
}
4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete
8,61,27 from the list. Display your list after each insertion and deletion.
By Prof. Amogha A R
#include <stdio.h>
#include <stdlib.h>
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("Inserted: %d\n", value);
}
int main() {
struct Node* head = NULL;
// Inserting elements
insert(&head, 61);
display(head);
insert(&head, 16);
display(head);
insert(&head, 8);
display(head);
insert(&head, 27);
display(head);
// Deleting elements
deleteNode(&head, 8);
display(head);
deleteNode(&head, 61);
display(head);
deleteNode(&head, 27);
display(head);
return 0;
By Prof. Amogha A R
}
5. Write a program to insert the elements {45, 34, 10, 63,3} into linear queue and delete
three elements from the list. Display your list after each insertion and deletion.
By Prof. Amogha A R
#include <stdio.h>
#define SIZE 5 // Define the maximum size of the queue
int main() {
// Insert elements into the queue
enqueue(45);
display();
enqueue(34);
display();
By Prof. Amogha A R
enqueue(10);
display();
enqueue(63);
display();
enqueue(3);
display();
dequeue();
display();
dequeue();
display();
return 0;
}
By Prof. Amogha A R
By Prof. Amogha A R
6. Write a program to simulate the working of Circular queue using an array.
#include <stdio.h>
#define SIZE 5 // Define the maximum size of the queue
int main() {
// Insert elements into the circular queue
enqueue(10);
display();
enqueue(20);
display();
enqueue(30);
display();
enqueue(40);
display();
enqueue(50);
display();
dequeue();
display();
enqueue(70);
display();
return 0;
}
By Prof. Amogha A R
By Prof. Amogha A R
By Prof. Amogha A R
7. Write a program to insert the elements {61,16,8,27} into ordered singly linked list and
delete 8,61,27 from the list. Display your list after each insertion and deletion.
8. #include <stdio.h>
9. #include <stdlib.h>
10.
11. // Structure for a node in the linked list
12. struct Node {
13. int data;
14. struct Node* next;
15. };
16.
17. // Function to insert a node into the linked list in sorted order
18. void insertOrdered(struct Node** head, int value) {
19. struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
20. newNode->data = value;
21. newNode->next = NULL;
22.
23. if (*head == NULL || (*head)->data >= value) {
24. // Insert at the beginning if list is empty or value is
the smallest
25. newNode->next = *head;
26. *head = newNode;
27. } else {
28. // Find the correct position
29. struct Node* current = *head;
30. while (current->next != NULL && current->next->data <
value) {
31. current = current->next;
32. }
33. newNode->next = current->next;
34. current->next = newNode;
35. }
36. printf("Inserted: %d\n", value);
37. }
38.
39. // Function to delete a node from the linked list
40. void deleteNode(struct Node** head, int value) {
41. struct Node* temp = *head, *prev = NULL;
42.
By Prof. Amogha A R
43. // If the head node itself holds the value
44. if (temp != NULL && temp->data == value) {
45. *head = temp->next;
46. free(temp);
47. printf("Deleted: %d\n", value);
48. return;
49. }
50.
51. // Search for the node
52. while (temp != NULL && temp->data != value) {
53. prev = temp;
54. temp = temp->next;
55. }
56.
57. // If the value is not in the list
58. if (temp == NULL) {
59. printf("Value %d not found in the list!\n", value);
60. return;
61. }
62.
63. // Unlink the node
64. prev->next = temp->next;
65. free(temp);
66. printf("Deleted: %d\n", value);
67. }
68.
69. // Function to display the linked list
70. void display(struct Node* head) {
71. if (head == NULL) {
72. printf("List is empty!\n");
73. return;
74. }
75. printf("Current List: ");
76. while (head != NULL) {
77. printf("%d -> ", head->data);
78. head = head->next;
79. }
80. printf("NULL\n");
81. }
82.
83. int main() {
84. struct Node* head = NULL;
85.
86. // Insert elements in sorted order
87. insertOrdered(&head, 61);
88. display(head);
89.
By Prof. Amogha A R
90. insertOrdered(&head, 16);
91. display(head);
92.
93. insertOrdered(&head, 8);
94. display(head);
95.
96. insertOrdered(&head, 27);
97. display(head);
98.
99. // Delete elements from the list
100. deleteNode(&head, 8);
101. display(head);
102.
103. deleteNode(&head, 61);
104. display(head);
105.
106. deleteNode(&head, 27);
107. display(head);
108.
109. return 0;
110. }
111.
By Prof. Amogha A R
By Prof. Amogha A R
8. Write a program for Tower of Honoi problem using recursion.
#include <stdio.h>
int main() {
int n; // Number of disks
printf("Enter the number of disks: ");
scanf("%d", &n);
return 0;
}
By Prof. Amogha A R
By Prof. Amogha A R
9. Write recursive program to find GCD of 3 numbers.
#include <stdio.h>
By Prof. Amogha A R
}
int main() {
int num1, num2, num3;
return 0;
}
By Prof. Amogha A R
10. Write a program to demonstrate working of stack using linked list.
#include <stdio.h>
#include <stdlib.h>
By Prof. Amogha A R
printf("%d pushed to stack.\n", value);
}
// Main function
int main() {
int choice, value;
while (1) {
printf("\nStack using Linked List:\n");
printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
By Prof. Amogha A R
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
}
Example Output:
By Prof. Amogha A R
Enter your choice: 4
Stack elements: 30 20 10
By Prof. Amogha A R
11. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression
Logic:
1. Infix Expression:
x^y / (5 * z) + 2
2. Operator Precedence & Associativity:
^ (Exponentiation) → Highest precedence, Right to Left
associativity.
* and / (Multiplication & Division) → Medium precedence, Left to
Right associativity.
+ (Addition) → Lowest precedence, Left to Right associativity.
Parentheses () → Override precedence.
3. Postfix Conversion Steps (Using Stack)
Convert x^y → xy^
Convert 5 * z → 5z*
Convert xy^ / (5 * z) → xy^5z*/
Convert xy^5z*/ + 2 → xy^5z*/2+
4. Final Postfix Expression:
xy^5z*/2+
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
struct Stack {
int top;
char items[MAX];
};
By Prof. Amogha A R
for (i = 0; infix[i] != '\0'; i++) {
char ch = infix[i];
int main() {
char infix[] = "x^y/(5*z)+2";
char postfix[MAX];
infixToPostfix(infix, postfix);
printf("Postfix Expression: %s\n", postfix);
return 0;
}
By Prof. Amogha A R
By Prof. Amogha A R
12. Write a program to evaluate a postfix expression 5 3+8 2 - *.
Logic:
Postfix Expression Given:
53+82-*
Evaluation:
-53+→8
-82-→6
- 8 * 6 → 48
Final Answer: 48
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
struct Stack {
int top;
int items[MAX];
};
// Initialize stack
By Prof. Amogha A R
void initStack(struct Stack *s) {
s->top = -1;
}
// Ignore spaces
if (ch == ' ')
continue;
switch (ch) {
case '+': result = val1 + val2; break;
case '-': result = val1 - val2; break;
case '*': result = val1 * val2; break;
case '/': result = val1 / val2; break;
default:
printf("Invalid operator!\n");
return -1;
}
// Main function
int main() {
char postfix[] = "5 3 + 8 2 - *"; // Given postfix expression
int result = evaluatePostfix(postfix);
By Prof. Amogha A R
13. Write a program to create a binary tree with the elements {18,15,40,50,30,17,41} after
creation insert 45 and 19 into tree and delete 15,17 and 41 from tree. Display the tree on
each insertion and deletion operation.
#include <stdio.h>
#include <stdlib.h>
By Prof. Amogha A R
// Function to insert a node in BST
struct Node* insert(struct Node* root, int value) {
if (root == NULL) return createNode(value);
return root;
}
// Main function
int main() {
struct Node* root = NULL;
printf("\nInserting 19:\n");
root = insert(root, 19);
inorder(root);
printf("\n");
// Delete nodes
printf("\nDeleting 15:\n");
root = deleteNode(root, 15);
inorder(root);
printf("\n");
printf("\nDeleting 17:\n");
root = deleteNode(root, 17);
By Prof. Amogha A R
inorder(root);
printf("\n");
printf("\nDeleting 41:\n");
root = deleteNode(root, 41);
inorder(root);
printf("\n");
return 0;
}
By Prof. Amogha A R
14. Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and
perform inorder, preorder and post order traversal.
#include <stdio.h>
#include <stdlib.h>
By Prof. Amogha A R
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
return root;
}
// Main Function
int main() {
struct Node* root = NULL;
By Prof. Amogha A R
int elements[] = {2, 5, 1, 3, 9, 0, 6};
int n = sizeof(elements) / sizeof(elements[0]);
// Perform Traversals
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
return 0;
}
By Prof. Amogha A R
15. Write a program to Sort the following elements using heap sort {9.16,32,8,4,1,5,8,0}.
#include <stdio.h>
// Main function
int main() {
float arr[] = {9, 16, 32, 8, 4, 1, 5, 8, 0}; // Given elements
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
By Prof. Amogha A R
16. Given S1={“Flowers”} ; S2={“are beautiful”} I. Find the length of S1 II. Concatenate S1 and
S2 III. Extract the substring “low” from S1 IV. Find “are” in S2 and replace it with “is” .
#include <stdio.h>
#include <string.h>
int main() {
char S1[20] = "Flowers"; // Declaring S1
char S2[20] = "are beautiful"; // Declaring S2
char result[40]; // To store concatenated string
char substring[4]; // To store extracted substring
By Prof. Amogha A R
// II. Concatenate S1 and S2
strcpy(result, S1); // Copy S1 to result
strcat(result, " "); // Adding space between words
strcat(result, S2); // Append S2
printf("Concatenated String: %s\n", result);
return 0;
}
By Prof. Amogha A R
17. Write a program to implement adjacency matrix of a graph.
#include <stdio.h>
int main() {
int matrix[MAX][MAX] = {0}; // Initialize matrix with 0s
int vertices, edges, src, dest, isDirected;
if (!isDirected) {
matrix[dest][src] = 1; // For undirected graphs
}
}
return 0;
}
By Prof. Amogha A R
By Prof. Amogha A R
18. Write a program to insert/retrieve an entry into hash/ from a hash table with open
addressing using linear probing.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int key;
int value;
} HashEntry;
// Hash function
By Prof. Amogha A R
int hashFunction(int key) {
return key % TABLE_SIZE;
}
// Linear probing
while (hashTable[index].key != EMPTY && hashTable[index].key !=
key) {
index = (index + 1) % TABLE_SIZE;
if (index == originalIndex) {
printf("Hash table is full!\n");
return;
}
}
hashTable[index].key = key;
hashTable[index].value = value;
printf("Inserted (%d, %d) at index %d\n", key, value, index);
}
int main() {
initializeTable();
insert(12, 100);
insert(22, 200);
insert(32, 300);
insert(42, 400);
displayTable();
return 0;
}
By Prof. Amogha A R
Example Run
Output:
Inserted (12, 100) at index 2
Inserted (22, 200) at index 3
Inserted (32, 300) at index 4
Inserted (42, 400) at index 5
Hash Table:
Index 0: Empty
Index 1: Empty
Index 2: (12, 100)
Index 3: (22, 200)
Index 4: (32, 300)
Index 5: (42, 400)
Index 6: Empty
Index 7: Empty
Index 8: Empty
Index 9: Empty
By Prof. Amogha A R