1 Linked List
1 Linked List
#include <stdio.h>
#include <stdlib.h>
} Node;
// Func on to insert a new node at the end of the linked list void insertNode(int value) {
return;
last->next = newNode;
*temp = head;
// If the head node itself holds the value if (temp != NULL && temp->data ==
}
// Search for the value to be deleted while (temp != NULL && temp->data != value)
// If the value was not found if (temp == NULL) { prin ("Value not found in
// Unlink the node from the linked list prev->next = temp->next; free(temp);
temp->next;
prin ("NULL\n");
head = prev;
}
// Func on to search for a node by value and return its posi on int searchNode(int value) { Node
*temp = head;
int index = 0;
index;
// Main func on with menu-driven interface int main() { int choice, value, posi on;
do {
prin ("\nMenu:\n");
posi on);
} else {
} break; case 6:
break; default:
return 0;
Output:-
Menu:
Menu:
Enter your choice: 3 58 -> 65 -> 98 -> 69 -> 14 -> NULL Menu:
Exi ng...
#include <stdlib.h>
#include <string.h>
char name[MAX_NAME_LENGTH];
} Student;
// Func on to add a new student record to the linked list void addStudent(char name[], int
// Func on to delete a student record with a given roll number void deleteStudent(int rollNumber)
return;
}
// Traverse the list to find the student while (current != NULL && current-
// If the student was not found if (current == NULL) { prin ("Student with roll number %d
return;
prin ("Student Records:\n"); while (current != NULL) { prin ("Name: %s, Roll Number: %d,
>next;
// Func on to search for a student record with a given roll number int searchStudent(int rollNumber) {
return -1;
// Func on to handle the menu op ons and perform opera ons void menu() { int choice,
rollNumber;
char name[MAX_NAME_LENGTH];
prin ("\nMenu:\n"); prin ("1. Add a student record\n"); prin ("2. Delete a student
record\n"); prin ("3. Display all student records\n"); prin ("4. Search for a student
record\n"); prin ("5. Exit\n"); prin ("Enter your choice: "); scanf("%d",
&choice);
scanf(" %[^\n]", name); // Read string with spaces prin ("Enter roll number: ");
break; case 2:
deleteStudent(rollNumber);
break; case 4:
prin ("Student with roll number %d found at posi on %d.\n", rollNumber, posi on);
} else {
} } break; case 5:
exit(0); default:
Output:-
Menu:
Enter marks: 65
Menu:
Student Records:
Name: Karan, Roll Number: 3, Marks: 97.00 Name: Stu e, Roll Number: 26, Marks: 98.00
Menu:
Menu:
Student Records:
Menu:
Enter roll number to search: 26 Student with roll number 26 found at posi on 3.
Menu:
Exi ng...