add

Pages: 12
thanks
Last edited on
I would recommend having a pointer to the end (tail) of the list. To add to the tail of the list (given a pointer to the tail):

1. If tail is NULL (empty list), set the head and tail to the new node
2. Otherwise, set the current tail's next node to be the new node. Set the new node to be the tail

If you don't have a pointer to the tail, then you have to navigate from the head to the end of the list, and set the last node's next pointer to the new node.
Last edited on
so this is what i did to add a pointer to the tail of the list

struct member_account *tail=NULL;

do i have to have current point to the tail or do i have t have a tail pointer in my struct function

can you explain to me how would i navigate from the head to the end of the list
Last edited on
To get to the end of the list, traverse along the list until the pointer to the next node in your current node is null. For example:

1
2
3
4
while (// The current node's pointer does not point to null)
{
    // Go to the next pointer.
}
Last edited on
thanksk
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
else {
    current = (struct member_account *)malloc(sizeof(struct member_account));
    strcpy(current->member_last,last);
    strcpy(current->member_first,first); 
    current->member_balance=balance;
    current->next=list; 
}
while(list!=NULL){
    current=list; 
    list=list->next; 
    current->next = new_list; 
    new_list= current; 
}

So, you're adding it to the head of the list and then trying to move it to the end? Save yourself the trouble and don't try to do that.

At least, that's what I think you're trying to do with that while loop. What it actually does is it breaks your list because you malloc a new_list node and then set current->next to this (uninitialized) node in the first iteration. Then, after the second iteration, the second node in the list (before the call to this function) points back to the old head of the list which is now pointing to an uninitialized node. If the loop doesn't continue after this point, you'll have two nodes which have no next pointers pointing to them (hence a broken list).

Instead, in the else, you should start with a pointer (call it foo) pointing to list. Then, keep checking foo's next pointer and update foo to be foo->next until foo->next is NULL. Then, set foo->next to be the new node.

Or, like I said, you can maintain a tail pointer which points to the last node in the list. If you have that, then you don't have to navigate to the end of the list. You can simply set tail->next to be the new node and then set tail to point to the new node.

1
2
return(new_list);                  
count3++;

And two things here: (1) count3 is passed by value so it won't have any effect on the caller's copy of count3, and (2) it's being updated after the return statement so it won't do anything regardless.
Last edited on
thanks you shacktar, for your reply. Can you give me an explain on how to make a tail pointer and an example on how to maintain tail pointer. Also you have suggested to start with a foo pointer. So should the foo pointer be after current->next=list' and then foo->list; and then foo->next=NULL.
Last edited on
Also you have suggested to start with a foo pointer. So should the foo pointer be after current->next=list' and then foo->list; and then foo->next=NULL.

I called it foo because it doesn't matter what you name it. Normally, when I traverse a linked list I call the pointer "current", though the name current was already taken, hence the name foo. I guess I could have called it "traversal".

First, the "traversal" pointer should point to list (and remember to not add current before list, because current will be added at the end). Then, "traversal" will point to list->next (if that's not NULL). Then, "traversal" will point to list->next->next, and so on until the next pointer of the node that "traversal" is pointing to is NULL. When traversal->next is NULL, you know you're at the end of the list. You can say traversal->next = current to add it at the end of the list.

Can you give me an explain on how to make a tail pointer and an example on how to maintain tail pointer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//our node which will be in a linked list. Here, data is an int, though a node
//can theoretically store anything
struct Node
{
    Node* next;
    int data;
};

//create the empty list

Node* head = NULL;
Node* tail = NULL;

//add a node to the list

Node* newNode = AllocateNode();
newNode->data = 2;
newNode->next = NULL;

head = tail = newNode;

//add a node to the head of the list

Node* newNodeAtHead = AllocateNode();
newNodeAtHead->data = 1;

newNodeAtHead->next = head;
head = newNodeAtHead;

//add a node at the end (tail) of the list

Node* newNodeAtTail = AllocateNode();
newNodeAtTail->data = 3;
newNodeAtTail->next = NULL;

tail->next = newNodeAtTail;
tail = newNodeAtTail;

//now the Node pointed to by head has data value 1
//the Node pointed to by head->next has data value 2
//and the Node pointed to by tail has data value 3 
THANKS FOR THE REPAY.
I have tried what you have said about putting traversal pointing to list in the else statement, but i really don't understand what you mean by keeping point traversal to next until traversal is null. what exactly i am pointing traversal at. THANKS YOU
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
else //list is not empty so we're going to add current to the end of the list
{
    //allocate current here and set its data members

    member_account* traversal = list;

    //navigate the list until we're pointing to the last node
    while(traversal->next != NULL)
    {
         traversal = traversal->next;
    }

    //traversal is pointing to the last node. Now, add current to the list
    traversal->next = current;
    current->next = NULL; //current is at the end of the list, so next is null
}
thanks
Last edited on
can someone please help me. I post above what i did but can't get it to work. What i did make sense to me but i just don't know why it is not working.
closed account (D80DSL3A)
The function looks like it should work. Are you expecting it to change the values of list and/or count3? It won't since those variables are passed by value.
Please show what you are doing in the main(). The problem may be there.
thanks for your replay fun2code. What i am trying to is add to the end of the list. so the following code above should add new members to the end of the list. What i did seems right to me but when i print the list out it only prints the member i entered but doesn't print the entire list along with the new member i have entered to the end.
Yeah, please post the function(s) that call add_to_end.

Again, count3 is being updated after the return statement and it is passed by value. If you want it to have an effect on the caller's copy of count3, then you should pass it by reference and update it before the return statement.

The fact that list is passed by value is a potential problem as well. In the case where list is NULL, if you don't set the caller's copy of list to be the returned value, then list will always be NULL (I'm assuming this isn't happening to you).
this is the entire code for main:
Last edited on
head= add_to_end(head, last, first, balance, count);

Here you're updating the head to be the last node in the list, thus losing access to every node but the end. Instead you should simply do this:

add_to_end(head, last, first, balance, count);

Also, there's no sense in passing in count if main is updating it itself. You can simply not pass in count and you'll have the same result (or, pass count by reference so main doesn't have extra work).
THANKS YOU, I have fixed it and it works.
Why did you remove your code?

http://www.cplusplus.com/articles/oGLN8vqX/
Pages: 12