制作自动排序插入函数 #包括 #包括 typedef int元素; 类型定义结构列表节点{ 元素数据; 结构ListNode*链接; }列表节点; ListNode*temp; int get_长度(ListNode*nodetype){ int i; for(i=0;节点类型!=NULL;i++){ 节点类型=节点类型->链接; } 返回i; } 无效添加(ListNode*listtype,element-elementtype){ listtype=(ListNode*)malloc(sizeof(ListNode)); listtype->data=elementtype; listtype=listtype->link; } 无效显示(ListNode*nodetype){ for(int i=0;nodetype!=NULL;i++){ printf(“%d”,节点类型->数据); 节点类型=节点类型->链接; } } int main(){ ListNode*list1=NULL; 添加(列表1、3); 添加(列表1、3); printf(“%d\n”,获取长度(列表1)); 显示(列表1); }
当我有三个因素(ListNode**phead,ListNode*p,ListNode*new_node)时,程序并没有犯任何错误。但我必须只使用“列表”因子和“项目”因子来遵守原始要求。由于“get_length”函数和“display”函数在以前的代码中运行良好,我认为“add”函数似乎有错误。制作自动排序插入函数 #包括 #包括 typedef int元素; 类型定义结构列表节点{ 元素数据; 结构ListNode*链接; }列表节点; ListNode*temp; int get_长度(ListNode*nodetype){ int i; for(i=0;节点类型!=NULL;i++){ 节点类型=节点类型->链接; } 返回i; } 无效添加(ListNode*listtype,element-elementtype){ listtype=(ListNode*)malloc(sizeof(ListNode)); listtype->data=elementtype; listtype=listtype->link; } 无效显示(ListNode*nodetype){ for(int i=0;nodetype!=NULL;i++){ printf(“%d”,节点类型->数据); 节点类型=节点类型->链接; } } int main(){ ListNode*list1=NULL; 添加(列表1、3); 添加(列表1、3); printf(“%d\n”,获取长度(列表1)); 显示(列表1); },c,C,当我有三个因素(ListNode**phead,ListNode*p,ListNode*new_node)时,程序并没有犯任何错误。但我必须只使用“列表”因子和“项目”因子来遵守原始要求。由于“get_length”函数和“display”函数在以前的代码中运行良好,我认为“add”函数似乎有错误。 我必须使用排序插入函数“add”来解决这个问题 除了您的帖子评论中提到的项目外,还需要进行一些其他必要的修改: 在添加(,)功能中,要创建新链接,请更改原始链接: #include <stdi
我必须使用排序插入函数“add”来解决这个问题 除了您的帖子评论中提到的项目外,还需要进行一些其他必要的修改: 在
添加(,)
功能中,要创建新链接,请更改原始链接:
#include <stdio.h>
#include <malloc.h>
typedef int element;
typedef struct ListNode {
element data;
struct ListNode *link;
} ListNode;
ListNode *temp;
int get_length(ListNode *nodetype) {
int i;
for (i = 0; nodetype != NULL; i++) {
nodetype = nodetype->link;
}
return i;
}
void add(ListNode *listtype, element elementtype){
listtype = (ListNode *)malloc(sizeof(ListNode));
listtype->data= elementtype;
listtype = listtype->link;
}
void display(ListNode *nodetype) {
for (int i=0; nodetype != NULL; i++) {
printf("%d ", nodetype->data);
nodetype = nodetype->link;
}
}
int main() {
ListNode *list1=NULL;
add(list1, 3);
add(list1, 3);
printf("%d\n", get_length(list1));
display(list1);
}
致:
另外,在get_length()
函数中,更改以下行:
//create new link in function: newNode, modify its members,
//then set original listtype == newNode
void add(ListNode **listtype, element elementtype)
{
ListNode *newNode = calloc(1, sizeof (*newNode)); // add this new Node
if(newNode)//test that calloc is successful
{
newNode->data = elementtype;
newNode->link = *listtype;//these lines
(*listtype) = newNode; //add new link to original
}
}
致:
最后,在main
中,更改:
nodetype = nodetype->link;
致:
注意: 我很久没有测试过这段代码,也没有用C编写代码了 说明 首先,让我们修复add函数 它应该检查列表是否为空,如果为空,则添加一个新节点。如果列表不是空的,那么它应该遍历它,直到找到最后一个节点,创建一个新节点,并将其附加到它的末尾
int main(void) { //Note minimum signature of main function
ListNode *list1=NULL;
add(&list1, 3);//pass address of object, not object itself
add(&list1, 3);
现在我们已经修复了add方法,我们可以为insert方法创建逻辑,它与add方法类似,但具有有序逻辑
基本上,如果列表不存在,我们会创建它。我们遍历整个列表,直到到达它的末尾,或者到达包含我们正在搜索的数据的元素。如果我们找到了数据,那么我们将该节点设置为temp(这样我们就不会丢失它),将新节点的链接指向temp节点,将当前节点的链接指向新节点。如果我们到达列表的末尾,但没有找到数据,那么我们将在列表的末尾添加新节点
void add(ListNode *listNode, element data){
// The list is non-existent! Create it.
if (listNode == null){
// Create new node
ListNode* newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->data = data;
newNode->link = null;
listNode = newNode;
return;
}
// We always want to ensure that the pointer to the first element
// of your list doesn't get override.
// Therefore, we create a new pointer which hold that address.
ListNode *currentNode = listNode;
// Traverse the linked list until finding last element.
while(currentNode->link != null){
currentNode = currentNode->Link;
}
// Create new node
ListNode* newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->data = data;
newNode->link = null;
// Attach new node to linked list
currentNode->link = newNode;
}
void插入(ListNode*ListNode,元素数据){
//列表不存在!请创建它。
if(listNode==null){
//创建新节点
ListNode*newNode=(ListNode*)malloc(sizeof(ListNode));
新建节点->数据=数据;
newNode->link=null;
listNode=newNode;
返回;
}
//我们总是希望确保指针指向第一个元素
//您列表中的未被覆盖。
//因此,我们创建了一个新指针来保存该地址。
ListNode*currentNode=ListNode;
//遍历链表,直到找到最后一个元素。
//在这里播放>=,>,=数据<数据){
currentNode=currentNode->link;
}
//创建新节点
ListNode*newNode=(ListNode*)malloc(sizeof(ListNode));
新建节点->数据=数据;
newNode->link=null;
//插入正确的位置
如果(当前节点->链接!=null){
ListNode*tempNode=currentNode->link;
currentNode->link=tempNode;
currentNode->link=newNode;
返回;
}
//将新节点附加到链接列表
currentNode->link=newNode;
}
请记住,C按值传递所有参数。这意味着来自main
函数的指针list1
将被复制到listtype
参数中。修改副本不会修改原件。首先想想那个明显的错误。你是说我应该在主函数中将list1修改为&list1吗?坦率地说。您需要通过引用将指针传递到链表中的第一个元素。在C语言中,这意味着指向指针的指针。因此,您的add()
函数应该具有原型add(Listnode**list,element)
。是的,然后调用add(&list1,2);添加(&list2,3)代码>在main()
中。在函数中,您不能接受list==NULL
,因为这意味着调用者没有提供对列表的任何引用。然后,指向函数中第一个元素的指针是(*list)
,您可以对其进行修改。记住还要检查malloc()
是否失败。
int main() {
ListNode *list1=NULL;
add(list1, 3);
add(list1, 3);
int main(void) { //Note minimum signature of main function
ListNode *list1=NULL;
add(&list1, 3);//pass address of object, not object itself
add(&list1, 3);
void add(ListNode *listNode, element data){
// The list is non-existent! Create it.
if (listNode == null){
// Create new node
ListNode* newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->data = data;
newNode->link = null;
listNode = newNode;
return;
}
// We always want to ensure that the pointer to the first element
// of your list doesn't get override.
// Therefore, we create a new pointer which hold that address.
ListNode *currentNode = listNode;
// Traverse the linked list until finding last element.
while(currentNode->link != null){
currentNode = currentNode->Link;
}
// Create new node
ListNode* newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->data = data;
newNode->link = null;
// Attach new node to linked list
currentNode->link = newNode;
}
void insert(ListNode *listNode, element data){
// The list is non-existent! Create it.
if (listNode == null){
// Create new node
ListNode* newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->data = data;
newNode->link = null;
listNode = newNode;
return;
}
// We always want to ensure that the pointer to the first element
// of your list doesn't get override.
// Therefore, we create a new pointer which hold that address.
ListNode *currentNode = listNode;
// Traverse the linked list until finding last element.
// Play here with >=, >, =<, <, or any other order combination.
while(currentNode->link != null
&& currentNode->data < data){
currentNode = currentNode->link;
}
// Create new node
ListNode* newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->data = data;
newNode->link = null;
// insert in the correct position
if (currentNode->link != null){
ListNode* tempNode = currentNode->link;
currentNode->link = tempNode;
currentNode->link = newNode;
return;
}
// Attach new node to linked list
currentNode->link = newNode;
}