我无法在链表中使用链表
I can not work with linked list inside linked list
当我开始学习链表时,我有了一个想法。我的想法是这样的。我想将 1,2,5 插入到链表中。在链表的另一个字段中,我想添加以 1、2、5 开头的 2 位数字。你可以从图像中看到。 (C 表示组合)
https://imgur.com/a/zmEqfPO
int main() {
struct Node* head = NULL;
insertEnd(&head,1);
insertEnd(&head,2);
insertEnd(&head,5);
printLinkedList(head);
insertStartsWith(&head,5,51);
insertStartsWith(&head,5,52);
insertStartsWith(&head,5,53);
insertStartsWith(&head,5,54);
insertStartsWith(&head,1,11);
insertStartsWith(&head,1,12);
insertStartsWith(&head,1,13);
insertStartsWith(&head,1,14);
insertStartsWith(&head,2,21);
insertStartsWith(&head,2,22);
insertStartsWith(&head,2,23);
insertStartsWith(&head,2,24);
printLinkedListWithStartsWith(head);}
我的节点结构:
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;
} }
我将 1 2 和 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
中你必须初始化结构Node
的所有数据成员。
函数 insertStartsWith
具有未定义的行为。表达式 *pNode
可以等于 NULL
。所以在这个语句中
while (tempNode->next!=NULL){
正在尝试使用空指针访问内存。
而且这个while循环
while (tempNode->next!=NULL)
没有意义,因为对于第一个节点 tempNode->next
可以等于 NULL
但函数在这种情况下不执行任何操作。它只是退出循环 returns 调用函数的控制。
您需要做的是找到一个值为 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 struct List List;
typedef struct Node Node;
struct Node{
size_t size;
int value;
Node* node;
List *list_value;
};
struct List{
size_t size;
Node* node;
};
并且您可以编写将数据插入列表的函数:
//Inserts list into listP
void list_set_list_value(List* listP, size_t index, List * list_valueP){
(*listP).node[index].list_value=list_valueP;
}
//Inserts value into listP
void list_set_int_value(List* listP, size_t index, int valueP){
(*listP).node[index].value=valueP;
}
当我开始学习链表时,我有了一个想法。我的想法是这样的。我想将 1,2,5 插入到链表中。在链表的另一个字段中,我想添加以 1、2、5 开头的 2 位数字。你可以从图像中看到。 (C 表示组合) https://imgur.com/a/zmEqfPO
int main() {
struct Node* head = NULL;
insertEnd(&head,1);
insertEnd(&head,2);
insertEnd(&head,5);
printLinkedList(head);
insertStartsWith(&head,5,51);
insertStartsWith(&head,5,52);
insertStartsWith(&head,5,53);
insertStartsWith(&head,5,54);
insertStartsWith(&head,1,11);
insertStartsWith(&head,1,12);
insertStartsWith(&head,1,13);
insertStartsWith(&head,1,14);
insertStartsWith(&head,2,21);
insertStartsWith(&head,2,22);
insertStartsWith(&head,2,23);
insertStartsWith(&head,2,24);
printLinkedListWithStartsWith(head);}
我的节点结构:
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;
} }
我将 1 2 和 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
中你必须初始化结构Node
的所有数据成员。
函数 insertStartsWith
具有未定义的行为。表达式 *pNode
可以等于 NULL
。所以在这个语句中
while (tempNode->next!=NULL){
正在尝试使用空指针访问内存。
而且这个while循环
while (tempNode->next!=NULL)
没有意义,因为对于第一个节点 tempNode->next
可以等于 NULL
但函数在这种情况下不执行任何操作。它只是退出循环 returns 调用函数的控制。
您需要做的是找到一个值为 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 struct List List;
typedef struct Node Node;
struct Node{
size_t size;
int value;
Node* node;
List *list_value;
};
struct List{
size_t size;
Node* node;
};
并且您可以编写将数据插入列表的函数:
//Inserts list into listP
void list_set_list_value(List* listP, size_t index, List * list_valueP){
(*listP).node[index].list_value=list_valueP;
}
//Inserts value into listP
void list_set_int_value(List* listP, size_t index, int valueP){
(*listP).node[index].value=valueP;
}