Thread: Requesting clarification regarding double pointers and linked lists

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    18

    Requesting clarification regarding double pointers and linked lists

    [FONT=sans-serif]Hi everyone, I am currently learning C using KNKing's book. Currently I'm confused by pointers to pointers/linked lists and would like to kindly request for some help and clarifications, with regards to this function below. Thank you all and bless.

    The below function will be called with (&first,10), with first being a pointer to the first node in the list and 10 being the value in which the node is to be deleted.
    Code:
    struct node *delete_from_list(struct node **list, int n)
    {
      struct node *item = *list;
      while (item) {
        if (item->value == n) {
          *list = item->next;
          free(item);
          break;
        }
        list = &item->next;
        item = item->next;
      }
      return *list;
    }

    my question is. whenver i use the "*list",
    *list = item->next;
    am i not editing the address which the pointer "first" contains? so when the above statement is executed wont "first" no longer be pointing to the first node? Thank you!
    Last edited by Salem; 10-27-2020 at 08:22 AM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    If your list is
    1 -> 2 -> 3 -> 4 -> 5
    and you try to delete 3 by doing this
    *list = item->next;

    You end up with 4 -> 5 as your list.

    Typically, you need to keep track of the previous node as well.
    Code:
    item = head;
    prev = NULL;
    while (item) {
      prev = item;
      item = item->next;
    }
    So that when you reach the node to delete, say 3, you have (or can easily obtain) pointers to nodes 2, 3, and 4.

    Because you want to turn
    1 -> 2 -> 3 -> 4 -> 5
    into
    1 -> 2 -> 4 -> 5
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    18

    Yes

    Quote Originally Posted by Salem View Post
    If your list is
    1 -> 2 -> 3 -> 4 -> 5
    and you try to delete 3 by doing this
    *list = item->next;

    You end up with 4 -> 5 as your list.

    Typically, you need to keep track of the previous node as well.
    Code:
    item = head;
    prev = NULL;
    while (item) {
      prev = item;
      item = item->next;
    }
    So that when you reach the node to delete, say 3, you have (or can easily obtain) pointers to nodes 2, 3, and 4.

    Because you want to turn
    1 -> 2 -> 3 -> 4 -> 5
    into
    1 -> 2 -> 4 -> 5
    Yes sir I get what you are saying, normally theres 2 pointers. But this is an exercise in a textbook (c programming a modern approach) which asks that
    Modify the delete_from_list function so that is uses only one pointer variable instead of two (cur and prev).

    which is why I was wondering. If I use the code as posted in my original post, "first" will no longer be pointing to the first node, so the code aint really right, right? Thank you for the fast response!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,617
    > Modify the delete_from_list function so that is uses only one pointer variable instead of two (cur and prev).
    I'm always suspicious of books / tutors that try to teach magic tricks as opposed to sound programming.

    But FWIW, try using recursion to get an extra variable 'for free'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 9
    Last Post: 01-03-2009, 03:48 PM
  3. Double pointers and linked lists.
    By simo_r in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 04:25 AM
  4. Double linked lists
    By JFonseka in forum C Programming
    Replies: 19
    Last Post: 03-13-2008, 06:05 AM
  5. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM

Tags for this Thread