Discussion on link list

Discussion on link list

by Shrikanta Paul (221-15-4868) -
Number of replies: 0

1. Write the function of insertion a node after a given node.

void insertAfter(struct Node* prevNode, int newData) {

    if (prevNode == NULL) {

        printf("The given previous node cannot be NULL.\n");

        return;

    }

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    if (newNode == NULL) {

        printf("Memory allocation failed.\n");

        return;

    }

    newNode->data = newData;

    newNode->next = prevNode->next;

    prevNode->next = newNode;

}


2. Write the function of insertion a node as Head.

void insertAsHead(int digit)

{

    struct node *newnode = malloc(sizeof(struct node));


    newnode->data = digit;

    newnode->next = head;


    head = newnode;

}