Here are the solutions for each of the tasks listed in your Data Structures
Lab in C language:
1. Recursive Function to Find GCD
c
Copy
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));
return 0;
}
2. Recursive Function to Find Fibonacci Series
c
Copy
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
3. Pointers to Find Length and Concatenate Strings
c
Copy
#include <stdio.h>
int string_length(char *str) {
int length = 0;
while (*str++) length++;
return length;
}
void concatenate_strings(char *dest, char *src) {
while (*dest) dest++;
while ((*dest++ = *src++));
}
int main() {
char str1[100], str2[50];
printf("Enter first string: ");
fgets(str1, sizeof(str1), stdin);
printf("Enter second string: ");
fgets(str2, sizeof(str2), stdin);
printf("Length of first string: %d\n", string_length(str1));
concatenate_strings(str1, str2);
printf("Concatenated string: %s", str1);
return 0;
}
4. Pointers to Copy a String and Extract a Substring
c
Copy
#include <stdio.h>
#include <string.h>
void copy_string(char *dest, const char *src) {
while ((*dest++ = *src++));
}
void substring(char *dest, const char *src, int start, int length) {
src += start;
while (length-- && (*dest++ = *src++));
*dest = '\0';
}
int main() {
char src[100], dest[100], sub[100];
printf("Enter a string: ");
fgets(src, sizeof(src), stdin);
copy_string(dest, src);
printf("Copied string: %s", dest);
substring(sub, src, 2, 5);
printf("Substring: %s\n", sub);
return 0;
}
5. Find Maximum and Minimum Elements in an Array
c
Copy
#include <stdio.h>
int main() {
int arr[100], n, i;
int max, min;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = min = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
printf("Maximum: %d, Minimum: %d\n", max, min);
return 0;
}
6. Delete an Integer from an Array
c
Copy
#include <stdio.h>
int main() {
int arr[100], n, i, del, pos = -1;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to delete: ");
scanf("%d", &del);
for (i = 0; i < n; i++) {
if (arr[i] == del) {
pos = i;
break;
}
}
if (pos != -1) {
for (i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;
printf("Array after deletion:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
} else {
printf("Element not found.\n");
}
return 0;
}
7. Create and Display a Linked List
c
Copy
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void display(struct Node *head) {
struct Node *temp = head;
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL, *temp, *newNode;
int n, i, value;
printf("Enter number of nodes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
newNode = (struct Node*)malloc(sizeof(struct Node));
printf("Enter value for node %d: ", i + 1);
scanf("%d", &value);
newNode->data = value;
newNode->next = head;
head = newNode;
}
display(head);
return 0;
}
8. Sort N Numbers Using Insertion Sort
c
Copy
#include <stdio.h>
void insertion_sort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
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[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
insertion_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
9. Sort N Numbers Using Selection Sort
c
Copy
#include <stdio.h>
void selection_sort(int arr[], int n) {
int i, j, min_idx, temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
selection_sort(arr, n);
printf("Sorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
10. Insert a Node into a Singly Linked List
c
Copy
#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 display(struct Node *head) {
struct Node *temp = head;
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
int n, value;
printf("Enter number of nodes to insert: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter value for node %d: ", i + 1);
scanf("%d", &value);
insert(&head, value);
}
display(head);
return 0;
}
11. Delete a Node from a Singly Linked List
c
Copy
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void delete_node(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) {
struct Node *temp = head;
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node *head = NULL;
int n, value;
// Insert some nodes first
for (int i = 0; i < 5; i++) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = i + 1;
newNode->next = head;
head = newNode;
}
printf("Initial List: ");
display(head);
printf("Enter value to delete: ");
scanf("%d", &value);
delete_node(&head, value);
printf("Updated List: ");
display(head);
return 0;
}
12. Implement Stack Operations Using a Pointer
c
Copy
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
int capacity;
int *array;
};
struct Stack* create_stack(int capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}
int is_full(struct Stack* stack) {
return stack->top == stack->capacity - 1;
}
int is_empty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int item) {
if (is_full(stack)) return;
stack->array[++stack->top] = item;
}
int pop(struct Stack* stack) {
if (is_empty(stack)) return -1;
return stack->array[stack->top--];
}
void display(struct Stack* stack) {
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->array[i]);
}
printf("\n");
}
int main() {
struct Stack* stack = create_stack(5);
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("Stack elements: ");
display(stack);
printf("Popped element: %d\n", pop(stack));
printf("Stack after pop: ");
display(stack);
return 0;
}
13. Search an Element in an Array Using Linear Search
c
Copy
#include <stdio.h>
int linear_search(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) return i;
}
return -1;
}
int main() {
int arr[100], n, key;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
int result = linear_search(arr, n, key);
if (result != -1) {
printf("Element found at index: %d\n", result);
} else {
printf("Element not found.\n");
}
return 0;
}
14. Implement Queue Operation
c
Copy
#include <stdio.h>
#include <stdlib.h>
struct Queue {
int front, rear, capacity;
int *array;
};
struct Queue* create_queue(int capacity) {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1;
queue->array = (int*)malloc(queue->capacity * sizeof(int));
return queue;
}
int is_full(struct Queue* queue) {
return (queue->size == queue->capacity);
}
int is_empty(struct Queue* queue) {
return (queue->size == 0);
}
void enqueue(struct Queue* queue, int item) {
if (is_full(queue)) return;
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size++;
}
int dequeue(struct Queue* queue) {
if (is_empty(queue)) return -1;
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size--;
return item;
}
void display(struct Queue* queue) {
int i;
for (i = queue->front; i < queue->front + queue->size; i++) {
printf("%d ", queue->array[i % queue->capacity]);
}
printf("\n");
}
int main() {
struct Queue* queue = create_queue(5);
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
printf("Queue elements: ");
display(queue);
printf("Dequeued element: %d\n", dequeue(queue));
printf("Queue after dequeue: ");
display(queue);
return 0;
}
15. Swap Two Numbers Using Pointers
c
Copy
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
These programs cover each of the tasks outlined in your Data Structures
Lab. You can compile and run them using a C compiler. Let me know if you
need any further assistance!