Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.6 kB
2
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

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 of the linked list
struct Node* insertAtBeginning(struct Node* head, int data) {
    struct Node* newNode = createNode(data);
    newNode->next = head;
    head = newNode;
    return head;
}

// function to insert a node at the end of the linked list
struct Node* 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;
    }
    return head;
}

// function to insert a node at a given position in the linked list
struct Node* insertAtPosition(struct Node* head, int data, int position) {
    struct Node* newNode = createNode(data);
    if (position == 1) {
        newNode->next = head;
        head = newNode;
    }
    else {
        struct Node* temp = head;
        for (int i = 1; i < position - 1 && temp != NULL; i++) {
            temp = temp->next;
        }
        if (temp == NULL) {
            printf("Invalid position!\n");
        }
        else {
            newNode->next = temp->next;
            temp->next = newNode;
        }
    }
    return head;
}

// function to delete a node from the beginning of the linked list
struct Node* deleteFromBeginning(struct Node* head) {
    if (head == NULL) {
        printf("Linked list is empty!\n");
    }
    else {
        struct Node* temp = head;
        head = head->next;
        free(temp);
    }
    return head;
}

// function to delete a node from the end of the linked list
struct Node* deleteFromEnd(struct Node* head) {
    if (head == NULL) {
        printf("Linked list is empty!\n");
    }
    else {
        struct Node* temp = head;
        struct Node* prev = NULL;
        while (temp->next != NULL) {
            prev = temp;
            temp = temp->next;
        }
        if (prev == NULL) {
            head = NULL;
        }
        else {
            prev->next = NULL;
        }
        free(temp);
    }
    return head;
}

// function to delete a node from a given position in the linked list
struct Node* deleteFromPosition(struct Node* head, int position) {
    if (head == NULL) {
        printf("Linked list is empty!\n");
    }
    else if (position == 1) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
    }
    else {
        struct Node* temp = head;
        struct Node* prev = NULL;
        for (int i = 1; i < position && temp != NULL; i++) {
            prev = temp;
            temp = temp->next;
        }
        if (temp == NULL) {
            printf("Invalid position!\n");
        }
else {
        prev->next = temp->next;
        free(temp);
    }
}
return head;
}

// function to search for a node with a given value in the linked list
void search(struct Node* head, int data) {
int found = 0;
int position = 1;
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == data) {
printf("%d found at position %d\n", data, position);
found = 1;
break;
}
temp = temp->next;
position++;
}
if (found == 0) {
printf("%d not found in the linked list\n", data);
}
}

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

// main function to test the linked list implementation
int main() {
struct Node* head = NULL;
int choice, data, position;
while (1) {
    printf("\n");
    printf("1. Insert at beginning\n");
    printf("2. Insert at end\n");
    printf("3. Insert at position\n");
    printf("4. Delete from beginning\n");
    printf("5. Delete from end\n");
    printf("6. Delete from position\n");
    printf("7. Search\n");
    printf("8. Display\n");
    printf("9. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("Enter the data to be inserted: ");
            scanf("%d", &data);
            head = insertAtBeginning(head, data);
            break;
        case 2:
            printf("Enter the data to be inserted: ");
            scanf("%d", &data);
            head = insertAtEnd(head, data);
            break;
        case 3:
            printf("Enter the data to be inserted: ");
            scanf("%d", &data);
            printf("Enter the position: ");
            scanf("%d", &position);
            head = insertAtPosition(head, data, position);
            break;
        case 4:
            head = deleteFromBeginning(head);
            break;
        case 5:
            head = deleteFromEnd(head);
            break;
        case 6:
            printf("Enter the position: ");
            scanf("%d", &position);
            head = deleteFromPosition(head, position);
            break;
        case 7:
            printf("Enter the data to be searched: ");
            scanf("%d", &data);
            search(head, data);
            break;
        case 8:
            display(head);
            break;
        case 9:
            exit(0);
        default:
            printf("Invalid choice!\n");
    }
}

return 0;