Untitled

 avatar
unknown
plain_text
a year ago
3.5 kB
2
Indexable
#include <stdio.h>
#include <stdlib.h>

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

// Function to insert a node at the beginning of the linked list
void insertAtBegin(struct Node **head, int value) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        return;
    }

    newNode->data = value;
    newNode->next = *head;
    *head = newNode;
}

// Function to delete the first node of the linked list
void deleteAtBegin(struct Node **head) {
    if (*head == NULL) {
        printf("List is empty, nothing to delete\n");
        return;
    }

    struct Node *temp = *head;
    *head = temp->next;
    free(temp);
}

// Function to insert a node at the end of the linked list
void insertAtEnd(struct Node **head, int value) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        return;
    }

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

    if (*head == NULL) {
        *head = newNode;
        return;
    }

    struct Node *temp = *head;
    while (temp->next != NULL) {
        temp = temp->next;
    }

    temp->next = newNode;
}

// Function to delete the last node of the linked list
void deleteAtEnd(struct Node **head) {
    if (*head == NULL) {
        printf("List is empty, nothing to delete\n");
        return;
    }

    if ((*head)->next == NULL) {
        free(*head);
        *head = NULL;
        return;
    }

    struct Node *temp = *head;
    struct Node *prev = NULL;

    while (temp->next != NULL) {
        prev = temp;
        temp = temp->next;
    }

    free(temp);
    prev->next = NULL;
}

// Function to display the linked list
void display(struct Node *head) {
    struct Node *temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

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

    do {
        printf("\nMenu:\n");
        printf("1. Insert at the beginning\n");
        printf("2. Delete at the beginning\n");
        printf("3. Insert at the end\n");
        printf("4. Delete at the end\n");
        printf("5. Display\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                insertAtBegin(&head, value);
                break;
            case 2:
                deleteAtBegin(&head);
                break;
            case 3:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                insertAtEnd(&head, value);
                break;
            case 4:
                deleteAtEnd(&head);
                break;
            case 5:
                display(head);
                break;
            case 0:
                printf("Exiting program\n");
                break;
            default:
                printf("Invalid choice, please try again\n");
        }

    } while (choice != 0);

    // Clean up the remaining nodes in the linked list before exiting
    while (head != NULL) {
        deleteAtBegin(&head);
    }

    return 0;
}
Leave a Comment