9.C Program to implement a linear linked list(Part B)

9.C Program to implement a linear linked list


ALGORITHM :

Step 1: Start
Step 2: Define a structure to represent a node in the linked list (e.g., Node) with necessary variables (e.g., data to store the data, and next to store the address of the next node).
Step 3: Declare a pointer variable (head) to represent the head (first node) of the linked list and initialize it to NULL.
Step 4: Implement the create_node function to create a new node with the given data.
        a. Input the data for the new node.
        b. Allocate memory for the new node.
        c. Set the data of the new node to the input data.
        d. Set the next of the new node to NULL.
        e. Return the address of the new node.
Step 5: Implement the insert_at_begin function to insert a new node at the beginning of the linked list.
        a. Create a new node using the create_node function.
        b. If the linked list is empty (head == NULL), set the head to the new node.
        c. If the linked list is not empty, set the next of the new node to the current head, and update the head to the new node.
Step 6: Implement the insert_at_end function to insert a new node at the end of the linked list.
        a. Create a new node using the create_node function.
        b. If the linked list is empty (head == NULL), set the head to the new node.
        c. If the linked list is not empty, traverse to the last node (using a temporary pointer variable) and set the next of the last node to the new node.
Step 7: Implement the display_list function to display the elements of the linked list.
        a. Initialize a temporary pointer variable (current) to the head of the linked list.
        b. Repeat the following steps while the current pointer is not NULL:
               i. Display the data of the current node.
               ii. Move the current pointer to the next node.
Step 8: Implement the delete_list function to delete the entire linked list and free the memory.
        a. Initialize a temporary pointer variable (current) to the head of the linked list.
        b. Repeat the following steps while the current pointer is not NULL:
               i. Keep a pointer (next_node) to the next node.
               ii. Free the memory of the current node using the free() function.
               iii. Move the current pointer to the next node (next_node).
Step 9: End

SOURCE CODE :

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

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

void insertAtEnd(struct Node **head, int data) {
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;

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

void display(struct Node *head) {
    if (head == NULL) {
        printf("Linked list is empty.\n");
    } else {
        printf("Linked list elements: ");
        while (head != NULL) {
            printf("%d ", head->data);
            head = head->next;
        }
        printf("\n");
    }
}

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

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

    insertAtEnd(&head, 5);
    insertAtEnd(&head, 10);
    insertAtEnd(&head, 15);
    display(head);

    freeLinkedList(head);

    return 0;
}


OUTPUT :

Linked list elements: 5 10 15 

No comments:

Post a Comment