Linked List Implementation in C - Micro Project

Linked List Implementation in C

Linked List Implementation in C

In this blog post, we will discuss the implementation of a singly linked list using the C programming language.

Linked List Operations

  • Create a new node
  • Insertion at the beginning of the linked list
  • Insertion at the end of the linked list
  • Insertion at a specific position in the linked list
  • Deletion of a node from the linked list
  • Searching for an element in the linked list
  • Displaying the elements of the linked list

Code Implementation


#include 
#include 

// Structure for a linked list 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));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Function to insert a node at the beginning of the linked list
void insertAtBeginning(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    newNode->next = *head;
    *head = newNode;
    printf("Node inserted successfully at the beginning.\n");
}

// Function to insert a node at the end of the linked list
void insertAtEnd(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
        printf("Node inserted successfully at the end.\n");
        return;
    }
    struct Node* temp = *head;
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = newNode;
    printf("Node inserted successfully at the end.\n");
}

// Function to insert a node at a specific position in the linked list
void insertAtPosition(struct Node** head, int data, int position) {
    if (position == 1) {
        insertAtBeginning(head, data);
        return;
    }
    struct Node* newNode = createNode(data);
    struct Node* temp = *head;
    int count = 1;
    while (temp != NULL && count < position - 1) {
        temp = temp->next;
        count++;
    }
    if (temp == NULL) {
        printf("Invalid position.\n");
        return;
    }
    newNode->next = temp->next;
    temp->next = newNode;
    printf("Node inserted successfully at position %d.\n", position);
}

// Function to delete a node from the linked list
void deleteNode(struct Node** head, int key) {
    struct Node* temp = *head;
    struct Node* prev = NULL;
    if (temp != NULL && temp->data == key) {
        *head = temp->next;
        free(temp);
        printf("Node deleted successfully.\n");
        return;
    }
    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("Node not found.\n");
        return;
    }
    prev->next = temp->next;
    free(temp);
    printf("Node deleted successfully.\n");
}

// Function to search for an element in the linked list
void searchElement(struct Node* head, int key) {
    struct Node* temp = head;
    int position = 1;
    while (temp != NULL) {
        if (temp->data == key) {
            printf("Element found at position %d.\n", position);
            return;
        }
        temp = temp->next;
        position++;
    }
    printf("Element not found.\n");
}

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

// Function to clear the linked list and free memory
void clearList(struct Node** head) {
    struct Node* temp = *head;
    while (temp != NULL) {
        struct Node* nextNode = temp->next;
        free(temp);
        temp = nextNode;
    }
    *head = NULL;
    printf("Linked list cleared and memory freed.\n");
}

int main() {
    struct Node* head = NULL;
    int choice, data, position, key;
    while (1) {
        printf("\n--- Linked List Implementation ---\n");
        printf("1. Insert at the beginning\n");
        printf("2. Insert at the end\n");
        printf("3. Insert at a position\n");
        printf("4. Delete a node\n");
        printf("5. Search for an element\n");
        printf("6. Display the linked list\n");
        printf("7. Clear the linked list\n");
        printf("8. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter the element to insert: ");
                scanf("%d", &data);
                insertAtBeginning(&head, data);
                break;
            case 2:
                printf("Enter the element to insert: ");
                scanf("%d", &data);
                insertAtEnd(&head, data);
                break;
            case 3:
                printf("Enter the element to insert: ");
                scanf("%d", &data);
                printf("Enter the position: ");
                scanf("%d", &position);
                insertAtPosition(&head, data, position);
                break;
            case 4:
                printf("Enter the element to delete: ");
                scanf("%d", &key);
                deleteNode(&head, key);
                break;
            case 5:
                printf("Enter the element to search: ");
                scanf("%d", &key);
                searchElement(head, key);
                break;
            case 6:
                displayList(head);
                break;
            case 7:
                clearList(&head);
                break;
            case 8:
                exit(0);
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }
    return 0;
}

        

Output


--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 1
Enter the element to insert: 5
Node inserted successfully at the beginning.

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 2
Enter the element to insert: 10
Node inserted successfully at the end.

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 3
Enter the element to insert: 7
Enter the position: 2
Node inserted successfully at position 2.

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 6
Linked list elements: 5 7 10

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 4
Enter the element to delete: 7
Node deleted successfully.

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 5
Enter the element to search: 10
Element found at position 2.

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 7
Linked list cleared and memory freed.

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 6
Linked list is empty.

--- Linked List Implementation ---
1. Insert at the beginning
2. Insert at the end
3. Insert at a position
4. Delete a node
5. Search for an element
6. Display the linked list
7. Clear the linked list
8. Exit
Enter your choice: 8
        

Conclusion

In this blog post, we have covered the implementation of a linked list in C. Linked lists are fundamental data structures that allow efficient insertion and deletion operations. By understanding and implementing linked lists, you will gain a solid foundation for further exploration of data structures and algorithms.

Comments

Popular posts from this blog

Installation Steps for Flutter and Android Studio

Exploring Flutter's Row and Column Widgets

Library Management System in C