C 链接插入函数
你好,我正在尝试创建n个列表。每个列表都是带有新实例的有序链表。但是,我还需要一个组合有序链表。下面是相同的代码。当涉及到每个独立列表时,我没有任何问题。然而,当我试图将每个节点插入到全局声明的链表节点中时,我得到的错误是输入会随着一个节点值的迭代而崩溃。请查看突出显示的函数和调用,以明确错误的确切位置。请记下全球声明。提前谢谢C 链接插入函数,c,linked-list,C,Linked List,你好,我正在尝试创建n个列表。每个列表都是带有新实例的有序链表。但是,我还需要一个组合有序链表。下面是相同的代码。当涉及到每个独立列表时,我没有任何问题。然而,当我试图将每个节点插入到全局声明的链表节点中时,我得到的错误是输入会随着一个节点值的迭代而崩溃。请查看突出显示的函数和调用,以明确错误的确切位置。请记下全球声明。提前谢谢 #include <stdio.h> #include <stdlib.h> #include <malloc.h> struct
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node{
int number;
struct node*next;
};
typedef struct node NODE;
NODE*createlist(int),*rel_strt,*new_entry[100];
NODE *start1=NULL,*rear1,*m1,*temp11;
NODE *insert_in_end(NODE*node1);
int i,num,n2,j,v,u;
int main() {
v=0;u=0;
printf("\n Enter the number of lists:");
scanf("%d",&n2);
for(j=0;j<n2;j++) {
printf("\n\n Enter the number of inputs to the list %d: \n ",j+1);
scanf("%d",&num);
new_entry[v]=createlist(num);
display(new_entry[v]);
v++;
}
printf("\nThe final List is:");
display(start1);
return 0;
}
NODE*insert_in_end(NODE *node11) { //problem in this function
int inf1=node11->number;
if(start1==NULL) {
start1=rear1=node11;
} else {
m1=start1;
if(m1->number>inf1) {
node11->next=m1;
start1=node11;
} else {
while(m1->next) {
if(m1->next->number < inf1)
m1=m1->next;
else
break;
}
temp11=m1->next;
m1->next=node11;
node11->next=temp11;
}
}
return start1;
}
void display(NODE*nodex) {
while(nodex!=NULL) {
printf("%d ->",nodex->number);
nodex=nodex->next;
}
}
NODE *createlist(int n1) {
NODE *node1,*start,*rear,*m,*temp1;
start=NULL;
for(i=0;i<n1;i++) {
node1=(NODE*) malloc(sizeof(NODE));
int inf;
printf("Enter node value %d:",i+1);
scanf("%d",&inf);
node1->number=inf;
node1->next=NULL;
insert_in_end(node1); // error during second fuction call check function definition
if(start==NULL) {
start=rear=node1;
} else {
m=start;
if(m->number>inf) {
node1->next=m;
start=node1;
} else {
while(m->next) {
if(m->next->number < inf)
m=m->next;
else
break;
}
temp1=m->next;
m->next=node1;
node1->next=temp1;
}
}
}
return start;
}
我建议您定义一个函数insertValue,它将使您更加方便。它还将使您的代码更具可读性。 函数在下面,您应该看到它非常接近您所拥有的功能
// Returns the new start of the list after inserting value in increasing order
Node* insertValue(Node * list, int value)
{
Node *newNode, *m;
newNode = malloc(sizeof(NODE));
newNode->number = value; // added this
newNode->next = NULL; // added this
if(list == 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类型的变量指针nx1,并尝试在调用函数和使用nx1作为参数之前将node1值复制到它。这是正确的方法。请注意,您首先必须malloc一个新节点来保存副本。然后复制节点字段。完成后,在全局列表中插入节点副本。只需一个函数即可在有序链表中插入数字。然后,您可以对短列表和全局列表使用相同的函数。这个函数需要在链表的第一个元素和要插入的数字上有一个指针。为什么你有一个后指针?这是要求的吗?不需要。@chmike您能给我一个与我的案例相关的插入节点函数的例子吗。非常感谢,非常感谢,先生。然而,当我使用display函数时,我得到了无限的输出。请你能把你的答案附加上显示功能。那太好了。这是我编译的程序:函数insertValue中有一个错误。我没有初始化newNode。我正在修改答案来解决这个问题。
int main(int argc, char* argv[])
{
NODE *globalList = NULL, *lists[100];
int nbrOfLists, listNo, nbrOfVal, valNo, val;
printf("\n Enter the number of lists:");
scanf("%d", &nbrOfLists);
if(nbrOfLists < 0 || nbrOfLists > 100)
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[listNo] = 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[listNo] = insertValue(lists[listNo], val);
globalList = insertValue(globalList, val);
}
display(lists[listNo]);
}
display(globalList);
return 0;
}
void display(NODE * list)
{
while(list)
{
printf("%d ->", list->number);
list = list->next;
}
}