#include <stdio.
h>
#include <stdlib.h>
// Define the structure for a node
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));
     if (newNode == NULL) {
         printf("Memory allocation failed!\n");
         exit(1);
     newNode->data = data;
     newNode->next = NULL;
     return newNode;
// Function to insert at beginning
struct Node* insertAtBeginning(struct Node* head, int data) {
     struct Node* newNode = createNode(data);
     newNode->next = head;
     return newNode;
// Function to insert at end
struct Node* insertAtEnd(struct Node* head, int data) {
     struct Node* newNode = createNode(data);
    if (head == NULL) {
        return newNode;
    struct Node* current = head;
    while (current->next != NULL) {
        current = current->next;
    current->next = newNode;
    return head;
// Function to delete a node with given value
struct Node* deleteNode(struct Node* head, int data) {
    if (head == NULL) return NULL;
    if (head->data == data) {
        struct Node* temp = head->next;
        free(head);
        return temp;
    struct Node* current = head;
    while (current->next != NULL && current->next->data != data) {
        current = current->next;
    if (current->next != NULL) {
        struct Node* temp = current->next;
        current->next = temp->next;
        free(temp);
    }
    return head;
// Function to print the list
void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    printf("NULL\n");
// Function to free the entire list
void freeList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        struct Node* temp = current;
        current = current->next;
        free(temp);
// Example usage
int main() {
    struct Node* head = NULL;
    // Insert some elements
    head = insertAtEnd(head, 10);
    head = insertAtEnd(head, 20);
    head = insertAtBeginning(head, 5);
    head = insertAtEnd(head, 30);
    // Print the list
    printf("Original list: ");
    printList(head);
    // Delete a node
    head = deleteNode(head, 20);
    printf("After deleting 20: ");
    printList(head);
    // Free the list
    freeList(head);
    return 0;