DATA STRUCTURE LAB
DATA STRUCTURE LAB
1. Write a program to search for an element in an array using binary and linear search.
#include <stdio.h>
if (arr[mid] == target) {
return mid; // Return the index if found
} else if (arr[mid] < target) {
low = mid + 1; // Search in the right half
} else {
high = mid - 1; // Search in the left half
}
}
return -1; // Return -1 if not found
}
int main() {
int search_list[] = {3, 1, 9, 8, 7, 12, 56, 23, 89};
int length = sizeof(search_list) / sizeof(search_list[0]);
int target,i,j;
int linearResult, binaryResult;
// Linear Search
printf("Enter a value to search using Linear Search: ");
scanf("%d", &target);
// Binary Search
printf("Enter a value to search using Binary Search: ");
scanf("%d", &target);
if (binaryResult != -1) {
printf("Binary Search: Element found at index %d\n", binaryResult);
} else {
printf("Binary Search: Element not found\n");
}
return 0;
}
#include <stdio.h>
int main() {
int n,i;
int arr[100];
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.
#include <stdio.h>
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.
#include <stdio.h>
#include <stdlib.h>
temp = *head;
while (temp->next != NULL) {
temp = temp->next; // Traverse to the last node
}
temp->next = newNode; // Link the new node at the end
}
// Search for the key to be deleted, keep track of the previous node
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
int main() {
struct Node* head = NULL;
return 0;
}
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.
#include <stdio.h>
#include <stdlib.h>
if (q->front == -1) {
q->front = 0; // Set front to 0 if it's the first element
}
q->rear++;
q->items[q->rear] = value; // Insert the element
}
printf("Queue: ");
for (i = q->front; i <= q->rear; i++) {
printf("%d ", q->items[i]);
}
printf("\n");
}
int main() {
struct Queue q;
int elements[] = {45, 34, 10, 63, 3};
int i;
createQueue(&q);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
while (1) {
printf("%d ", q->items[i]);
if (i == q->rear) break; // Stop when we reach the rear
i = (i + 1) % MAX_SIZE; // Move to next index in a circular manner
}
printf("\n");
}
int main() {
struct CircularQueue q;
int elements[] = {45, 34, 10, 63, 3};
int i;
createQueue(&q);
return 0;
}
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.
#include <stdio.h>
#include <stdlib.h>
// Search for the key to be deleted, keep track of the previous node
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
printf("List: ");
while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
int i;
for ( i = 0; i < 4; i++) {
insertOrdered(&head, elements[i]);
printf("After inserting %d: ", elements[i]);
printList(head);
}
return 0;
}
8. Write a program for Tower of Honoi problem using recursion.
#include <stdio.h>
// Move n-1 disks from source to auxiliary, using target as a temporary rod
towerOfHanoi(n - 1, source, auxiliary, target);
// Move the n-1 disks from auxiliary to target, using source as a temporary rod
towerOfHanoi(n - 1, auxiliary, target, source);
}
int main() {
int n = 4; // Number of disks
printf("Solution for Tower of Hanoi with %d disks:\n", n);
towerOfHanoi(n, 'A', 'C', 'B'); // A is the source rod, C is the target rod, B is the
auxiliary rod
return 0;
}
#include <stdio.h>
int main() {
int num1, num2, num3,result;
// Taking input from the user
printf("Enter three positive integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Calculating GCD
result = gcdOfThree(num1, num2, num3);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* stack = NULL; // Initialize an empty stack
return 0;
}
11. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
struct Stack {
char items[MAX];
int top;
};
j = 0; // Postfix index
int main() {
const char* infix = "x^y/(5*z)+2";
char postfix[MAX];
infixToPostfix(infix, postfix);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Stack structure
int stack[MAX];
int top = -1;
switch (token) {
case '+':
push(operand1 + operand2);
break;
case '-':
push(operand1 - operand2);
break;
case '*':
push(operand1 * operand2);
break;
case '/':
push(operand1 / operand2);
break;
default:
printf("Invalid operator: %c\n", token);
exit(1);
}
}
}
return pop(); // The result will be on top of the stack
}
int main() {
char postfix[] = "5 3 + 8 2 - *"; // Postfix expression
int result = evaluatePostfix(postfix); // Evaluate the expression
printf("The result of the postfix expression '%s' is: %d\n", postfix, result);
return 0;
}
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>
return root;
}
// Node with two children: Get the inorder successor (smallest in the right
subtree)
temp = findMin(root->right);
root->data = temp->data; // Copy the inorder successor's content to this node
root->right = deleteNode(root->right, temp->data); // Delete the inorder
successor
}
return root;
}
return 0;
}
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>
return root;
}
// Display traversals
printf("In-order Traversal: ");
inorder(root);
printf("\n");
return 0;
}
15. Write a program to Sort the following elements using heap sort {9.16,32,8,4,1,5,8,0}.
#include <stdio.h>
int main() {
float arr[] = {9.16, 32, 8, 4, 1, 5, 8, 0};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
return 0;
}
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() {
// Initialize the strings
char S1[] = "Flowers";
char S2[] = "are beautiful";
char result[100]; // To store concatenated string
char *substr; // To store extracted substring
char *pos;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Input edges
for ( i = 0; i < max_edges; i++) {
printf("Enter edge %d (or -1 -1 to stop): ", i + 1);
scanf("%d %d", &origin, &destin);
int main() {
createGraph(); // Create the graph
displayGraph(); // Display the adjacency matrix
return 0;
}
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>
int main() {
HashEntry* hashTable = (HashEntry*)malloc(TABLE_SIZE * sizeof(HashEntry));
int searchKey = 32;
int index;
if (index != -1) {
printf("Key %d found at index %d\n", searchKey, index);
} else {
printf("Key %d not found in the hash table\n", searchKey);
}
************************************