0

I am newbie in C and I am trying to write a linked list in which each node simply contains an int. The definition of the structure is ok, but I also want to write methods to update this linked list (add element at the tail and delete the head element). (I want to be able to read the most recently added element)

I wrote the functions below, but I don't know where the free should take place and how to implement it. Could anyone help me with this?

typedef struct Node{
    Node next = NULL;
    int number;
} Node;

void add_node(Node *LL,int val){ 
    // add node to the end of the linked list
    new_node = (struct Node *)malloc(1*sizeof(struct Node));
    new_node->number = val;
    Node n = *LL;
    while (n.next != NULL){
        n = n.next;
    }
    n.next = new_node;
}

void delete_head(Node *LL){
     // update the head
    *LL = LL->next;
    //free?
}

void update_LL(*LL,int val){
    add_node(*LL,val);
    delete_head(*LL);
}
2
  • 1
    Node next = NULL; --> struct Node *next; in C. Commented Oct 7, 2014 at 15:57
  • 1
    As written, there is no way to update the head from within delete_head: it is passed a pointer to the head node, but has no idea where that value is stored, and thus cannot update it. You can (as many have explained) delete that node, but whatever is keeping track of the head needs to be updated to the new head. Commented Oct 7, 2014 at 16:06

4 Answers 4

1

I rename your data structure this way:

struct pointer
            {
            int field; 

            struct pointer *link; 
            };
typedef struct pointer cell;  

Then we can use this function for your need:

void ad_an_element_at_the_end_of_the_list()
         {
         cell *p=NULL;
         cell *ptr=head;

         int value;

         cout<<" Integer number to insert at the end of the list: ";
         cin>>value;
         p=(cell*)malloc(sizeof(cell));
         p->field=value;
         p->link=NULL;
         if(ptr==NULL) 
            {
            ptr=p;
            head=ptr;

            }else
                {
                if(ptr->link==NULL)  t
                  {
                  ptr->link=p;
                  head=ptr;

                  }else
                     {
                      while(ptr->link!=NULL) 
                        {
                        ptr=ptr->link;
                        }
                     ptr->link=p;

                    }
            }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Try changing *LL = LL->next; for Node *nextNode = LL->next;. Then you can call free(LL) followed by LL = nextNode.

void delete_head(Node *LL){
    Node *nextNode = LL->next;
    free(LL);
    LL = nextNode;
}

This then frees the Node at the head and moves the pointer to the next one in the Linked List.

5 Comments

Since LL is effectively a local variable, this will not move the head.
@ScottHunter It's been a while since I've used C but would this be solved by passing a reference (using the & symbol before the variable, when passing it to the function)?
I think you should review how "passing by reference" and "passing by value" is done. If a pointer is passed as an argument it points to a memory location, whatever operations are performed on that memory location by whomever function in whichever scope are permanent.
@MurtazaZaidi ^ This was my original understanding
@AeroX: No; then you'd be passing a pointer-to-a-pointer-to-a-Node, and have a type mismatch. But if LL were declared as Node **LL, and the code referred to *LL for the pointer to the Node, then that would work.
0

You need to save your link to node which is next before you delete current node. Otherwise you won't be able to refer any node of your linkedlist. Now when you have backed up your link to next node, you can free current node pointed by LL, and then assign LL pointer to next node which you had earlier backed up in temp pointer.

Node *temp = LL->next;
free(LL);
LL = temp;

9 Comments

Since LL is effectively a local variable, this will not move the head.
@ScottHunter please review this nongnu.org/c-prog-book/online/x641.html
If a pointer is passed as an argument it points to a memory location, whatever operations are performed on that memory location by whomever function in whichever scope are permanent.
But the pointer being passed points to a Node, so you can make changes to that Node, but not the original variable pointing to it (which is what head is). And to do that, you'd need to make an assignment to *LL; changes to LL from within this function have no effect outside of the function.
I think you need to review your knowledge of pointers. If p is a pointer, it points to some memory location, we use p to make changes in that memory location directly. Now that change is permanent. If I free that memory location all data which was stored there is now lost, if I update value at that location, new value would be permanent, not specific to scope of function.
|
0

Possibly a duplicate of this question LinkedList - How to free the memory allocated using malloc

Basically you have to store the pointer to the node you want to delete otherwise you will leak that memory as there will be no reference to that memory location anywhere in your code.

try this in your delete_head function:

Node *temp = LL;

*LL = LL->next;

free(LL);

Hope this helps!

3 Comments

temp and *LL don't have the same type; for that matter, neither do *LL and LL->next.
Don't trust me; ask a compiler.
Ok, since it was declared as a Node in the Node class not a node pointer I got things mixed up. Assuming that it is changed to a Node pointer so it can compile, this solution should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.