添加和排序在C链表 [英] Adding and sorting a linked list in C
问题描述
在我的任务,我必须写一个函数,作为参数指向一个叶子节点的结构和一个整型参数。然后,我不仅要添加到整数链表,但也把它的地方,这样的列表是正确的升序排列。我试着在这几款各种尝试,这是我的code作为发布的。
叶子节点*的AddItem(叶子节点* headPtr,INT的newitem)
{
自叶子节点* PTR = headPtr; PTR =的malloc(sizeof的(叶子节点)); 如果(headPtr == NULL)
{
ptr->值=的newitem;
ptr->接下来= headPtr;
返回PTR;
}
其他
{ 而(headPtr-> VALUE>的newitem || ptr->!下次= NULL)
{
的printf(当\\ n); //这简直让我知道多少次的循环运行
headPtr = headPtr->接下来,
}
ptr->值=的newitem;
ptr->接下来= headPtr;
返回PTR;
}} //的AddItem结束
当我运行它,并尝试插入说5,然后3分,5个被插入,但随后的while循环运行一次,我得到一个分段错误。
此外,我不能改变参数,因为它是一个骨骼code这个项目的一部分。由于任何人谁可以提供帮助。
如果它有助于这是结构的样子
typedef结构叶子节点
{
int值;
结构叶子节点*接下来的;}叶子节点;
在你的循环
而(headPtr-> VALUE>的newitem || ptr->!下次= NULL)
{
的printf(当\\ n); //这简直让我知道多少次的循环运行
headPtr = headPtr->接下来,
您检查一下是否未初始化 ptr->接下来
(不) NULL
。那是不理智的,可能会导致混乱,如果或许在那一部分的位模式 PTR
不是一个 NULL
指针,因为你有一个 ||
中的条件,然后循环条件永远是真实的你运行了你的列表的末尾。
的 ||
是错误的,无论如何,你需要一个&放大器;&安培;
,两个条件必须持有,东西是不是 NULL
和价值观之间的关系必须持有
此外,由于要在升序排列的列表中,您必须遍历列表,而该列表中的值的小的比要被插入的值。
但你必须前停止指向的值变大或比新的值越大,因为你必须修改节点的接下来
指针之后你插入新的价值。
如果(headPtr-> VALUE> =的newitem){
ptr->值=的newitem;
ptr->接下来= headPtr;
返回PTR;
}
而(headPtr->接下来= NULL&放大器;!&安培; headPtr->下一步 - > VALUE<的newitem){
headPtr = headPtr->接下来,
}
//现在headPtr是它的后面的新产品要插入的节点
ptr->值=的newitem;
ptr->接下来= headPtr->接下来,
headPtr->接下来= PTR;
返回?
但你会回到什么指针?如果新的项是在列表中的第一个,则返回一个指针到第一节点。如果你也想这样做,如果新项目是后来插入,你需要保持(副本)原 headPtr
并返回。
In my assignment, I have to write a function that takes as arguments a pointer to a "LNode" structure and an integer argument. Then, I have to not only add that integer into the linked list, but also put place it so that the list is in proper ascending order. I've tried several various attempts at this, and this is my code as of posting.
LNode* AddItem(LNode *headPtr, int newItem)
{
auto LNode *ptr = headPtr;
ptr = malloc(sizeof(LNode));
if (headPtr == NULL)
{
ptr->value = newItem;
ptr->next = headPtr;
return ptr;
}
else
{
while (headPtr->value > newItem || ptr->next != NULL)
{
printf("While\n"); // This is simply to let me know how many times the loop runs
headPtr = headPtr->next;
}
ptr->value = newItem;
ptr->next = headPtr;
return ptr;
}
} // end of "AddItem"
When I run it, and try to insert say a 5 and then a 3, the 5 gets inserted, but then the while loop runs once and I get a segmentation fault.
Also I cannot change the arguments as it's part of a skeletal code for this project. Thanks to anyone who can help.
If it helps this is what the structure looks like
typedef struct LNode
{
int value;
struct LNode *next;
} LNode;
In your loop
while (headPtr->value > newItem || ptr->next != NULL)
{
printf("While\n"); // This is simply to let me know how many times the loop runs
headPtr = headPtr->next;
you check whether the uninitialised ptr->next
is (not) NULL
. That is not sensible and may cause havoc if perchance the bit pattern in that part of ptr
is not that of a NULL
pointer, because you have an ||
in the condition, then the loop condition is always true and you run off the end of your list.
The ||
is wrong anyway, you need a &&
, both conditions must hold, something is not NULL
and a relation among the values must hold.
And, since you want the list in ascending order, you must traverse the list while the value in the list is smaller than the to-be-inserted value.
But you must stop before the pointed-to value becomes as large as or larger than the new value, because you must modify the next
pointer of the node after which you insert the new value.
if (headPtr->value >= newItem) {
ptr->value = newItem;
ptr->next = headPtr;
return ptr;
}
while(headPtr->next != NULL && headPtr->next->value < newItem) {
headPtr = headPtr->next;
}
// Now headPtr is the node behind which the new item is to be inserted
ptr->value = newItem;
ptr->next = headPtr->next;
headPtr->next = ptr;
return ??
But what pointer will you return? If the new Item is the first in the list, you return a pointer to the first node. If you also want to do that if the new item is inserted later, you need to keep (a copy of) the original headPtr
and return that.
这篇关于添加和排序在C链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!