-1

I wonder how to make 2 functions based of this code:

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

typedef struct node {
    int val;
    struct node * next;
} node_t;



node_t* create_node(int val)
{
    node_t * head = NULL;
    head = malloc(sizeof(node_t));
    if (head == NULL) {
        return NULL;
    }

    head->val = val;
    head->next = NULL;
}


void print_list(node_t * head)
{
    node_t * current = head;

    while (current != NULL) {
        printf("%d\n", current->val);
        current = current->next;
    }
}


void insert_end(node_t * head, int val)
{
    node_t * current = head;
    while (current->next != NULL) {
        current = current->next;
    }


    current->next = malloc(sizeof(node_t));
    current->next->val = val;
    current->next->next = NULL;
}


void insert_begin(node_t ** head, int val)
{
    node_t * new_node;
    new_node = malloc(sizeof(node_t));
    new_node->val = val;
    new_node->next = *head;
    *head = new_node;
}

One function should remove an element with the given index, and the second one with the given value.

I'm new to the C language, and I don't really understand the syntax, that's why I would appreciate any kind of help.

1 Answers1

0

If you want to delete a specific index, then follow the procedure:

  1. Firstly iterate to that index, starting from the head.Let say we are searching node N, so that scenario becomes, (N-1) Node-> N Node -> (N+1) Node.

You need to just make it (N-1) Node -> (N+1) Node.

you need to take care of two corner cases. If the required node is the first or the last node then you have to do it differently.

If you can solve like this, hopefully, you would be abel to the second problem. In this case, you have to iterate to that specific value starting from the end.

Shovo
  • 133
  • 2
  • 9