双链表输出验证 [英] Doubly linked list output validation

查看:58
本文介绍了双链表输出验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下方法,使用指针将链接列表添加到链接列表中但是我仍然停留在方法的最后部分,我正在检查curr的状态。



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

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





这是我调用addContact的代码:





This is the code where I call addContact:

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;

推荐答案

首先,传递列表的参数是错误的。如果列表仍为空,则您希望为List传递NULL。但是你如何返回在该调用中分配的新元素?



解决方案是通过

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
int addContact(struct contact **theList)
{
    ...
    curr = *theList;



现在到功能结束。您要测试的是curr是否为NULL,因此列表仍为空。在这种情况下,您希望将指向第一个元素的指针放入列表锚点:


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:

if (curr == 0)
{
    *theList = newContact;
    newContact->next = NULL;
    newContact->prev = NULL:
}
else
...



顺便说一句,我怀疑你的代码是否会编译。 newContact真的是你联系结构的成员吗?



[修改]

你的主叫代码应如下所示:


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:

struct contact* ptrHead = 0;
...
addContact (&ptrHead);
...
writeListToFile ("test.csv", ptrHead);



我想知道的是:为什么要保留一个双向链表,但只有一个头指针? doulbe链接的优点是你可以在两个方向上遍历它。指向列表中最后一个元素的附加指针可以简化并支持您的追加操作;它可以帮助您避免循环遍历整个列表以查找最后一个元素。但首先让我们找到阻止它工作的小虫。


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.


这篇关于双链表输出验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆