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

Selected Data Structure Programs

The document contains a series of programming tasks that demonstrate various data structures and algorithms in C, including searching algorithms (linear and binary), sorting algorithms (bubble sort, insertion sort), linked lists, queues (linear and circular), the Tower of Hanoi problem, GCD calculation, and stack implementation using linked lists. Each task includes code snippets and expected outputs. The programs cover fundamental concepts in data structures and algorithms, providing practical examples for learning.

Uploaded by

dhanushchitra579
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Selected Data Structure Programs

The document contains a series of programming tasks that demonstrate various data structures and algorithms in C, including searching algorithms (linear and binary), sorting algorithms (bubble sort, insertion sort), linked lists, queues (linear and circular), the Tower of Hanoi problem, GCD calculation, and stack implementation using linked lists. Each task includes code snippets and expected outputs. The programs cover fundamental concepts in data structures and algorithms, providing practical examples for learning.

Uploaded by

dhanushchitra579
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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 linear_search(int arr[], int n, int target) {


for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Element found, return index
}
}
return -1; // Element not found
}

int binary_search(int arr[], int n, int target) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}

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;

int linear_result = linear_search(arr, n, target_linear);


printf("Linear Search Result (Target %d): %d\n", target_linear, linear_result);

int binary_result = binary_search(arr, n, target_binary);


printf("Binary Search Result (Target %d): %d\n", target_binary, binary_result);

return 0;
}
Output:
Linear Search Result (Target 12): 3
Binary Search Result (Target 16): 4

2. Write a program to sort list of n numbers using Bubble Sort algorithms.

#include <stdio.h>

void bubble_sort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

bubble_sort(arr, n);

printf("Sorted array using Bubble Sort: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\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);

printf("Sorted array (Descending) using Insertion Sort: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Output:
Sorted array (Descending) using Insertion Sort: 75 48 16 8 7 3 1 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>

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

void insert(struct Node** head, int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}

void delete(struct Node** head, int key) {


struct Node* temp = *head, *prev = NULL;
if (temp != NULL && temp->data == key) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = 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;
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

int queue[SIZE], front = -1, rear = -1;

void enqueue(int value) {


if (rear == SIZE - 1) printf("Queue is full!\n");
else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Enqueued: %d\n", value);
}
}

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

6. Write a program to simulate the working of Circular queue using


array.

#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 enqueue(int value) {


if (isFull()) {
printf("Queue is Full!\n");
} else {
if (front == -1) front = 0; // First element
rear = (rear + 1) % SIZE;
queue[rear] = value;
printf("Enqueued: %d\n", value);
}
}

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

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>

struct Node {

int data;

struct Node* next;

};

void insert(struct Node** head, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

if (*head == NULL || (*head)->data >= data) {

newNode->next = *head;

*head = newNode;
} else {

struct Node* temp = *head;

while (temp->next != NULL && temp->next->data < data)

temp = temp->next;

newNode->next = temp->next;

temp->next = newNode;

void delete(struct Node** head, int data) {

struct Node* temp = *head, *prev = NULL;

if (temp != NULL && temp->data == data) {

*head = temp->next;

free(temp);

return;

while (temp != NULL && temp->data != data) {

prev = temp;

temp = temp->next;

if (temp == NULL) return;

prev->next = temp->next;
free(temp);

void printList(struct Node* head) {

while (head != NULL) {

printf("%d -> ", head->data);

head = head->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

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;

8. Write a program for Tower of Hanoi problem using recursion.

#include <stdio.h>

void towerOfHanoi(int n, char from, char to, char aux) {

if (n == 1) {

printf("Move disk 1 from %c to %c\n", from, to);

return;

towerOfHanoi(n-1, from, aux, to);

printf("Move disk %d from %c to %c\n", n, from, to);

towerOfHanoi(n-1, aux, to, from);

int main() {

int n = 3;

towerOfHanoi(n, 'A', 'C', 'B');

return 0;

}
9. Write recursive program to find GCD of 3 numbers.

#include <stdio.h>

int gcd(int a, int b) {


if (b == 0)
return a;
return gcd(b, a % b);
}

int gcd_of_three(int a, int b, int c) {


return gcd(gcd(a, b), c);
}

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

10. Write a program to demonstrate working of stack using linked list.

#include <stdio.h>
#include <stdlib.h>

// Define the node structure


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

// Function to push an element onto the stack


void push(struct Node** top, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = *top;
*top = newNode;
printf("%d pushed to stack\n", value);
}

// Function to pop an element from the stack


int pop(struct Node** top) {
if (*top == NULL) {
printf("Stack Underflow\n");
return -1;
}
struct Node* temp = *top;
int poppedValue = temp->data;
*top = (*top)->next;
free(temp);
return poppedValue;
}

// Function to display the stack


void display(struct Node* top) {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
printf("Stack elements are: ");
while (top != NULL) {
printf("%d ", top->data);
top = top->next;
}
printf("\n");
}

// Main function
int main() {
struct Node* stack = NULL;

push(&stack, 10);
push(&stack, 20);
push(&stack, 30);

display(stack);

printf("Popped element is: %d\n", pop(&stack));


display(stack);

return 0;
}

You might also like