Q1
#include <stdio.h>
#include <stdlib.h>
// Structure for linked list node
Struct Node {
Int data;
Struct Node* next;
};
// Function to insert a node at the end of the linked list
Void insertNode(struct Node** head, int data) {
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
Struct Node* temp = *head;
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
While (temp->next != NULL) {
Temp = temp->next;
}
Temp->next = newNode;
}
}
// Function to print a linked list
Void printList(struct Node* head) {
Struct Node* temp = head;
While (temp != NULL) {
Printf(“%d “, temp->data);
Temp = temp->next;
}
Printf(“\n”);
}
// Function to split linked list into two lists: odd and even nodes
Void splitList(struct Node* head, struct Node** oddList, struct Node** evenList) {
Struct Node* odd = NULL;
Struct Node* even = NULL;
Struct Node* current = head;
Int index = 1;
While (current != NULL) {
If (index % 2 == 1) { // Odd position
insertNode(oddList, current->data);
} else { // Even position
insertNode(evenList, current->data);
}
Current = current->next;
Index++;
}
}
Int main() {
Struct Node* originalList = NULL;
Struct Node* oddList = NULL;
Struct Node* evenList = NULL;
Int choice, value;
// Taking input to create the original list
While (1) {
Printf(“\nPress:\n1 to insert node\n2 to exit\n”);
Scanf(“%d”, &choice);
If (choice == 1) {
Printf(“Enter value: “);
Scanf(“%d”, &value);
insertNode(&originalList, value);
} else if (choice == 2) {
Break;
} else {
Printf(“Invalid choice!\n”);
}
}
// Output the original list
Printf(“Original List: “);
printList(originalList);
// Splitting the list into odd and even lists
splitList(originalList, &oddList, &evenList);
// Output the odd and even lists
Printf(“Odd numbered nodes list: “);
printList(oddList);
printf(“Even numbered nodes list: “);
printList(evenList);
return 0;
}
Q2
#include <stdio.h>
#include <stdlib.h>
// Structure for linked list node
Struct Node {
Int data;
Struct Node* next;
};
// Function to insert a node at the end of the linked list
Void insertNode(struct Node** head, int data) {
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
Struct Node* temp = *head;
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
While (temp->next != NULL) {
Temp = temp->next;
}
Temp->next = newNode;
}
}
// Function to print a linked list
Void printList(struct Node* head) {
Struct Node* temp = head;
While (temp != NULL) {
Printf(“%d “, temp->data);
Temp = temp->next;
}
Printf(“\n”);
}
// Function to find the value at n’th node from the end of the linked list
Int findNthFromEnd(struct Node* head, int n) {
Struct Node* first = head;
Struct Node* second = head;
Int count = 0;
// Move the first pointer n nodes ahead
While (count < n) {
If (first == NULL) {
Printf(“The list is shorter than %d nodes.\n”, n);
Return -1; // Error case: n is larger than the list size
}
First = first->next;
Count++;
}
// Move both pointers until the first pointer reaches the end
While (first != NULL) {
First = first->next;
Second = second->next;
}
// The second pointer is now at the n’th node from the end
Return second->data;
}
Int main() {
Struct Node* head = NULL;
Int choice, value, n;
// Taking input to create the original list
While (1) {
Printf(“\nPress:\n1 to insert node\n2 to exit\n”);
Scanf(“%d”, &choice);
If (choice == 1) {
Printf(“Enter value: “);
Scanf(“%d”, &value);
insertNode(&head, value);
} else if (choice == 2) {
Break;
} else {
Printf(“Invalid choice!\n”);
}
}
// Output the original list
Printf(“Linked List: “);
printList(head);
// Taking input for n (position from the end)
Printf(“Enter value n: “);
Scanf(“%d”, &n);
// Find the value at the n’th node from the end
Int result = findNthFromEnd(head, n);
If (result != -1) {
Printf(“Value at position %d from end: %d\n”, n, result);
}
Return 0;
}
Q3
#include <stdio.h>
#include <stdlib.h>
// Structure for linked list node
Struct Node {
Int data;
Struct Node* next;
};
// Function to insert a node at the end of the linked list
Void insertNode(struct Node** head, int data) {
Struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
Struct Node* temp = *head;
newNode->data = data;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
While (temp->next != NULL) {
Temp = temp->next;
}
Temp->next = newNode;
}
}
// Function to print a linked list
Void printList(struct Node* head) {
Struct Node* temp = head;
While (temp != NULL) {
Printf(“%d “, temp->data);
Temp = temp->next;
}
Printf(“\n”);
}
// Function to reverse a linked list
Void reverseList(struct Node** head) {
Struct Node* prev = NULL;
Struct Node* current = *head;
Struct Node* next = NULL;
While (current != NULL) {
// Store the next node
Next = current->next;
// Reverse the current node’s pointer
Current->next = prev;
// Move pointers one position ahead
Prev = current;
Current = next;
}
// Update the head pointer to the new head (last node of original list)
*head = prev;
}
Int main() {
Struct Node* head = NULL;
Int choice, value;
// Taking input to create the original list
While (1) {
Printf(“\nPress:\n1 to insert node\n2 to exit\n”);
Scanf(“%d”, &choice);
If (choice == 1) {
Printf(“Enter value: “);
Scanf(“%d”, &value);
insertNode(&head, value);
} else if (choice == 2) {
Break;
} else {
Printf(“Invalid choice!\n”);
}
}
// Output the original list
Printf(“Original Linked List: “);
printList(head);
// Reverse the linked list
reverseList(&head);
// Output the reversed list
Printf(“Reversed Linked List: “);
printList(head);
return 0;
}