我如何...链接C中的链接列表 [英] How do I...link linked lists in C
问题描述
我正在尝试创建(有序)链接列表的(有序)链接列表。 list-of-list链接由其成员列表的第一个节点承载。我试图通过以下代码实现这一点,但是当我尝试显示第二个列表时,我的程序崩溃了。第一个列表显示完美。
这是我想要构建的数据结构的示意图:构造的图像
请给我一个解决方案,我已尝试过使用指向下一个列表的列表等所有内容还尝试创建另一个节点指针,第一个节点作为第一个列表的第一个节点。我也尝试插入链接列表第一个节点列表。
请解决问题并尽快帮助我。
代码:
I am trying to create an (ordered) linked list of (ordered) linked lists. The list-of-list links are carried by the first nodes of its member lists. I am trying to achieve this via the following code, but my program crashes when I try displaying the second list. First list displays perfectly.
Here's a schematic of the data structure I am trying to construct: Image of construct
Please give me a solution, I have tried everything from using lists pointing to next list and so on and also trying to create another node pointer with first node as first node of first list. I also tried insertion at end for list of linked lists first nodes.
Please fix the problem and help me asap.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node{
int number;
struct node*next;
struct node*lsnext;
};
typedef struct node Node;
Node* insertValue(Node * list, int value);
void display(Node*);
Node* insertArr(Node * list, int value);
Node* addNodeBottom(int val, Node *start);
int main()
{
Node *globalList = NULL, *lists,*start,*save;
int nbrOfLists, listNo, nbrOfVal, valNo, val;
start=NULL;
printf("\n Enter the number of lists:");
scanf("%d", &nbrOfLists);
if(nbrOfLists < 0)
return -1;
for(listNo = 0; listNo < nbrOfLists; listNo++)
{
printf("\n\n Enter the number of inputs to the list %d: \n ",listNo+1);
scanf("%d", &nbrOfVal);
lists = NULL;
for(valNo = 0; valNo < nbrOfVal; valNo++)
{
printf("Enter node value %d:", valNo+1);
scanf("%d", &val);
// Here we insert the value in both lists
lists= insertValue(lists, val);
globalList = insertValue(globalList, val);
}
start=addNodeBottom(val,lists);
if(listNo==0){
save=start;
}
printf("\n The list %d is: ",listNo+1);
display(lists);
}
printf("\n\n The final list is: ");
display(globalList);
printf("The first list is");
display(save);
printf("The second list is");
display(save->lsnext); //crashes here
return 0;
}
Node* insertValue(Node * list, int value) //to insert node at the end
{
Node *newNode, *m;
newNode = malloc(sizeof(Node));
newNode->number=value;
if(list == NULL)
{
newNode->next=NULL;
return newNode;
}
if(value < list->number)
{
newNode->next = list;
return newNode;
}
m = list;
while(m->next)
{
if(value < m->next->number)
break;
m = m->next;
}
newNode->next = m->next;
m->next = newNode;
return list;
}
Node* addNodeBottom(int val, Node *start)
{
Node*rear;
Node* node1=(Node*) malloc(sizeof(Node));
node1->number=val;
node1->lsnext=NULL;
if(start==NULL){
start=node1;
}
else{
start->lsnext=node1;
start=node1;
}
return start;
}
void display(Node*nodex){
while(nodex)
{
printf("%d ->",nodex->number);
nodex=nodex->next;
}
}
推荐答案
addNodeBottom
函数破了:在里面,在陈述中
TheaddNodeBottom
function is broken: inside it, in the statements
rear->lsnext=node1;
rear=node1;
您使用未初始化的值后面
。
这篇关于我如何...链接C中的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!