Knowee
Questions
Features
Study Tools

Write a program in C, to create a link list and perform operation such as inserting or deleting a node from list

Question

Write a program in C, to create a link list and perform operation such as inserting or deleting a node from list

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a step-by-step guide to creating a linked list and performing operations such as inserting or deleting a node from the list in C:

Step 1: Define the structure for the linked list node

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

Step 2: Create a function to insert a node at the beginning of the linked list

void insertNode(struct Node** head, int newData) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = newData;
    newNode->next = *head;
    *head = newNode;
}

Step 3: Create a 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);
        return;
    }

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

    if (temp == NULL) {
        return;
    }

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

Step 4: Create a function to display the linked list

void displayList(struct Node* node) {
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}

Step 5: Test the linked list operations in the main function

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

    insertNode(&head, 3);
    insertNode(&head, 5);
    insertNode(&head, 7);
    insertNode(&head, 9);

    printf("Linked list: ");
    displayList(head);

    deleteNode(&head, 7);

    printf("Linked list after deleting node with key 7: ");
    displayList(head);

    return 0;
}

This program creates a linked list with nodes containing integer data. It provides functions to insert a node at the beginning of the list and delete a node with a specific key from the list. The main function demonstrates the usage of these functions by inserting nodes into the list, displaying the list, and deleting a node with a specific key.

This problem has been solved

Similar Questions

Write a program in C, to create and display Single Linked List

C program to performs all operations on singly linked listWrite a program that uses functions to perform the following operations on singly linked listi) Creation ii) Insertion  iii) Deletion  iv) TraversalSample Test CasesTest Case 1:Expected Output:Singly·Linked·List·Example·-·All·OperationsOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·111Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·222Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·333Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·444Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·222·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·2Enter·position·of·the·element·for·deleteing·the·element·:·2Deleted·successfullyOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·4No·of·elements·in·the·linked·list·are·:·3Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·5

What is doubly Linked List? What are its applications?Explain how an element can be deleted from doublylinked list using C program

Linked List Operations: Traverse, Insert and DeleteTraversal - access each element of the linked list.Insertion - adds a new element to the linked list.Deletion - removes the existing elements.Search - find a node in the linked list.Sort - sort the nodes of the linked list.

Which of the following code snippets represents the correct way to delete a node from a singly linked list in C++?Group of answer choicesNode* temp = node->next; node = temp; delete temp;delete node;Node* temp = node; node = temp->next; delete temp;Node* temp = node; node->next = temp->next; delete temp;

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.