0% found this document useful (0 votes)
11 views

1 Linked List

Uploaded by

karanjaiswalf39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

1 Linked List

Uploaded by

karanjaiswalf39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Input:-

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

// Define the Node structure typedef struct Node {

int data; struct Node *next;

} Node;

// Global head pointer ini alized to NULL

Node *head = NULL;

// Func on to insert a new node at the end of the linked list void insertNode(int value) {

Node *newNode = (Node *)malloc(sizeof(Node));

Node *last = head;

newNode->data = value; newNode->next = NULL;

if (head == NULL) { head = newNode;

return;

while (last->next != NULL) { last = last->next;

last->next = newNode;

// Func on to delete a node by value void deleteNode(int value) { Node

*temp = head;

Node *prev = NULL;

// If the head node itself holds the value if (temp != NULL && temp->data ==

value) { head = temp->next; free(temp); return;

}
// Search for the value to be deleted while (temp != NULL && temp->data != value)

{ prev = temp; temp = temp->next;

// If the value was not found if (temp == NULL) { prin ("Value not found in

the list.\n"); return;

// Unlink the node from the linked list prev->next = temp->next; free(temp);

// Func on to display the linked list void displayList() {

Node *temp = head;

if (temp == NULL) { prin ("List is empty.\n"); return;

while (temp != NULL) { prin ("%d -> ", temp->data); temp =

temp->next;

prin ("NULL\n");

// Func on to reverse the linked list void reverseList() {

Node *prev = NULL;

Node *current = head;

Node *next = NULL;

while (current != NULL) { next = current->next; current-

>next = prev; prev = current; current = next;

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;

while (temp != NULL) { if (temp->data == value) { return

index;

temp = temp->next; index++;

return -1; // Value not found

// Main func on with menu-driven interface int main() { int choice, value, posi on;

do {

prin ("\nMenu:\n");

("1. Insert a node at the end of the


prin list\n");
("2. Delete a node by value\n");
prin
("3. Display the list\n");
prin
("4. Reverse the list\n");
prin
("5. Search for a node\n");
prin
("6. Exit\n");
prin
("Enter your choice: ");
prin
scanf("%d", &choice);

switch (choice) { case 1:

prin ("Enter value to insert: "); scanf("%d", &value);

insertNode(value); break; case 2:

prin ("Enter value to delete: "); scanf("%d", &value);

deleteNode(value); break; case 3: displayList();

break; case 4: reverseList(); break; case 5:


prin ("Enter value to search for: "); scanf("%d", &value); posi on =

searchNode(value); if (posi on != -1) { prin ("Value found at posi on: %d\n",

posi on);

} else {

prin ("Value not found in the list.\n");

} break; case 6:

prin ("Exi ng...\n");

break; default:

prin ("Invalid choice, please try again.\n");

} while (choice != 6);

return 0;

Output:-
Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 1 Enter value to insert: 58 Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 1 Enter value to insert: 65 Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit


Enter your choice: 1

Enter value to insert: 98

Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 1 Enter value to insert: 69 Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 1 Enter value to insert: 14 Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 3 58 -> 65 -> 98 -> 69 -> 14 -> NULL Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 2 Enter value to delete: 69 Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 3 58 -> 65 -> 98 -> 14 -> NULL Menu:

1. Insert a node at the end of the list


2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 4 Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 3 14 -> 98 -> 65 -> 58 -> NULL Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 5

Enter value to search for: 58

Value found at posi on: 3 Menu:

1. Insert a node at the end of the list

2. Delete a node by value

3. Display the list

4. Reverse the list

5. Search for a node 6. Exit

Enter your choice: 6

Exi ng...

=== Code Execu on Successful ===


Input:-
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_NAME_LENGTH 100

// Define the structure for a student record typedef struct Student {

char name[MAX_NAME_LENGTH];

int rollNumber; float marks; struct Student* next;

} Student;

// Global pointer to the head of the linked list

Student* head = NULL;

// Func on to add a new student record to the linked list void addStudent(char name[], int

rollNumber, float marks)

Student* newStudent = (Student*)malloc(sizeof(Student)); if (newStudent ==

NULL) { prin ("Memory alloca on failed!\n"); return;

strncpy(newStudent->name, name, MAX_NAME_LENGTH); newStudent-

>rollNumber = rollNumber; newStudent->marks = marks; newStudent->next =

head; head = newStudent;

// Func on to delete a student record with a given roll number void deleteStudent(int rollNumber)

{ Student *current = head;

Student *previous = NULL; // If the list is empty if (head == NULL) {

prin ("No records to delete.\n"); return;

// Check if the student to be deleted is the head if (head->rollNumber == rollNumber)

{ Student* temp = head; head = head->next; free(temp); prin ("Student

with roll number %d deleted.\n", rollNumber);

return;

}
// Traverse the list to find the student while (current != NULL && current-

>rollNumber != rollNumber) { previous = current; current = current->next;

// If the student was not found if (current == NULL) { prin ("Student with roll number %d

not found.\n", rollNumber); return;

// Delete the student record previous->next = current->next; free(current); prin

("Student with roll number %d deleted.\n", rollNumber);

// Func on to display all student records void displayStudents() { Student*

current = head; if (current == NULL) { prin ("No student records available.\n");

return;

prin ("Student Records:\n"); while (current != NULL) { prin ("Name: %s, Roll Number: %d,

Marks: %.2f\n", current->name, current->rollNumber, current->marks); current = current-

>next;

// Func on to search for a student record with a given roll number int searchStudent(int rollNumber) {

Student* current = head; int index = 0; while (current != NULL) { if (current->rollNumber ==

rollNumber) { return index;

current = current->next; index++;

return -1;

// Func on to handle the menu op ons and perform opera ons void menu() { int choice,

rollNumber;

char name[MAX_NAME_LENGTH];

float marks; while (1) {

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);

switch (choice) { case 1:

prin ("Enter name: ");

scanf(" %[^\n]", name); // Read string with spaces prin ("Enter roll number: ");

scanf("%d", &rollNumber); prin ("Enter marks: "); scanf("%f", &marks);

addStudent(name, rollNumber, marks);

break; case 2:

prin ("Enter roll number to delete: "); scanf("%d", &rollNumber);

deleteStudent(rollNumber);

break; case 3: displayStudents();

break; case 4:

prin ("Enter roll number to search: "); scanf("%d", &rollNumber);

int posi on = searchStudent(rollNumber); if (posi on != -1) {

prin ("Student with roll number %d found at posi on %d.\n", rollNumber, posi on);

} else {

prin ("Student with roll number %d not found.\n", rollNumber);

} } break; case 5:

// Free all allocated memory while (head != NULL) {

Student* temp = head; head = head->next; free(temp);

prin ("Exi ng...\n");

exit(0); default:

prin ("Invalid choice. Please try again.\n");

int main() { menu(); return 0;

Output:-
Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 1

Enter name: Stu e

Enter roll number: 26

Enter marks: 98 Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 1

Enter name: Karan

Enter roll number: 03

Enter marks: 97 Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 1

Enter name: Ankit

Enter roll number: 25

Enter marks: 65

Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 1

Enter name: Ayush

Enter roll number: 2


Enter marks: 30 Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 1

Enter name: Bhagat

Enter roll number: 6

Enter marks: 87 Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit Enter your choice: 3

Student Records:

Name: Bhagat, Roll Number: 6, Marks: 87.00

Name: Ayush, Roll Number: 2, Marks: 30.00

Name: Ankit, Roll Number: 25, Marks: 65.00

Name: Karan, Roll Number: 3, Marks: 97.00 Name: Stu e, Roll Number: 26, Marks: 98.00

Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 2

Enter roll number to delete: 6 Student with roll number 6 deleted.

Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record

5. Exit Enter your choice: 3

Student Records:

Name: Ayush, Roll Number: 2, Marks: 30.00

Name: Ankit, Roll Number: 25, Marks: 65.00


Name: Karan, Roll Number: 3, Marks: 97.00 Name: Stu e, Roll Number: 26, Marks: 98.00

Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 4

Enter roll number to search: 26 Student with roll number 26 found at posi on 3.

Menu:

1. Add a student record

2. Delete a student record

3. Display all student records

4. Search for a student record 5. Exit

Enter your choice: 5

Exi ng...

=== Code Execu on Successful ===

You might also like