Skip to main content

LINKED LIST -- INSERT A NODE AT A PARTICULAR POSITION

 

LINKED LIST -- INSERT A NODE AT A PARTICULAR POSITION


Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its  attribute, insert this node at the desired position and return the head node.

A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.

Example
 refers to the first node in the list 

Insert a node at position  with . The new list is 

Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.

insertNodeAtPosition has the following parameters:

  • head: a SinglyLinkedListNode pointer to the head of the list
  • data: an integer value to insert as data in your new node
  • position: an integer position to insert the new node, zero based indexing

Returns

  • SinglyLinkedListNode pointer: a reference to the head of the revised list

Input Format

The first line contains an integer , the number of elements in the linked list.
Each of the next  lines contains an integer SinglyLinkedListNode[i].data.
The next line contains an integer , the data of the node that is to be inserted.
The last line contains an integer .

Constraints

  • , where  is the  element of the linked list.
  • .

Sample Input

3
16
13
7
1
2

Sample Output

16 13 1 7

Explanation

The initial linked list is . Insert  at the position  which currently has  in it. The updated linked list is  16 --> 13 --> 1 --> 7.

SOLUTION


SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) {
    SinglyLinkedListNode* newnode=malloc(sizeof(SinglyLinkedList));
    SinglyLinkedListNode* insert=malloc(sizeof(SinglyLinkedList));
    insert->data=data;
    newnode=llist;
    if(llist==NULL){
        insert->next=NULL;
        llist=insert;
    }
    else{
    for (int i=0; i<=position; i++) {
        if(i==position-1){
            insert->next=newnode->next;
            newnode->next=insert;
        }
        else{
        //newnode->prev=newnode;
        newnode=newnode->next;
        }
    }
    }
    return llist;
}

int main()
{
    FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    SinglyLinkedList* llist = malloc(sizeof(SinglyLinkedList));
    llist->head = NULL;
    llist->tail = NULL;

    char* llist_count_endptr;
    char* llist_count_str = readline();
    int llist_count = strtol(llist_count_str, &llist_count_endptr, 10);

    if (llist_count_endptr == llist_count_str || *llist_count_endptr != '\0') { exit(EXIT_FAILURE); }

    for (int i = 0; i < llist_count; i++) {
        char* llist_item_endptr;
        char* llist_item_str = readline();
        int llist_item = strtol(llist_item_str, &llist_item_endptr, 10);

        if (llist_item_endptr == llist_item_str || *llist_item_endptr != '\0') { exit(EXIT_FAILURE); }

        insert_node_into_singly_linked_list(&llist, llist_item);
    }

    char* data_endptr;
    char* data_str = readline();
    int data = strtol(data_str, &data_endptr, 10);

    if (data_endptr == data_str || *data_endptr != '\0') { exit(EXIT_FAILURE); }

    char* position_endptr;
    char* position_str = readline();
    int position = strtol(position_str, &position_endptr, 10);

    if (position_endptr == position_str || *position_endptr != '\0') { exit(EXIT_FAILURE); }

    SinglyLinkedListNode* llist_head = insertNodeAtPosition(llist->head, data, position);

    char *sep = " ";

    print_singly_linked_list(llist_head, sep, fptr);
    fprintf(fptr, "\n");

    free_singly_linked_list(llist_head);

    fclose(fptr);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) { break; }

        data_length += strlen(cursor);

        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }

        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);

        if (!data) { break; }

        alloc_length = new_length;
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }

    data = realloc(data, data_length);

    return data;
}

Comments

Popular posts from this blog

POST ORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below. It received   parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the   function. Constraints   Nodes in the tree    Output Format Print the tree's postorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 4 3 6 5 2 1 Explanation The postorder traversal is shown. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct  node *ri...

HACKERRANK -- DELETE A NODE AT A GIVEN POSITION

  HACKERRANK --  DELETE A NODE AT A GIVEN POSITION Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example After removing the node at position  ,  . Function Description Complete the  deleteNode  function in the editor below. deleteNode  has the following parameters: -  SinglyLinkedListNode pointer llist:  a reference to the head node in the list -  int position:  the position of the node to remove Returns -  SinglyLinkedListNode pointer:  a reference to the head of the modified list Input Format The first line of input contains an integer  , the number of elements in the linked list. Each of the next   lines contains an integer, the node data values in order. The last line contains an integer,  , the position of the node to delete. Constraints , wh...

TREE PREORDER TRAVERSAL -- HACKERRANK SOLUTIONS

  Complete the   function in the editor below, which has   parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the  preOrder  function. Constraints  Nodes in the tree  Output Format Print the tree's preorder traversal as a single line of space-separated values. Sample Input 1 \ 2 \ 5 / \ 3 6 \ 4 Sample Output 1 2 5 3 4 6 Explanation The preorder traversal of the binary tree is printed. Solution #include   < stdio.h > #include   < string.h > #include   < math.h > #include   < stdlib.h > struct  node {           int  data;      struct  node *left;      struct ...