C 链表填充空表
这是我的节点和列表,我想通过在每次迭代中创建一个新节点,将每个I值逐个添加到节点,但我的程序崩溃了,我不知道原因是什么C 链表填充空表,c,linked-list,C,Linked List,这是我的节点和列表,我想通过在每次迭代中创建一个新节点,将每个I值逐个添加到节点,但我的程序崩溃了,我不知道原因是什么 struct Node { int val; struct Node *next; }; struct List { struct Node *head; struct Node *tail; int size; }; typedef struct List *List; inti; 列出原产地; List myList=(struct List*)malloc(s
struct Node
{
int val;
struct Node *next;
};
struct List
{
struct Node *head;
struct Node *tail;
int size;
};
typedef struct List *List;
inti;
列出原产地;
List myList=(struct List*)malloc(sizeof(struct List));
myList->head=(结构节点*)malloc(sizeof(结构节点));
myList->head->next=NULL;
myList->tail=myList->head;
起源=myList;
对于(i=0;ihead->next=(结构节点*)malloc(sizeof(结构节点));
myList->head->next->val=i;
myList->head->next=myList->head->next->next;
myList->head->next->next=NULL;
}
要查找问题,我们可以添加一些调试输出:
for(i=0;ihead->next=(结构节点*)malloc(sizeof(结构节点));
printf(“myList->head->next=%p\n”,myList->head->next);
myList->head->next->val=i;
printf(“myList->head->next->next=%p\n”,myList->head->next->next);
myList->head->next=myList->head->next->next;
printf(“myList->head->next=%p\n”,myList->head->next);
如果(NULL==myList->head->next){
printf(“我们刚刚将下一个指针设置为NULL,所以现在应该停止。\n”);
出口(1);
}
myList->head->next->next=NULL;
}
我们得到这个输出:
myList->head->next=0x7f8062c05800
myList->head->next->next=0x0
myList->head->next=0x0
我们只是将下一个指针设置为NULL,所以现在应该停止。
我对初始化循环进行了以下更改:
struct Node*current=myList->head;
对于(i=0;inxt=(结构节点*)malloc(sizeof(结构节点));
当前->下一个->值=i;
当前->下一步->下一步=空;
当前=当前->下一步;
}
我还添加了一个输出循环,以验证初始化是否有效:
current=myList->head->next;
while(当前){
printf(“%p=%d\n”,当前,当前->值);
当前=当前->下一步;
}
输出
0x7fced6405800=0
0x7fced6405810=1
0x7fced6405820=2
0x7fced6405830=3
0x7fced6405840=4
注意
您没有对尾部指针进行任何操作,因此我将让您了解如何处理它。一些问题:
- 您根本没有遍历列表
- 您正在破坏列表
指针,而没有更新头
指针尾
- 您正在使用
作为虚拟节点。最好将其值设置为一些特殊标记,例如-1head
- 最好使用临时
节点进行遍历,使用temp
插入新节点,以避免未初始化的指针访问和分配newnode
main()
函数:
int i;
List origin;
List myList = (struct List*)malloc(sizeof(struct List));
myList->head = (struct Node*)malloc(sizeof(struct Node));
myList->head->next = NULL;
myList->tail = myList->head;
origin=myList;
for(i=0;i<5;i++)
{
myList->head->next = (struct Node*)malloc(sizeof(struct Node));
myList->head->next->val = i;
myList->head->next = myList->head->next->next;
myList->head->next->next = NULL;
}
intmain()
{
int i;
//列出原产地;
List myList=(struct List*)malloc(sizeof(struct List));
myList->head=(结构节点*)malloc(sizeof(结构节点));
myList->head->val=-1;/*为虚拟第一个节点设置一个值*/
myList->head->next=NULL;
myList->tail=myList->head;
//起源=myList;
结构节点*temp=myList->head;/*使用临时指针遍历*/
对于(i=0;ival=i;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;/*将临时头更新到新节点*/
myList->tail=newnode;/*将tail更新为最后一个节点*/
}
温度=myList->head;
while(temp!=NULL)
{
printf(“%d”,temp->val);
if(temp!=myList->tail)
printf(“->”);
温度=温度->下一步;
}
返回0;
}
结果:
int i;
List origin;
List myList = (struct List*)malloc(sizeof(struct List));
myList->head = (struct Node*)malloc(sizeof(struct Node));
myList->head->next = NULL;
myList->tail = myList->head;
origin=myList;
for(i=0;i<5;i++)
{
myList->head->next = (struct Node*)malloc(sizeof(struct Node));
myList->head->next->val = i;
myList->head->next = myList->head->next->next;
myList->head->next->next = NULL;
}
使用
free()释放malloc
ed内存时也要小心
一旦你完成列表。我也不明白列表来源的目的,所以我把那部分注释掉了。这能回答你的问题吗?不,它不能@davidcullen什么是struct ListRecord
类型?@swag2198键入错误,它应该是list only在myList->head->next->next>上仔细查看。它是什么myList->head->next->next
此时是否包含?它是一个未初始化的指针。