C语言程序使用链表结构出现段错误

#include
typedef struct element *Hash;
struct element
{
int x;
int hash;
int position;
Hash next;
};
void Insert(int hash,int position,int x,Hash P)
{
Hash tmp;
if(tmp==NULL){
printf("Out of Space!");
}
else{
tmp->x=x;
tmp->hash=hash;
tmp->position=position;
tmp->next=P->next;
P->next=tmp;
}
}
Hash FindPosition(int hash,int position,int x,Hash L)
{
int i,count; Hash P;
count=position-hash;
P=L;
if(count>0){
while(count>=0){
if(P->positionposition>=hash){
P=P->next;
count--;
}
else if(x>P->x){
P=P->next;
}
if(P->next==NULL) break;
}
}
else if(count==0)
{
while(x>P->x) P=P->next;
}
else
{
count+=11;
while(count>0){
if(P->positionposition>position){
P=P->next;
count--;
}
else if(x>P->x)
{
P=P->next;
}
if(P->next==NULL) break;
}
}
return P;}
void Printresult(Hash L)
{
while(L!=NULL){
printf("%d",L->x);
L=L->next;
}
}
int main(void){
int N,i,x,hash;
Hash P,L,tmp;
scanf("%d",&N);
scanf("%d",&x);
L=malloc(sizeof(struct element));
if(L==NULL){ printf("Out of Space!"); }
else{
L->x=x;
L->hash=x%N;
L->position=0;
L->next=NULL;
}
for(i=1;i<N;i++){
scanf("%d",x);
if(x<0) hash="x%N;"
p="L-">next;
L->next=NULL;
while(P!=NULL){
tmp=P->next;
free(P);
P=tmp;
}
free(L);
}

/* 头文件的包含应写清楚 */
#include<stdio.h>
#include <malloc.h>

/* 原来的定义方式虽然可以,但是比较奇怪 */
typedef struct element
{
int x;
int hash;
int position;
struct element* next;
}S_HASH;
typedef S_HASH* Hash;

void Insert(int hash,int position,int x,Hash P)
{
Hash tmp = NULL; /* 未初始化,会导致接下来的if语句判断无效 */
if(tmp == NULL)
{
printf("Out of Space!");
}
else
{
tmp->x=x;
tmp->hash=hash;
tmp->position=position;
tmp->next=P->next;
P->next=tmp;
}
}

Hash FindPosition(int hash,int position,int x,Hash L)
{
int count;
Hash P;

count=position-hash;
P=L;
if(count>0)
{
while(count>=0)
{
if(P->position>=hash)
{
P=P->next;
count--;
}
else if(x>P->x)
{
P=P->next;
}
if(P->next==NULL) 
break;
}
}
else if(count==0)
{
while(x>P->x)
P=P->next;
}
else
{
count+=11;
while(count>0)
{
if(P->position>position)
{
P=P->next;
count--;
}
else if(x>P->x)
{
P=P->next;
}
if(P->next==NULL)
break;
}
}
return P;
}

void Printresult(Hash L)
{
while(L!=NULL)
{
printf("%d",L->x);
L=L->next;
}
}

int main(void)
{
int N,i,x,hash;
Hash P,L,tmp;
scanf("%d",&N);
scanf("%d",&x);
L = (Hash)malloc(sizeof(struct element));/* malloc时需指明数据类型 */
if(L==NULL)
{
printf("Out of Space!");
}
else
{
L->x=x;
L->hash=x%N;
L->position=0;
L->next=NULL;
}
for(i=1;i<N;i++)
{
scanf("%d",x);
if(x<0) 
hash=x%N; /* 不清楚为什么会加上"" */
P=L->next; /* 不清楚为什么会加上"" */
L->next=NULL;
while(P!=NULL)
{
tmp=P->next;
free(P);
P=tmp;
}
free(L);
}
} /* 缺少一个} */

只改了编译错误,逻辑上有无错误没看

追问

好像把程序拷过来的时候有点问题……你提供的代码还是有段错误诶……

追答

把error贴出来撒

追问

编译通过了的,但是运行的时候有问题,调试的时候出现了Program stopped at 0x7559ecc0.It is stopped with signal SIGSEGV,Segmentation fault.是段错误,好像是因为内存分配问题引起的,但是不知道是代码的哪里错了。

追答for(i=1;i<N;i++)
{  
scanf("%d",&x); /* 缺少& */
if(x<0) 
hash=x%N; 
P=L->next;
L->next=NULL;
while(P!=NULL)
{
tmp=P->next;
free(P);
P=tmp; 
}
free(L); /* 后面应加上L = NULL */
}

只改了编译错误,逻辑上有无错误没看,因为我并不清楚你这个程序到底是要实现什么功能的,不过你这个for循环很有问题,L已经free了,就不应该再继续循环下去,否则执行到P = L->next的时候会崩溃

温馨提示:答案为网友推荐,仅供参考
相似回答