Operations on Singly Linked List

  1. Creation:

    • Define a Node class with data and next attributes.

  2. Insertion:

    • At the beginning: Insert a new node at the start of the list.

    • At the end: Append a new node to the end of the list.

    • After a given node: Insert a new node after a specific node.

  3. Deletion:

    • By key: Remove the first node with a specific value.

    • By position: Remove the node at a specific position.

  4. Traversal:

    • Visit each node in the list and print its data.

  5. Search:

    • Look for a node with a specific value in the list.

				
					#include <stdio.h>
#include <stdlib.h>

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

struct Node* head = NULL;

void InsertAtFirst() {
    int data;
    struct Node* newNode;
    printf("Enter data insert at first: ");
    scanf("%d", &data);
    newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = head;
    head = newNode;
}

void InsertAtEnd() {
    int data;
    struct Node *newNode, *temp;
    printf("Enter data insert at end: ");
    scanf("%d", &data);
    newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    
    if (head == NULL) {
        head = newNode;
    } else {
        temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

void InsertAtPosition() {
    int data, pos, count = 1;
    struct Node *newNode, *temp;
    printf("Enter data to insert: ");
    scanf("%d", &data);
    printf("Enter position to insert: ");
    scanf("%d", &pos);
    newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    
    if (pos == 1) {
        newNode->next = head;
        head = newNode;
    } else {
        temp = head;
        while (count < pos - 1 && temp != NULL) {
            temp = temp->next;
            count++;
        }
        if (temp != NULL) {
            newNode->next = temp->next;
            temp->next = newNode;
        } else {
            printf("Invalid position\n");
            free(newNode);
        }
    }
}

void DeleteFirst() {
    struct Node* temp;
    if (head == NULL) {
        printf("List is empty\n");
    } else {
        temp = head;
        head = head->next;
        printf("Deleted first node with data %d\n", temp->data);
        free(temp);
    }
}

void DeleteLast() {
    struct Node *temp, *prev;
    if (head == NULL) {
        printf("List is empty\n");
    } else if (head->next == NULL) {
        printf("Deleted last node with data %d\n", head->data);
        free(head);
        head = NULL;
    } else {
        temp = head;
        while (temp->next != NULL) {
            prev = temp;
            temp = temp->next;
        }
        printf("Deleted last node with data %d\n", temp->data);
        free(temp);
        prev->next = NULL;
    }
}

void DeleteNNode() {
    int pos, count = 1;
    struct Node *temp, *prev;
    printf("Enter position to delete: ");
    scanf("%d", &pos);
    if (head == NULL) {
        printf("List is empty\n");
    } else if (pos == 1) {
        DeleteFirst();
    } else {
        temp = head;
        while (count < pos && temp != NULL) {
            prev = temp;
            temp = temp->next;
            count++;
        }
        if (temp != NULL) {
            prev->next = temp->next;
            printf("Deleted node at position %d with data %d\n", pos, temp->data);
            free(temp);
        } else {
            printf("Invalid position\n");
        }
    }
}

void Display() {
    struct Node* temp;
    if (head == NULL) {
        printf("List is empty\n");
    } else {
        temp = head;
        printf("Data in Singly Linked List:\n");
        while (temp != NULL) {
            printf("%d\t", temp->data);
            temp = temp->next;
        }
    }
}

int main() {
    int choose;
    while (1) {
        printf("\nEnter operation to perform on Singly Linked List\n");
        printf("1 for Insert at First\n");
        printf("2 for Insert at End\n");
        printf("3 for Insert at Position\n");
        printf("4 for Delete First\n");
        printf("5 for Delete Last\n");
        printf("6 for Delete N-th Node\n");
        printf("7 for Display\n");
        printf("8 for Exit\n");
        printf("Choose: ");
        scanf("%d", &choose);
        
        if (choose == 8) {
            printf("You are exiting....\n");
            break;
        } else if (choose == 1) {
            InsertAtFirst();
        } else if (choose == 2) {
            InsertAtEnd();
        } else if (choose == 3) {
            InsertAtPosition();
        } else if (choose == 4) {
            DeleteFirst();
        } else if (choose == 5) {
            DeleteLast();
        } else if (choose == 6) {
            DeleteNNode();
        } else if (choose == 7) {
            Display();
        } else {
            printf("Error, Please enter correct operation\n");
        }
    }
    return 0;
}

				
			

LEAVE A REPLY

Please enter your comment!
Please enter your name here