Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我不能在链表中使用链表_C_Function_Linked List_Singly Linked List_Definition - Fatal编程技术网

C 我不能在链表中使用链表

C 我不能在链表中使用链表,c,function,linked-list,singly-linked-list,definition,C,Function,Linked List,Singly Linked List,Definition,当我开始学习链表时,我有了一个想法。我的想法是这样的。我想在链表中插入1,2,5。在链表的另一个字段中,我想添加以1,2,5开头的两位数。你们可以从图片上看到。(C)指组合) 我的节点结构: struct Node { int data; struct Node* next; struct Node* startsWith; }; 插入初始链表1、2和5的代码: void insertEnd(struct Node **pNode, int data){ struct Node* newNo

当我开始学习链表时,我有了一个想法。我的想法是这样的。我想在链表中插入1,2,5。在链表的另一个字段中,我想添加以1,2,5开头的两位数。你们可以从图片上看到。(C)指组合)

我的节点结构:

struct Node {
int data;
struct Node* next;
struct Node* startsWith; };
插入初始链表1、2和5的代码:

void insertEnd(struct Node **pNode, int data){
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
struct Node *lastNode=*pNode;
newNode->data=data;
newNode->next=NULL;
if (*pNode==NULL)
{
    *pNode=newNode;
    return;
}
while (lastNode->next!=NULL){
    lastNode=lastNode->next;
}
lastNode->next=newNode;
return; }
此部件为搜索编号,并在其startsWith节点中插入2位数字

void insertStartsWith(struct Node **pNode, int i, int i1) {
struct Node *tempNode=*pNode;
while (tempNode->next!=NULL){
    if (tempNode->data==i){
        struct Node *tempNode1=tempNode->startsWith;
        while (tempNode1->next!=NULL){
            tempNode1=tempNode1->next;
        }
        struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
        newNode->data=i1;
        newNode->next=NULL;
        tempNode1->next=newNode;
        return;
    }
    tempNode=tempNode->next;
} }
我将12和5添加到我的链接列表中。但当我尝试添加组合时,它失败了。在链接列表中,我应该在哪里查找链接列表

编辑08.18.19

void printLinkedListWithStartsWith(struct Node *pNode) {
printf("Linked list with starts: ");
while (pNode!=NULL) {
    printf("%d \n",pNode->data);
    while(pNode->startsWith!=NULL){
        printf("%d ",pNode->startsWith->data);
        pNode->startsWith=pNode->startsWith->next;
    }
    pNode=pNode->next;
} }

在函数
insertEnd
中,必须初始化结构
节点的所有数据成员

函数
insertStartsWith
具有未定义的行为。表达式
*pNode
可以等于
NULL
。所以在这个声明中

while (tempNode->next!=NULL){
试图使用空指针访问内存

而且这个while循环

while (tempNode->next!=NULL)
没有意义,因为对于第一个节点
tempNode->next
可以等于
NULL
,但函数在这种情况下不执行任何操作。它只是退出循环,然后将控件返回给调用函数

您需要做的是找到一个值为
i
的节点,如果找到了这样一个节点,则调用函数
insertEnd
将指针传递给所找到节点的数据成员
startsWith
,以及变量
ii
的值

函数可以按以下方式查看

int insertStartsWith(struct Node **pNode, int i, int i1) 
{
    while ( *pNode != NULL && ( *pNode )->data != i ) pNode = &( *pNode )->next;

    int success = *pNode != NULL;

    if ( success )
    {
        insertEnd( &( *pNode )->startsWith, ii );
    }

    return success;
}
如果未找到具有变量
i
值的节点,则另一种方法是创建此类节点。比如说

void insertStartsWith(struct Node **pNode, int i, int i1) 
{
    while ( *pNode != NULL && ( *pNode )->data != i ) pNode = &( *pNode )->next;

    if ( *pNode == NULL )
    {
         *pNode = malloc( sizeof( struct Node ) );
         ( *pNode )->data = i;
         ( *pNode )->next = NULL;
         ( *pNode )->startsWith = NULL;
    }

    insertEnd( &( *pNode )->startsWith, ii );
}

在函数
insertEnd
中,必须初始化结构
节点的所有数据成员

函数
insertStartsWith
具有未定义的行为。表达式
*pNode
可以等于
NULL
。所以在这个声明中

while (tempNode->next!=NULL){
试图使用空指针访问内存

而且这个while循环

while (tempNode->next!=NULL)
没有意义,因为对于第一个节点
tempNode->next
可以等于
NULL
,但函数在这种情况下不执行任何操作。它只是退出循环,然后将控件返回给调用函数

您需要做的是找到一个值为
i
的节点,如果找到了这样一个节点,则调用函数
insertEnd
将指针传递给所找到节点的数据成员
startsWith
,以及变量
ii
的值

函数可以按以下方式查看

int insertStartsWith(struct Node **pNode, int i, int i1) 
{
    while ( *pNode != NULL && ( *pNode )->data != i ) pNode = &( *pNode )->next;

    int success = *pNode != NULL;

    if ( success )
    {
        insertEnd( &( *pNode )->startsWith, ii );
    }

    return success;
}
如果未找到具有变量
i
值的节点,则另一种方法是创建此类节点。比如说

void insertStartsWith(struct Node **pNode, int i, int i1) 
{
    while ( *pNode != NULL && ( *pNode )->data != i ) pNode = &( *pNode )->next;

    if ( *pNode == NULL )
    {
         *pNode = malloc( sizeof( struct Node ) );
         ( *pNode )->data = i;
         ( *pNode )->next = NULL;
         ( *pNode )->startsWith = NULL;
    }

    insertEnd( &( *pNode )->startsWith, ii );
}

为链表的链表使用两个结构可以清楚地表明:

typedef结构列表;
typedef结构节点;
结构节点{
大小;
int值;
节点*节点;
列表*列表_值;
};
结构列表{
大小;
节点*节点;
};
您可以编写函数将数据插入列表:

//将列表插入listP
无效列表\设置\列表\值(列表*列表、大小\索引、列表*列表\值){
(*listP).node[index].list\u value=list\u valueP;
}
//将值插入listP
无效列表设置整型值(列表*列表,大小索引,整型值){
(*listP).node[index].value=valueP;
}

对于链表的链表,使用两个相互关联的结构可以清楚地说明:

typedef结构列表;
typedef结构节点;
结构节点{
大小;
int值;
节点*节点;
列表*列表_值;
};
结构列表{
大小;
节点*节点;
};
您可以编写函数将数据插入列表:

//将列表插入listP
无效列表\设置\列表\值(列表*列表、大小\索引、列表*列表\值){
(*listP).node[index].list\u value=list\u valueP;
}
//将值插入listP
无效列表设置整型值(列表*列表,大小索引,整型值){
(*listP).node[index].value=valueP;
}

您永远不会初始化
开始宽度
。这意味着
while(tempNode1->next!=NULL)
取消对未初始化指针的引用。如果您在此处正确初始化为
NULL
,则需要在取消引用之前检查
NULL
。“它失败”不是一个有用的描述。请阅读并告诉我们你的期望和你得到了什么。你怎么查的?顺便说一句,你有没有用startsWidth检查
printLinkedListWidth
的输出的debuffer中的值。哦,通过应用一些适当的缩进,你可以大大提高代码的可读性。你不应该在C中强制转换
malloc()
的返回值:你永远不会初始化
startsWidth
。这意味着
while(tempNode1->next!=NULL)
取消对未初始化指针的引用。如果您在此处正确初始化为
NULL
,则需要在取消引用之前检查
NULL
。“它失败”不是一个有用的描述。请阅读并告诉我们你的期望和你得到了什么。你怎么查的?顺便说一句,您是否检查了
printLinkedListWithStartsWith
仅检查输出的debuffer中的值。哦,通过应用一些适当的缩进,您可以大幅提高代码的可读性。您不应在C中强制转换
malloc()
的返回值: