Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have coded the following method to add details to a linked list using pointers but I'm stuck at the final part of the method where I'm checking the condition of 'curr'.

if(curr-> newContact)
   {
       curr->next = newContact;
       newContact->prev = NULL;


   }
   else
   {
   curr->next = newContact;
   newContact->prev = curr;
   newContact->next = NULL;
   }

It doesn't add the contact.I'm not sure where I have gone wrong in the loop to check.This is the whole method.Anyone have any ideas?Thanks

C#
int addContact(struct contact *theList)
{
    struct contact *newContact, *curr;

    // create the new structure
    newContact = (struct contact *)malloc(sizeof(struct contact));
    if( newContact == NULL )
    {   // if true, then no memory left - oops
        return(0);
    }
    // find the end of the list
    curr = theList;
    // scroll through the list
    if(curr != NULL)
    {

        while( curr->next != NULL)
        {
            curr = curr->next;
        }

    }
    // now have the last contact
    // add the new one here.
    printf("\nEnter a surname: ");
    fflush(stdin);
    gets(newContact->sname);
    printf("\nEnter a first name: ");
    gets(newContact->fname);
    printf("\nEnter a phone: ");
    gets(newContact->phone);
    printf("\nEnter a company: ");
    gets(newContact->company);

    // need to hook the new contact to
    // end of list

    if(curr-> newContact)
    {
        curr->next = newContact;
        newContact->prev = NULL;


    }
    else
    {
    curr->next = newContact;
    newContact->prev = curr;
    newContact->next = NULL;
    }
    return(1);
}//add


This is the code where I call addContact:

C#
sts = addContact(head);
            sts = writeListToFile("test.csv",head);

            printf("\n\nUpdate Contact List");
            printf("\n%s,%s,%s,%s,%s",head->sname,head->fname,head->phone,head->company);
            head = head->next;
Posted
Updated 26-Apr-13 6:13am
v3

1 solution

First of, the argument passing of theList is wrong. In case that the list is still empty, you were expecting to pass NULL for theList. But how do you then return the new element that is being allocated in that call?

The solution is to pass
C++
int addContact(struct contact **theList)
{
    ...
    curr = *theList;

Now to the end of your function. What you want to test is whether curr is NULL, hence the list is still empty. In that case you want to place the pointer to the first element into the list anchor:
C++
if (curr == 0)
{
    *theList = newContact;
    newContact->next = NULL;
    newContact->prev = NULL:
}
else
...

By the way, I doubt that your code will compile. Is newContact really a member of you contact structure?

[AMENDED]
And your calling code should look like this:
C++
struct contact* ptrHead = 0;
...
addContact (&ptrHead);
...
writeListToFile ("test.csv", ptrHead);

What I was wondering about is: Why are keeping a doubly linked list, but have only a single head pointer? The advantage of a doulbe linked is that you can traverse it in both directions. An additional pointer to the last element in the list would make that easy and also support your append operation; it would save you from looping over the entire list to find the last element. But first let's find the little bug that keeps it from working.
 
Share this answer
 
v2
Comments
johnmolo 26-Apr-13 11:53am    
Hi,thanks for the feedback this problem is really setting me back.I tried the above suggestions to the code:

int addContact(struct contact **theList)
{
struct contact *newContact, *curr;

// create the new structure
newContact = (struct contact *)malloc(sizeof(struct contact));
if( newContact == NULL )
{ // if true, then no memory left - oops
return(0);
}
// find the end of the list
curr = *theList;
// scroll through the list
if(curr != NULL)
{

while( curr->next != NULL)
{
curr = curr->next;
}

}
// now have the last contact
// add the new one here.
printf("\nEnter a surname: ");
fflush(stdin);
gets(newContact->sname);
printf("\nEnter a first name: ");
gets(newContact->fname);
printf("\nEnter a phone: ");
gets(newContact->phone);
printf("\nEnter a company: ");
gets(newContact->company);

// need to hook the new contact to
// end of list

if (curr == 0)
{
*theList = newContact;
newContact->next = NULL;
newContact->prev = NULL;
}
else
{
curr->next = newContact;
newContact->prev = curr;
newContact->next = NULL;
}
return(1);
}//add

But I'm getting the same error.What exactly am I doing wrong here in the function that the list isn't being passed?
nv3 26-Apr-13 12:03pm    
My guess is that there is another problem in the code that calls addContact. Can you show that code as well. I suggest you add it to your question by using the green Improve Question button.
johnmolo 26-Apr-13 12:09pm    
ya sure I will add it now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900