1

I am trying to remove a node from a linked list. What should be the data type of the "indirect" variable ?

I am trying to remove a node from a linked list using pointers. I am not able to figure out the type of the variable "indirect" in the function remove_list_entry. remove_list_entry expects a Node to remove as input.

What should be the datatype?

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

void remove_list_entry(struct Node* entry)
{
    indirect=&head;

    while ((*indirect) != entry)
        indirect = &(*indirect)->next;

    *indirect = entry->next;
}

Edit:- So below is the solution to this

#include <stdlib.h> 

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

void remove_list_entry(struct Node* entry, struct Node* head)
{
    struct Node** indirect=&head;

    while ((*indirect) != entry)
        indirect = &(*indirect)->next;

    *indirect = entry->next;
}

void print_list(struct Node* ptr)
{
    while(ptr!=NULL)
    {
        printf("%d--> ",ptr->data);
        ptr=ptr->next;
    }
}
int main()
{
    struct Node* head = NULL;

    struct Node* first = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;

    first = (struct Node*)malloc(sizeof(struct Node)); 
    second = (struct Node*)malloc(sizeof(struct Node)); 
    third = (struct Node*)malloc(sizeof(struct Node));

    first->data = 1; 
    first->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    head = first;
    printf("Initial Linked List\n");
    print_list(head);

    printf("\n\nNode to remove %d",second->data);
    remove_list_entry(second,head);

    printf("\n\nAfter deletion\n");
    print_list(head);

    return 0;
}
Akash jain
  • 189
  • 1
  • 8
  • What do you think and why? If you do `entry->next`, what must the type of `entry` be for that to work? – Blaze Sep 03 '19 at 07:44
  • should be of type Node – RoQuOTriX Sep 03 '19 at 07:46
  • What is the type of `head`? Where is it? – chux - Reinstate Monica Sep 03 '19 at 07:46
  • 3
    It's not really clear. What is `entry`, is it a pointer, an instance of `struct Node` or an integer value? How is the linked list defined, and so on. Post a [mcve]. – Lundin Sep 03 '19 at 07:48
  • Related: https://stackoverflow.com/questions/982388/how-to-implement-a-linked-list-in-c (Spoiler warning) – moooeeeep Sep 03 '19 at 07:57
  • 1
    It should be the same type as `&head` - "pointer to whatever `head` is". – molbdnilo Sep 03 '19 at 08:01
  • Why do you think you need another level of indirection here? Why do you think you need the *address* of `head` (or any other node)? What is the type of `entry` supposed to be? – John Bode Sep 03 '19 at 12:28
  • @JohnBode ... Linus Torvalds suggested this solution in a Ted talk video. I was trying to code the same , but was stuck on the data types. – Akash jain Sep 04 '19 at 08:35
  • @Lundin -- As mentioned in the original post "remove_list_entry expects a Node to remove as input." , entry is a node to delete. – Akash jain Sep 04 '19 at 08:36
  • @Akashjain Don't describe the code, _post_ the code. The real code, which compiles. Or if it doesn't compile, post the compiler errors. – Lundin Sep 04 '19 at 08:38
  • @Akashjain: Hm. I'd need to see the video, because in this particular case it doesn't make much sense. Now, if either the `entry` or `head` parameters needed to be updated to point to a different node it would make sense to declare them as `struct Node **`, but as a temporary to point to the current list object? All it does is add an extra layer of dereference syntax, which makes the code marginally harder to read. – John Bode Sep 04 '19 at 14:09
  • @JohnBode ... here it is [link](https://www.youtube.com/watch?v=o8NPllzkFhE&t=943s) ... Skip to 14:27 – Akash jain Sep 04 '19 at 17:58

1 Answers1

2

You need a double-pointer, i.e. struct Node**. The type of entry should be struct Node*.

Erlkoenig
  • 2,664
  • 1
  • 9
  • 18