0% found this document useful (0 votes)
7 views2 pages

8

The document contains a C program that implements a singly linked list with functionalities to insert nodes at the beginning and end, delete a node by value, and traverse the list to print its elements. It defines a Node structure and provides functions for each operation, along with a main menu for user interaction. The program runs in a loop until the user chooses to exit.

Uploaded by

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

8

The document contains a C program that implements a singly linked list with functionalities to insert nodes at the beginning and end, delete a node by value, and traverse the list to print its elements. It defines a Node structure and provides functions for each operation, along with a main menu for user interaction. The program runs in a loop until the user chooses to exit.

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

// Node structure
typedef struct Node {
int data;
struct Node* next;
}Node;

// Function to create a new node


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

// Function to insert a node at the beginning


void insertAtBeginning(Node** head, int data) {
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(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
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(Node** head, int data) {
Node* temp = *head;
Node* prev = NULL;

// If the head node itself holds the data to be deleted


if (temp != NULL && temp->data == data) {
*head = temp->next;
free(temp);
printf("Deleted %d from the list\n", data);
return;
}

// Function to traverse and print the linked list


void traverseList(Node* head) {
if (head == NULL) {
printf("The list is empty\n");
return;
}
Node* temp = head;
printf("Linked list elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

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

while (1) {
printf("\nMenu:\n");
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Delete a node\n");
printf("4. Traverse the list\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

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

return 0;
}

You might also like