Docsity

DSA LAB ASSIGNMENT ., Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
Assignment 4
Roll no. 25a04010
2.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head = NULL;
// Check if number is prime
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i*i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
//Check if number is Fibonacci
int isFibonacci(int n) {
if (n == 0 || n == 1) return 1; // 0 and 1 are Fibonacci
int a = 0, b = 1, c;
while (b < n) {
c = a + b;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download DSA LAB ASSIGNMENT . and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Assignment 4 Roll no. 25a

#include #include struct Node { int data; struct Node* next; }; struct Node* head = NULL; // Check if number is prime int isPrime(int n) { if (n <= 1) return 0; for (int i = 2; i*i <= n; i++) { if (n % i == 0) return 0; } return 1; } //Check if number is Fibonacci int isFibonacci(int n) { if (n == 0 || n == 1) return 1; // 0 and 1 are Fibonacci int a = 0, b = 1, c; while (b < n) { c = a + b;

a = b; b = c; } return (b == n); // true if no. is fibonacci }

// a. Insert element at end

void insert(int value) { struct Node* newNode = (struct Node)malloc(sizeof(struct Node)); newNode->data = value; if (head == NULL) { head = newNode; head->next = head; // circular link } else { struct Node temp = head; while (temp->next != head) temp = temp->next; temp->next = newNode; newNode->next = head; } }

// b. Print elements

void printList() { if (head == NULL) { printf("List is empty\n"); return; }

// e. Count nodes > 15

int countGreaterThan15() { if (head == NULL) return 0; int count = 0; struct Node* temp = head; do { if (temp->data > 15) count++; temp = temp->next; } while (temp != head); return count; }

// f. Delete a particular element

void deleteElement(int value) { if (head == NULL) return; struct Node* curr = head, prev = NULL; do { if (curr->data == value) { if (curr == head) { // deleting head struct Node last = head; while (last->next != head) last = last->next; if (head->next == head) { free(head); head = NULL; return;

last->next = head->next; struct Node* temp = head; head = head->next; free(temp); } else { prev->next = curr->next; free(curr); } return; } prev = curr; curr = curr->next; } while (curr != head); }

// g. Update value of a particular element

void updateElement(int oldVal, int newVal) { if (head == NULL) return; struct Node* temp = head; do { if (temp->data == oldVal) { temp->data = newVal; return; } temp = temp->next; } while (temp != head); }

// i. Delete all nodes with prime values

void deletePrimes() { if (head == NULL) return; struct Node* temp = head; int n = countNodes(); for (int i = 0; i < n; i++) { int val = temp->data; temp = temp->next; if (isPrime(val)) deleteElement(val); } }

// j. Delete all nodes with Fibonacci values

void deleteFibonacci() { if (head == NULL) return; struct Node* temp = head; int n = countNodes(); for (int i = 0; i < n; i++) { int val = temp->data; temp = temp->next; if (isFibonacci(val)) deleteElement(val); } } int main() { insert(10); insert(20); insert(5);

insert(17); insert(8); printf("List: "); printList(); printf("Count: %d\n", countNodes()); printf("Has Negative? %s\n", hasNegative()? "Yes" : "No"); printf("Nodes > 15: %d\n", countGreaterThan15()); updateElement(20, 25); printf("After update: "); printList(); insertAtPosition(3, 99); printf("After inserting 99 at pos 3: "); printList(); deleteElement(5); printf("After deleting 5: "); printList(); deletePrimes(); printf("After deleting primes: "); printList(); deleteFibonacci(); printf("After deleting Fibonacci values: "); printList();

struct Node* next; }; struct Node* head = NULL; // a. Insert element at end void insert(int value) { struct Node* newNode = (struct Node)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; newNode->prev = NULL; if (head == NULL) { head = newNode; } else { struct Node temp = head; while (temp->next != NULL) temp = temp->next; temp->next = newNode; newNode->prev = temp; } } void printList() { struct Node* temp = head; printf("List: "); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next;

printf("\n"); } // b. Traverse and check divisibility by m void checkDivisible(int m) { struct Node* temp = head; while (temp != NULL) { if (temp->data % m == 0) printf("%d is divisible by %d\n", temp->data, m); temp = temp->next; } } // c. Delete nodes greater than x void deleteGreaterThan(int x) { struct Node* temp = head; while (temp != NULL) { if (temp->data > x) { struct Node* toDelete = temp; if (toDelete->prev != NULL) toDelete->prev->next = toDelete->next; else head = toDelete->next; // deleting head if (toDelete->next != NULL) toDelete->next->prev = toDelete->prev; temp = toDelete->next;

for (int i = 0; i < n; i++) insert(arr[i]); printList(); printf("\nChecking divisibility by 5:\n"); checkDivisible(5); printf("\nDeleting nodes greater than 50...\n"); deleteGreaterThan(50); printList(); int dupVal = 3; int result = elementsBetweenDuplicate(dupVal); if (result != -1) printf("\nNo. of elements between first pair of '%d' = %d\n", dupVal, result); else printf("\nNo duplicate '%d' found\n", dupVal); return 0; }

#include #include struct Node {

if (temp == NULL) { printf("Invalid position\n"); free(newNode); return; } newNode->next = temp->next; temp->next = newNode; } // c.Split number into digits and store in linked list void storeDigits(int num) { if (num == 0) { insertAtBeginning(0); return; } int digits[20], count = 0; while (num > 0) { digits[count++] = num % 10; num /= 10; } for (int i = count-1; i >= 0; i--) { insertAtPosition(count-i, digits[i]); } }

// Print linked list void printList() { struct Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { insertAtBeginning(5); insertAtBeginning(10); insertAtPosition(2, 99); printf("List after insertions: "); printList(); int num = 12345; storeDigits(num); printf("List after storing digits of %d: ", num); printList(); return 0; }

struct Node* head = NULL; void insertChar(char c) { struct Node* newNode = (struct Node)malloc(sizeof(struct Node)); newNode->data = c; newNode->next = NULL; if (!head) head = newNode; else { struct Node temp = head; while (temp->next) temp = temp->next; temp->next = newNode; } } void removeVowels() { struct Node* temp = head, *prev = NULL; while (temp) { char c = tolower(temp->data); if (c=='a'||c=='e'||c=='i'||c=='o'||c=='u') { if (prev) prev->next = temp->next; else head = temp->next; free(temp); temp = (prev?prev->next:head); } else { prev = temp; temp = temp->next; } }

void printList() { struct Node* temp = head; while (temp) { printf("%c ", temp->data); temp = temp->next; } printf("\n"); } int main() { char name[50]; printf("Enter name: "); scanf("%s", name); for (int i=0; name[i]; i++) insertChar(name[i]); printf("Original list: "); printList(); removeVowels(); printf("After removing vowels: "); printList(); return 0; }