Data Structures Lab
Data Structures Lab
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
return 0;
}
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. Use pointers to find the length of a string and to concatenate two strings.
#include <stdio.h>
int main() {
char str1[100], str2[50];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
concatenateStrings(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
4. Use pointers to copy a string and extract a substring from a given string.
#include <stdio.h>
int main() {
char source[100], copy[100], substring[50];
int start, length;
printf("Enter a string: ");
scanf("%s", source);
copyString(source, copy);
printf("Copied string: %s\n", copy);
return 0;
}
int main() {
int size, min, max;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
int main() {
int size, element;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
struct Node {
int data;
struct Node* next;
};
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original array: ");
printArray(arr, n);
insertionSort(arr, n);
return 0;
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
int choice, data, position;
while (1) {
printf("\n1. Insert at beginning\n2. Insert at end\n3. Insert after a node\n4.
Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtBeginning(&head, data);
break;
case 2:
printf("Enter data to insert: ");
scanf("%d", &data);
insertAtEnd(&head, data);
break;
case 3:
printf("Enter data to insert: ");
scanf("%d", &data);
printf("Enter position after which to insert: ");
scanf("%d", &position);
struct Node* temp = head;
for (int i = 1; i < position && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range\n");
} else {
insertAfter(temp, data);
}
break;
case 4:
displayList(head);
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
struct Node {
int data;
struct Node* next;
};
if (temp == NULL) {
printf("Key not found in the list\n");
return;
}
prev->next = temp->next;
free(temp);
}
int main() {
struct Node* head = NULL;
int n, data, key;
deleteNode(&head, key);
return 0;
struct Stack {
int* array;
int top;
int capacity;
};
while (1) {
printf("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to push: ");
scanf("%d", &item);
push(stack, item);
break;
case 2:
item = pop(stack);
if (item != -1) {
printf("Popped item: %d\n", item);
}
break;
case 3:
item = peek(stack);
if (item != -1) {
printf("Top item: %d\n", item);
}
break;
case 4:
displayStack(stack);
break;
case 5:
free(stack->array);
free(stack);
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
if (result == -1) {
printf("Element %d not found in the array.\n", key);
} else {
printf("Element %d found at index %d.\n", key, result);
}
return 0;
}
struct Queue {
int items[MAX_SIZE];
int front;
int rear;
};
int main() {
struct Queue q;
initQueue(&q);
while (1) {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
value = dequeue(&q);
if (value != -1) {
printf("Dequeued value: %d\n", value);
}
break;
case 3:
displayQueue(&q);
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
int main() {
int num1, num2;
swap(&num1, &num2);
return 0;
}