简体   繁体   中英

What happens when pointer A points pointer B, and pointer B changed

I am doing single linked list. Here is list_elem class:

    class list_elem{
     friend class single_list;
     private:
      int data;   /* Data of list element. */
      list_elem *next;   /* Next list element. */
     public:...

And I have another class called single_list, where contains member methods I need to implement

    class single_list{
     private:
     list_elem *head; /* List head. */
     public:
     void list_insert_front(list_elem *elem)
     ...

Here is the code where I am confused with, I want to insert an element in the beginning of list.

    void single_list::list_insert_front (list_elem *elem)
    {
        if (head == NULL)
        {
         head == elem;
        }
        else
        {
         elem->next = head;  //confused
         head == elem;       //confused
        }
    }

I wonder, after "head == elem", will elem->next still points to the first element of the list? What is elem->next points to now?

I also thought about another solution for this. Can anyone tell me if this one is correct?

    void single_list::list_insert_front (list_elem *elem)
    {
        if (head == NULL)
        {
         head == elem;
        }
        else
        {
         list_elem* temp;
         temp = head; 
         elem->next = temp;
         head == elem;
        }
    }

Many thanks in advance

First, your code incorrectly uses == in place of = *

The code for inserting an element at the head should look like this:

elem->next = head; // This works when head==NULL, too
head = elem;

What this does is pointing the next of the elem being inserted at the former head on the first line, and making elem the new head on the second line.

Here is a diagram that shows what is going on:

链表转换

  • First drawing shows the list before an addition of the new element shown in pink.
  • Second drawing shows the list after the first assignment elem->next = head is made
  • Third drawing shows the final state of the list.

* I've seen it swapped the other way around too many times, but this is the first time I see it swapped like this.

See my comments below:

void single_list::list_insert_front (list_elem *elem)
    {
        if (head == NULL)//head is the first reference in the linked-list
        {
         head == elem;//should be "=" not "==", since your linked-list is empty(no elements added yet) your head reference is assigned the reference value elem which should be null the first time. so your linked list structure at this points looks like head->null, head reference pointing to null.
        }
        else// at this point you are inserting the 1st to n elements
        {
         elem->next = head;  //Let's assume you previously inserted an element, Here you are assigning the reference that head was pointing to elem, this is necessary so we do not lose the reference of the element that was previously in the first position.  The structure looks like: "head->?", "new elemenent->reference of the previous element that was first"
         head == elem;       //Now we set the head reference equal to the reference of the new inserted element(which is now the first element), so the linked list structure looks like: head->[reference of new element]->[previous element] *
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM