在链表的开头插入新节点

发布于 2024-10-20 14:34:59 字数 1689 浏览 3 评论 0原文

在 C 上的简单链表实现中,我无法找出一行名为 insert() 的函数。 它需要一个字符并按字母顺序添加到链接列表中。 该行是关于当列表为空时创建一个新节点。由于列表上只有一个节点,因此该行应该像我评论的那样,我错了吗?

/****************************************************/

void insert( ListNodePtr *sPtr, char value ){
ListNodePtr newPtr;    
ListNodePtr previousPtr;
ListNodePtr currentPtr;

newPtr = malloc( sizeof( ListNode) );

if( newPtr != NULL ){       //is space available
    newPtr->data = value;       //place value in node
    newPtr->nextPtr = NULL;      //node does not link to another node

    previousPtr = NULL;
    currentPtr = *sPtr;         //indirection to startPtr

    while( currentPtr != NULL && value > currentPtr->data ){
        previousPtr = currentPtr;               //walk to ...
        currentPtr = currentPtr->nextPtr;       //... next node
    }

    //insert new node at the beginning of the list
    if( previousPtr == NULL ){
        newPtr->nextPtr = *sPtr;            ///////////////////////////////////////////////  newPtr->nextPtr = NULL   ???
        *sPtr = newPtr;
    }
    else{           //insert new node between previousPtr and currentPtr
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    }

}
else
    printf( "%c not inserted. No memory available.\n", value);
}//end-of insert

/*******************************************************/

main() 中的 typedef 指令是;

typedef struct listNode ListNode;
typedef ListNode* ListNodePtr;

在 main() 中调用函数 insert() ,如下所示;

insert( &startPtr, item);

main()中startPointer的初始化;

ListNodePtr startPtr = NULL;

In a simple Linked List implementation on C, I couldn’t figure out a line of function named insert().
It takes a char and add to the linked list in alphabetical order.
The line is about creating a new node when the list is empty. And since there will be only one node on the list, the line should be like I’ve commented, am I wrong?

/****************************************************/

void insert( ListNodePtr *sPtr, char value ){
ListNodePtr newPtr;    
ListNodePtr previousPtr;
ListNodePtr currentPtr;

newPtr = malloc( sizeof( ListNode) );

if( newPtr != NULL ){       //is space available
    newPtr->data = value;       //place value in node
    newPtr->nextPtr = NULL;      //node does not link to another node

    previousPtr = NULL;
    currentPtr = *sPtr;         //indirection to startPtr

    while( currentPtr != NULL && value > currentPtr->data ){
        previousPtr = currentPtr;               //walk to ...
        currentPtr = currentPtr->nextPtr;       //... next node
    }

    //insert new node at the beginning of the list
    if( previousPtr == NULL ){
        newPtr->nextPtr = *sPtr;            ///////////////////////////////////////////////  newPtr->nextPtr = NULL   ???
        *sPtr = newPtr;
    }
    else{           //insert new node between previousPtr and currentPtr
        previousPtr->nextPtr = newPtr;
        newPtr->nextPtr = currentPtr;
    }

}
else
    printf( "%c not inserted. No memory available.\n", value);
}//end-of insert

/*******************************************************/

the typedef instructions in main() are;

typedef struct listNode ListNode;
typedef ListNode* ListNodePtr;

and the function insert() is called in main() like this;

insert( &startPtr, item);

initialization of startPointer in main();

ListNodePtr startPtr = NULL;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合久必婚 2024-10-27 14:34:59

我想你忘记了一个案例。 则将调用您标记的行,

  • 如果列表为空,
  • 该字符比列表中的所有其他字符都小,并且必须插入到列表的开头

要了解第二种情况,请看一下之前的代码

while( currentPtr != NULL && value > currentPtr->data ){
    previousPtr = currentPtr;               //walk to ...
    currentPtr = currentPtr->nextPtr;       //... next node
}

: <代码>值> currentPtr->data 在第二种情况下为 true,因此您将到达带有 previousPtr == NULL*sPtr != NULL 的行(包含它的初始值,指向列表第一个节点的指针)。

在第一种情况下,*sPtr确实是NULL,在第二种情况下,当使用NULL并结束时,您会错误地丢弃整个列表列表中只有一个字符并且存在内存泄漏。

I think you forgot a case. The line you marked will be called if

  • the list is empty
  • the character is smaller than all other characters in the list and has to be inserted at the beginning of the list

To understand the second case, have a look at the code before:

while( currentPtr != NULL && value > currentPtr->data ){
    previousPtr = currentPtr;               //walk to ...
    currentPtr = currentPtr->nextPtr;       //... next node
}

The condition value > currentPtr->data is true in the second case, so you will arrive at the line with previousPtr == NULL and *sPtr != NULL (containing its initial value, the pointer to the first node of the list).

In the first case, *sPtr is NULL indeed, in the second case, you would incorrectly throw away the whole list when using NULL and end up with only one character in the list and a memory leak.

鸵鸟症 2024-10-27 14:34:59

您正在将 *sPtr 传递给该函数。如果 *sPtr 指向非空列表中的节点,并且使用 NULL 而不是 *sPtr,您将丢失对该列表的引用。如果 *sPtr 为 NULL,则行为相同。

您建议:

if( previousPtr == NULL ){
        newPtr->nextPtr = NULL;
        *sPtr = newPtr;
    }

但如果 *sPtr = Node1 并且列表是:

Node1->Node2->Node3

如果您想在 Node1 之前插入并且使用您的实现,

您将使 newPtr->指向NULL
然后设置 *sPtr = newPtr 并丢失原始列表,

其他实现将新节点添加到旧列表中。

You are passing in a *sPtr to the function. If *sPtr points to a Node in a non-empty list, you will lose your reference to the list if you use NULL instead of *sPtr. If *sPtr is NULL the behavior is the same.

You are suggesting:

if( previousPtr == NULL ){
        newPtr->nextPtr = NULL;
        *sPtr = newPtr;
    }

but if *sPtr = Node1 and the list is:

Node1->Node2->Node3

if you want to insert before Node1 and you use your implementation

you will make your newPtr-> point to NULL
and then set your *sPtr = newPtr and lose your original list

the other implementation prepends your new node to the old list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文