Selected Data Structure Programs
Selected Data Structure Programs
1. Write a program to search for an element in an array using binary and linear
search.
#include <stdio.h>
int main() {
int arr[] = {2, 5, 8, 12, 16, 20, 24};
int n = sizeof(arr) / sizeof(arr[0]);
int target_linear = 12, target_binary = 16;
return 0;
}
Output:
Linear Search Result (Target 12): 3
Binary Search Result (Target 16): 4
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
return 0;
}
Output:
Sorted array using Bubble Sort: 11 12 22 25 34 64 90
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>
void insertion_sort_desc(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {75, 8, 1, 16, 48, 3, 7, 0};
int n = sizeof(arr) / sizeof(arr[0]);
insertion_sort_desc(arr, n);
return 0;
}
Output:
Sorted array (Descending) using Insertion Sort: 75 48 16 8 7 3 1 0
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
insert(&head, 61);
insert(&head, 16);
insert(&head, 8);
insert(&head, 27);
display(head);
delete(&head, 8);
delete(&head, 61);
delete(&head, 27);
display(head);
return 0;
}
Output:
8 -> 16 -> 27 -> 61 -> NULL
16 -> 27 -> NULL
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>
#define SIZE 5
void dequeue() {
if (front == -1 || front > rear) printf("Queue is empty!\n");
else printf("Dequeued: %d\n", queue[front++]);
}
void display() {
if (front == -1 || front > rear) printf("Queue is empty!\n");
else {
for (int i = front; i <= rear; i++) printf("%d ", queue[i]);
printf("\n");
}
}
int main() {
enqueue(45);
enqueue(34);
enqueue(10);
enqueue(63);
enqueue(3);
display();
dequeue();
dequeue();
dequeue();
display();
return 0;
}
Output:
Enqueued: 45, 34, 10, 63, 3
Dequeued: 45, 34, 10
Queue after deletion: 63, 3
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
int isFull() {
return (rear + 1) % SIZE == front;
}
int isEmpty() {
return front == -1;
}
void dequeue() {
if (isEmpty()) {
printf("Queue is Empty!\n");
} else {
printf("Dequeued: %d\n", queue[front]);
if (front == rear) {
front = rear = -1; // Reset queue after last element
} else {
front = (front + 1) % SIZE;
}
}
}
void display() {
if (isEmpty()) {
printf("Queue is Empty!\n");
} else {
int i = front;
while (i != rear) {
printf("%d ", queue[i]);
i = (i + 1) % SIZE;
}
printf("%d\n", queue[rear]);
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
display();
dequeue();
display();
enqueue(50);
enqueue(60);
display();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
newNode->next = NULL;
newNode->next = *head;
*head = newNode;
} else {
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
free(temp);
head = head->next;
printf("NULL\n");
int main() {
insert(&head, 61);
insert(&head, 16);
insert(&head, 8);
insert(&head, 27);
printList(head);
delete(&head, 8);
delete(&head, 61);
delete(&head, 27);
printList(head);
return 0;
#include <stdio.h>
if (n == 1) {
return;
int main() {
int n = 3;
return 0;
}
9. Write recursive program to find GCD of 3 numbers.
#include <stdio.h>
int main() {
int a = 24, b = 36, c = 60;
printf("GCD of 24, 36, and 60 is: %d\n", gcd_of_three(a, b, c));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Main function
int main() {
struct Node* stack = NULL;
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
display(stack);
return 0;
}