Day16 Assignment
Day16 Assignment
/*create a node in a linked list which will have the following details of
student
1. Name, roll number, class, section, an array having marks of any three subjects
Create a linked list for 5 students and print it.*/
#include <stdio.h>
#include <stdlib.h>
student* createNode();
int main() {
student *first = NULL;
for (int i = 0; i < 5; i++) {
printf("\nEnter student %d details:", i+1);
student *newNode = createNode();
if (first == NULL) {
first = newNode;
} else {
student *temp = first;
while (temp->next!= NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
student *temp = first;
printf("\n--------------------- Student Details ---------------------\n\n");
printf("%-15s %-10s %-8s %-10s %-20s\n", "Name", "Roll No.", "Class",
"Section", "Marks");
printf("------------------------------------------------------------\n");
while (temp != NULL) {
printf("%-15s %-10d %-8d %-10s ", temp->name, temp->rollNumber, temp->class,
temp->section);
for (int i = 0; i < 3; i++) {
printf("%d ", temp->marks[i]);
}
printf("\n");
temp = temp->next;
}
student* createNode() {
student *newNode = (student*)malloc(sizeof(student));
if (!newNode) {
printf("Memory allocation failed!\n");
exit(1);
}
int main() {
Node *head = NULL;
InsertEnd(&head, 6);
InsertEnd(&head, 8);
InsertEnd(&head, 10);
InsertFront(&head, 4);
InsertFront(&head, 0);
InsertMiddle(head, 2, 7); // Inserts 7 after position 2 (1-based indexing)
printList(head);
return 0;
}
if (*ptrHead == NULL) {
*ptrHead = newNode;
} else {
Node *ptrTail = *ptrHead;
while (ptrTail->next != NULL) {
ptrTail = ptrTail->next;
}
ptrTail->next = newNode;
}
}
if (ptrCurrent == NULL) {
printf("Invalid position: List has fewer than %d nodes.\n", after);
free(newNode);
return;
}
newNode->next = ptrCurrent->next;
ptrCurrent->next = newNode;
}
3.
/*Problem 1: Reverse a Linked List
Write a C program to reverse a singly linked list. The program should traverse
the list, reverse the pointers between the nodes, and display the reversed list.
Requirements:
Define a function to reverse the linked list iteratively.
Update the head pointer to the new first node.
Display the reversed list.
Example Input:
rust
Copy code
Initial list: 10 -> 20 -> 30 -> 40
Example Output:
rust
Copy code
Reversed list: 40 -> 30 -> 20 -> 10*/
#include <stdio.h>
#include <stdlib.h>
int main() {
Node *head = NULL;
InsertEnd(&head, 10);
InsertEnd(&head, 20);
InsertEnd(&head, 30);
InsertEnd(&head, 40);
ReverseList(&head);
return 0;
}
// If the linked list is empty, make ptrHead point to the new node created
if (*ptrHead == NULL) {
*ptrHead = newNode;
} else {
// Traverse till the last node and insert the new node at the end
Node *ptrTail = *ptrHead; // Start at the head
while (ptrTail->next != NULL) { // Traverse to the last node
ptrTail = ptrTail->next;
}
ptrTail->next = newNode; // Insert the new node at the end
}
}
4.
/*Problem 2: Find the Middle Node
Write a C program to find and display the middle node of a singly linked list. If
the list has an even number of nodes, display the first middle node.
Requirements:
Use two pointers: one moving one step and the other moving two steps.
When the faster pointer reaches the end, the slower pointer will point to the
middle node.
Example Input:
rust
Copy code
List: 10 -> 20 -> 30 -> 40 -> 50
Example Output:
scss
Copy code
Middle node: 30*/
#include <stdio.h>
#include <stdlib.h>
int main() {
Node *head = NULL;
InsertEnd(&head, 10);
InsertEnd(&head, 20);
InsertEnd(&head, 30);
InsertEnd(&head, 40);
InsertEnd(&head, 50);
printf("List: ");
printList(head);
findMiddle(&head);
return 0;
}
// If the linked list is empty, make ptrHead point to the new node created
if (*ptrHead == NULL) {
*ptrHead = newNode;
} else {
// Traverse till the last node and insert the new node at the end
Node *ptrTail = *ptrHead; // Start at the head
while (ptrTail->next != NULL) { // Traverse to the last node
ptrTail = ptrTail->next;
}
ptrTail->next = newNode; // Insert the new node at the end
}
}
5.
/*Problem 3: Detect and Remove a Cycle in a Linked List
Write a C program to detect if a cycle (loop) exists in a singly linked list and
remove it if present. Use Floyd’s Cycle Detection Algorithm (slow and fast
pointers) to detect the cycle.
Requirements:
Detect the cycle in the list.
If a cycle exists, find the starting node of the cycle and break the loop.
Display the updated list.
Example Input:
rust
Copy code
List: 10 -> 20 -> 30 -> 40 -> 50 -> (points back to 30)
Example Output:
rust
Copy code
Cycle detected and removed.
Updated list: 10 -> 20 -> 30 -> 40 -> 50*/
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct node {
int data;
struct node *next;
} Node;
// Function Prototypes
void InsertEnd(Node **, int);
int findCycle(Node **);
void createCycle(Node **, int);
void printList(Node *);
// Main Function
int main() {
Node *head = NULL;
int n, data, cycleIndex;
// Input elements
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &data);
InsertEnd(&head, data);
}
return 0;
}
if (*ptrHead == NULL) {
*ptrHead = newNode;
} else {
Node *ptrTail = *ptrHead;
while (ptrTail->next != NULL) {
ptrTail = ptrTail->next;
}
ptrTail->next = newNode;
}
}