0% found this document useful (0 votes)
16 views6 pages

Experiment 5

Uploaded by

bobby.valsaraj24
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)
16 views6 pages

Experiment 5

Uploaded by

bobby.valsaraj24
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/ 6

Experiment No.

– 5
Singly Linked List
CODE
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in the linked list


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

// Function to create a new node


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

// Function to insert a node at the beginning


void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
printf("Inserted %d at the beginning.\n", data);
}

// Function to insert a node at the end


void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
printf("Inserted %d at the end.\n", data);
}

// Function to delete a node by value


void deleteNode(struct Node** head, int key) {
struct Node *temp = *head, *prev = NULL;

if (temp != NULL && temp->data == key) {


*head = temp->next;
free(temp);
printf("Deleted node with value %d.\n", key);
return;
}

while (temp != NULL && temp->data != key) {


prev = temp;
temp = temp->next;
}

if (temp == NULL) {
printf("Value %d not found in the list.\n", key);
return;
}

prev->next = temp->next;
free(temp);
printf("Deleted node with value %d.\n", key);
}

// Function to display the linked list


void displayList(struct Node* head) {
if (head == NULL) {
printf("The list is empty.\n");
return;
}

struct Node* temp = head;


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

// Main function
int main() {
struct Node* head = NULL;
int choice, value;

while (1) {
printf("\nSingly Linked List Operations:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Delete by Value\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(&head, value);
break;
case 4:
displayList(head);
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}

return 0;
}

OUTPUT

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 1
Enter value to insert at beginning: 23
Inserted 23 at the beginning.

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 4
Linked List: 23 -> NULL

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 2
Enter value to insert at end: 45
Inserted 45 at the end.

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 4
Linked List: 23 -> 45 -> NULL

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 1
Enter value to insert at beginning: 56
Inserted 56 at the beginning.

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 1
Enter value to insert at beginning: 78
Inserted 78 at the beginning.

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 1
Enter value to insert at beginning: 49
Inserted 49 at the beginning.

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 4
Linked List: 49 -> 78 -> 56 -> 23 -> 45 -> NULL
Singly Linked List Operations:
1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 3
Enter value to delete: 49
Deleted node with value 49.

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice: 4
Linked List: 78 -> 56 -> 23 -> 45 -> NULL

Singly Linked List Operations:


1. Insert at Beginning
2. Insert at End
3. Delete by Value
4. Display List
5. Exit
Enter your choice:

You might also like