在链表的开头插入新节点
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你忘记了一个案例。 则将调用您标记的行,
要了解第二种情况,请看一下之前的代码
: <代码>值> currentPtr->data 在第二种情况下为 true,因此您将到达带有
previousPtr == NULL
和*sPtr != NULL
的行(包含它的初始值,指向列表第一个节点的指针)。在第一种情况下,
*sPtr
确实是NULL
,在第二种情况下,当使用NULL
并结束时,您会错误地丢弃整个列表列表中只有一个字符并且存在内存泄漏。I think you forgot a case. The line you marked will be called if
To understand the second case, have a look at the code before:
The condition
value > currentPtr->data
is true in the second case, so you will arrive at the line withpreviousPtr == NULL
and*sPtr != NULL
(containing its initial value, the pointer to the first node of the list).In the first case,
*sPtr
isNULL
indeed, in the second case, you would incorrectly throw away the whole list when usingNULL
and end up with only one character in the list and a memory leak.您正在将 *sPtr 传递给该函数。如果 *sPtr 指向非空列表中的节点,并且使用 NULL 而不是 *sPtr,您将丢失对该列表的引用。如果 *sPtr 为 NULL,则行为相同。
您建议:
但如果 *sPtr = Node1 并且列表是:
如果您想在 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:
but if *sPtr = Node1 and the list is:
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.