CA1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

1.

Find the Length of a Linked List

Question: Write a function to find the length of a singly linked list.

Answer:

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

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

int lengthOfList(struct Node* head) {


int length = 0;
struct Node* temp = head;
while (temp != NULL) {
length++;
temp = temp->next;
}
return length;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);

printf("List:\n");
printList(head);
printf("Length of the list: %d\n", lengthOfList(head));

return 0;
}

2. Find the Middle of a Linked List

Question: Write a function to find the middle node of a singly linked list.

Answer:

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

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

struct Node* findMiddle(struct Node* head) {


struct Node* slow = head;
struct Node* fast = head;

while (fast != NULL && fast->next != NULL) {


slow = slow->next;
fast = fast->next->next;
}

return slow;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);

printf("List:\n");
printList(head);

struct Node* middle = findMiddle(head);


printf("Middle node data: %d\n", middle->data);

return 0;
}

3. Delete a Node in a Linked List

Question: Write a function to delete a node at a specific position in a singly linked list.

Answer:

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

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

void deleteNode(struct Node** head_ref, int position) {


if (*head_ref == NULL) return;

struct Node* temp = *head_ref;

if (position == 0) {
*head_ref = temp->next; // Change head
free(temp); // Free old head
return;
}

for (int i = 0; temp != NULL && i < position - 1; i++) {


temp = temp->next;
}

if (temp == NULL || temp->next == NULL) return;

struct Node* next = temp->next->next;


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

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);

printf("Original list:\n");
printList(head);

deleteNode(&head, 2);

printf("List after deleting node at position 2:\n");


printList(head);

return 0;
}

4. Check for a Palindrome Linked List

Question: Write a function to check if a singly linked list is a palindrome.

Answer:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node {
int data;
struct Node* next;
};

bool isPalindrome(struct Node* head) {


struct Node* slow = head;
struct Node* fast = head;
struct Node* prev_of_slow = NULL;
struct Node* second_half;
struct Node* prev = NULL;
struct Node* current;

if (head == NULL || head->next == NULL) return true;

while (fast != NULL && fast->next != NULL) {


fast = fast->next->next;
prev_of_slow = slow;
slow = slow->next;
}

second_half = slow;
prev_of_slow->next = NULL;
prev = NULL;
current = second_half;

while (current != NULL) {


struct Node* next = current->next;
current->next = prev;
prev = current;
current = next;
}

second_half = prev;
struct Node* first_half = head;

while (second_half != NULL) {


if (first_half->data != second_half->data) return false;
first_half = first_half->next;
second_half = second_half->next;
}

return true;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(2);
head->next->next->next->next = createNode(1);

printf("List:\n");
printList(head);

if (isPalindrome(head)) {
printf("The list is a palindrome.\n");
} else {
printf("The list is not a palindrome.\n");
}

return 0;
}

5. Merge Two Sorted Linked Lists

Question: Write a function to merge two sorted linked lists into a single sorted list.

Answer:

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

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

struct Node* mergeSortedLists(struct Node* head1, struct Node* head2) {


struct Node* mergedHead = NULL;
struct Node* mergedTail = NULL;
while (head1 != NULL && head2 != NULL) {
struct Node* temp;

if (head1->data < head2->data) {


temp = head1;
head1 = head1->next;
} else {
temp = head2;
head2 = head2->next;
}

if (mergedHead == NULL) {
mergedHead = temp;
mergedTail = temp;
} else {
mergedTail->next = temp;
mergedTail = temp;
}
}

if (head1 != NULL) mergedTail->next = head1;


if (head2 != NULL) mergedTail->next = head2;

return mergedHead;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head1 = createNode(1);
head1->next = createNode(3);
head1->next->next = createNode(5);

struct Node* head2 = createNode(2);


head2
->next = createNode(4);
head2->next->next = createNode(6);

printf("List 1:\n");
printList(head1);

printf("List 2:\n");
printList(head2);

struct Node* mergedHead = mergeSortedLists(head1, head2);

printf("Merged list:\n");
printList(mergedHead);

return 0;
}

6. Reverse a Linked List

Question: Write a function to reverse a singly linked list.

Answer:

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

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

struct Node* reverseList(struct Node* head) {


struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;

while (current != NULL) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}

head = prev;
return head;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);

printf("Original list:\n");
printList(head);

head = reverseList(head);

printf("Reversed list:\n");
printList(head);

return 0;
}

7. Detect a Cycle in a Linked List

Question: Write a function to detect if there is a cycle in a singly linked list.

Answer:

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

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

bool hasCycle(struct Node* head) {


struct Node* slow = head;
struct Node* fast = head;

while (fast != NULL && fast->next != NULL) {


slow = slow->next;
fast = fast->next->next;

if (slow == fast) {
return true;
}
}

return false;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);

// Creating a cycle for testing


head->next->next->next->next = head->next;

if (hasCycle(head)) {
printf("The list has a cycle.\n");
} else {
printf("The list does not have a cycle.\n");
}

return 0;
}
8. Find the nth Node from the End

Question: Write a function to find the nth node from the end of a singly linked list.

Answer:

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

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

struct Node* findNthFromEnd(struct Node* head, int n) {


struct Node* main_ptr = head;
struct Node* ref_ptr = head;

int count = 0;
if (head != NULL) {
while (count < n) {
if (ref_ptr == NULL) {
printf("%d is greater than the number of nodes in the list.\n", n);
return NULL;
}
ref_ptr = ref_ptr->next;
count++;
}

while (ref_ptr != NULL) {


main_ptr = main_ptr->next;
ref_ptr = ref_ptr->next;
}
}

return main_ptr;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);

printf("List:\n");
printList(head);

int n = 2;
struct Node* nthNode = findNthFromEnd(head, n);
if (nthNode) {
printf("The %dth node from the end is %d\n", n, nthNode->data);
}

return 0;
}

9. Remove Duplicates from a Sorted Linked List

Question: Write a function to remove duplicates from a sorted singly linked list.

Answer:

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

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

void removeDuplicates(struct Node* head) {


struct Node* current = head;
struct Node* next_next;

if (current == NULL) return;

while (current->next != NULL) {


if (current->data == current->next->data) {
next_next = current->next->next;
free(current->next);
current->next = next_next;
} else {
current = current->next;
}
}
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(1);
head->next->next = createNode(2);
head->next->next->next = createNode(3);
head->next->next->next->next = createNode(3);

printf("Original list:\n");
printList(head);

removeDuplicates(head);

printf("List after removing duplicates:\n");


printList(head);

return 0;
}

10. Swap Nodes in a Linked List Without Swapping Data

Question: Write a function to swap nodes in a singly linked list without swapping their data.

Answer:

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

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

void swapNodes(struct Node** head_ref, int x, int y) {


if (x == y) return;

struct Node* prevX = NULL, *currX = *head_ref;


struct Node* prevY = NULL, *currY = *head_ref;

while (currX != NULL && currX->data != x) {


prevX = currX;
currX = currX->next;
}

while (currY != NULL && currY->data != y) {


prevY = currY;
currY = currY->next;
}

if (currX == NULL || currY == NULL) return;

if (prevX != NULL) prevX->next = currY;


else *head_ref = currY;

if (prevY != NULL) prevY->next = currX;


else *head_ref = currX;

struct Node* temp = currY->next;


currY->next = currX->next;
currX->next = temp;
}

// Helper functions to create and print a linked list


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void printList(struct Node* head) {


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

int main() {
struct Node* head = createNode(1);
head->next = createNode(2);
head->next

->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);

printf("Original list:\n");
printList(head);

int x = 2, y = 4;
swapNodes(&head, x, y);

printf("List after swapping %d and %d:\n", x, y);


printList(head);

return 0;
}

These questions and answers should provide a solid understanding of common operations and
techniques for working with pointers and singly linked lists in C.

You might also like