0% found this document useful (0 votes)
16 views11 pages

ChartGPT - DATA STRUCTURES LA1

Am not sur why you criminals are changing ths goal post
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

ChartGPT - DATA STRUCTURES LA1

Am not sur why you criminals are changing ths goal post
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

DATA STRUCTURES LAB

1. Use a recursive function to find the GCD of two numbers.

#include <stdio.h>

int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
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;
}

2. Use a recursive function to find the Fibonacci series.


#include <stdio.h>

int stringLength(char *str) {


int length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}

void stringConcat(char *str1, char *str2) {


while (*str1)
str1++;
while ((*str1++ = *str2++));
}

int main() {
char str1[100], str2[100];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);

printf("Length of first string: %d\n", stringLength(str1));


stringConcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
3. Use pointers to find the length of a string and to concatenate two strings.
#include <stdio.h>

int stringLength(char *str) {


int length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}

void stringConcat(char *str1, char *str2) {


while (*str1)
str1++;
while ((*str1++ = *str2++));
}

int main() {
char str1[100], str2[100];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);

printf("Length of first string: %d\n", stringLength(str1));


stringConcat(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>

void stringCopy(char *source, char *dest) {


while ((*dest++ = *source++));
}

void substring(char *source, int start, int length, char *result) {


int i;
for (i = 0; i < length && *(source + start + i) != '\0'; i++) {
result[i] = *(source + start + i);
}
result[i] = '\0';
}

int main() {
char str[100], copy[100], sub[100];
int start, len;

printf("Enter a string: ");


gets(str);

stringCopy(str, copy);
printf("Copied string: %s\n", copy);

printf("Enter starting index and length for substring: ");


scanf("%d %d", &start, &len);
substring(str, start, len, sub);
printf("Extracted substring: %s\n", sub);

return 0;
}

5. Write a C program to find the maximum and minimum elements in an array.

#include <stdio.h>

int main() {
int n, i, max, min, arr[100];

printf("Enter the number of elements: ");


scanf("%d", &n);

printf("Enter the elements of the array: ");


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 element: %d\n", max);


printf("Minimum element: %d\n", min);

return 0;
}

6. Write a C program to delete an integer from an array.

#include <stdio.h>

int main() {
int n, i, pos, arr[100];

printf("Enter the number of elements: ");


scanf("%d", &n);

printf("Enter the elements of the array: ");


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the position to delete (0 to %d): ", n - 1);
scanf("%d", &pos);

if (pos < 0 || pos >= n) {


printf("Invalid position!\n");
} else {
for (i = pos; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
n--;

printf("Array after deletion: ");


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

return 0;
}

7. Write a program to create a linked list and display it.

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

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

void display(struct Node* head) {


struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node *head = NULL, *temp, *newNode;
int n, i, data;

printf("Enter the number of nodes: ");


scanf("%d", &n);

for (i = 0; i < n; i++) {


newNode = (struct Node*)malloc(sizeof(struct Node));
printf("Enter data for node %d: ", i + 1);
scanf("%d", &newNode->data);
newNode->next = NULL;

if (head == NULL)
head = newNode;
else {
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}

display(head);
return 0;
}

8. Write a program to sort N numbers using insertion sort.


#include <stdio.h>

void insertionSort(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 = j - 1;
}
arr[j + 1] = key;
}
}

int main() {
int n, i, arr[100];

printf("Enter the number of elements: ");


scanf("%d", &n);

printf("Enter the elements: ");


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

insertionSort(arr, n);

printf("Sorted array: ");


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

return 0;
}

9. Write a program to sort N numbers using selection sort.

#include <stdio.h>

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


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

int main() {
int n, i, arr[100];

printf("Enter the number of elements: ");


scanf("%d", &n);

printf("Enter the elements: ");


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

selectionSort(arr, n);

printf("Sorted array: ");


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

return 0;
}

10. Write a program to Insert a node into a singly linked list.


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

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

void insertAtBeginning(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 != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;
int data;

printf("Enter data to insert at the beginning: ");


scanf("%d", &data);
insertAtBeginning(&head, data);
display(head);

return 0;
}

11 Write a program to delete a node from a singly linked list.

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

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

void deleteNode(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 != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node *head = NULL, *temp, *newNode;
int data, n, key;

printf("Enter the number of nodes: ");


scanf("%d", &n);

for (int i = 0; i < n; i++) {


newNode = (struct Node*)malloc(sizeof(struct Node));
printf("Enter data for node %d: ", i + 1);
scanf("%d", &newNode->data);
newNode->next = head;
head = newNode;
}

printf("Original List: ");


display(head);

printf("Enter node data to delete: ");


scanf("%d", &key);
deleteNode(&head, key);

printf("Updated List: ");


display(head);

return 0;
}

12. Write a program to implement stack operations using a pointer.


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

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

void push(struct Stack** top, int data) {


struct Stack* newNode = (struct Stack*)malloc(sizeof(struct Stack));
newNode->data = data;
newNode->next = *top;
*top = newNode;
}

int pop(struct Stack** top) {


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

void display(struct Stack* top) {


struct Stack* temp = top;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Stack* top = NULL;
push(&top, 10);
push(&top, 20);
push(&top, 30);
printf("Stack: ");
display(top);
printf("Popped element: %d\n", pop(&top));
printf("Stack after pop: ");
display(top);

return 0;
}

13 Write a program to search an element in an array using linear search.


#include <stdio.h>

int linearSearch(int arr[], int n, int x) {


for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}

int main() {
int n, arr[100], x, result;

printf("Enter the number of elements: ");


scanf("%d", &n);

printf("Enter the elements: ");


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

printf("Enter the element to search: ");


scanf("%d", &x);

result = linearSearch(arr, n, x);


if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");

return 0;
}

14. Write a program to implement queue operation.


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

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

struct Queue* front = NULL;


struct Queue* rear = NULL;

void enqueue(int data) {


struct Queue* newNode = (struct Queue*)malloc(sizeof(struct Queue));
newNode->data = data;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
return;
}
rear->next = newNode;
rear = newNode;
}

int dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return -1;
}
struct Queue* temp = front;
front = front->next;
if (front == NULL)
rear = NULL;
int data = temp->data;
free(temp);
return data;
}

void display() {
struct Queue* temp = front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);

printf("Queue: ");
display();

printf("Dequeued element: %d\n", dequeue());


printf("Queue after dequeue: ");
display();

return 0;
}

15. Write a C program to swap two numbers using pointers.


#include <stdio.h>

void swap(int *x, int *y) {


int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Before swap: a = %d, b = %d\n", a, b);


swap(&a, &b);
printf("After swap: a = %d, b = %d\n", a, b);

return 0;
}

You might also like