C 获取链表大小时的分段错误
调用函数getLength时出现分段错误。我编辑了代码, 现在我得到的长度是0,而不是5C 获取链表大小时的分段错误,c,C,调用函数getLength时出现分段错误。我编辑了代码, 现在我得到的长度是0,而不是5 #include <stdio.h> #include <stdlib.h> node *headptr; node *topptr; typedef struct node { int value; struct node *nextPtr; }node; void initializeLinkedList(node *headptr, nod
#include <stdio.h>
#include <stdlib.h>
node *headptr;
node *topptr;
typedef struct node
{
int value;
struct node *nextPtr;
}node;
void initializeLinkedList(node *headptr, node *topptr)
{
int i=0;
headptr = (node*)malloc(sizeof(node));
topptr = (node*)malloc(sizeof(node));
topptr = headptr;
headptr->value=i;
headptr->nextPtr = (node*)malloc(sizeof(node));
for(i=1;i<5;i++)
{
headptr = headptr->nextPtr ;
headptr->value=i;
headptr->nextPtr=(node*)malloc(sizeof(node));
printf("val is %p \n ", *headptr);
}
headptr->nextPtr = NULL;
}
int getLength(node *topptr)
{
int i=0;
node* local;
local = topptr;
while(local!=NULL)
{
local=local->nextPtr;
i++;
}
return i;
}
int main()
{
initializeLinkedList(headptr,topptr);
printf("val is %d \n", getLength(topptr));
return 0;
#包括
#包括
节点*headptr;
节点*topptr;
类型定义结构节点
{
int值;
结构节点*nextPtr;
}节点;
无效初始值ElinkedList(节点*headptr,节点*toptr)
{
int i=0;
headptr=(节点*)malloc(节点大小);
topttr=(节点*)malloc(sizeof(节点));
topttr=headptr;
headptr->value=i;
headptr->nextPtr=(node*)malloc(sizeof(node));
对于(i=1;inxtptr;
headptr->value=i;
headptr->nextPtr=(node*)malloc(sizeof(node));
printf(“val为%p\n”,*headptr);
}
headptr->nextPtr=NULL;
}
int getLength(节点*topttr)
{
int i=0;
节点*本地;
本地=topptr;
while(本地!=NULL)
{
本地=本地->下一个TPTR;
i++;
}
返回i;
}
int main()
{
初始化eLink列表(headptr、topptr);
printf(“val是%d\n”,getLength(topttr));
返回0;
}InitializeLink列表不修改main中定义的变量headptr和topttr(传递值)。因此传递给getLength的变量包含垃圾
void initializeLinkedList(node *headptr, node *topptr)
换成
void initializeLinkedList(node *headptr, node** topptr)
并相应地更改您的代码
还有很多其他问题
当您需要指针时,只需定义指针,而不分配内存并覆盖指针
如果我必须编码的话
void initializeLinkedList( node **topptr)
{
int i=0;
node* headptr = (node*)malloc(sizeof(node));
headptr->value=i;
*topptr = headptr;
for(i=1;i<5;i++)
{
headptr->nextPtr = (node*)malloc(sizeof(node));
headptr->nextPtr->value=i;
headptr->nextPtr->nextPtr=NULL;
headptr=headptr->nextPtr;
}
}
int main()
{
node* topptr;
initializeLinkedList(&topptr);
printf("val is %d \n", getLength(topptr));
return 0;
}
void initializeLinkedList(节点**topttr)
{
int i=0;
node*headptr=(node*)malloc(sizeof(node));
headptr->value=i;
*topttr=headptr;
对于(i=1;inxtptr=(node*)malloc(sizeof(node));
headptr->nextPtr->value=i;
headptr->nextPtr->nextPtr=NULL;
headptr=headptr->nextPtr;
}
}
int main()
{
节点*topptr;
初始化ElinkedList(&topptr);
printf(“val是%d\n”,getLength(topttr));
返回0;
}
#包括
#包括
类型定义结构节点{
int值;
结构节点*nextPtr;
}节点;
无效初始值ElinkedList(节点**顶部,节点**后部){
int i=0;
节点*本地;
*top=(node*)malloc(sizeof(node));
本地=*顶部;
本地->值=i;
local->nextPtr=NULL;
对于(i=1;inxtptr=(node*)malloc(sizeof(node));
本地=本地->下一个TPTR;
本地->值=i;
local->nextPtr=NULL;
}
*后=本地;
}
int getLength(节点*np){
int i;
对于(i=0;np!=NULL;++i,np=np->nextPtr)
;//printf(“调试:%d\n”,np->value);
返回i;
}
内部主(空){
节点*顶部,*后部;
初始化kedlist(顶部和后部);
printf(“长度为%d\n”,getLength(顶部));
返回0;
}
在计算列表中的元素时,为什么要分配数据?您只需迭代列表并增加计数器。我实际上没有看到上面提出的问题,只有一条语句和一些代码。我同意Max的说法,我已经编辑了代码,但仍然没有得到正确的结果。编辑后,headptr
和toptr
是文件范围对象,因此不再是垃圾,而是NULL
。但诊断保持不变。
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *nextPtr;
} node;
void initializeLinkedList(node **top, node **rear){
int i=0;
node *local;
*top = (node*)malloc(sizeof(node));
local = *top;
local->value=i;
local->nextPtr = NULL;
for(i=1;i<5;++i){
local->nextPtr = (node*)malloc(sizeof(node));
local = local->nextPtr;
local->value = i;
local->nextPtr = NULL;
}
*rear = local;
}
int getLength(node *np){
int i;
for(i=0;np!=NULL;++i, np = np->nextPtr)
;//printf("debug:%d\n", np->value);
return i;
}
int main(void){
node *top, *rear;
initializeLinkedList(&top, &rear);
printf("length is %d \n", getLength(top));
return 0;
}