2014-12-12 5 views
0

나는 처음으로 Duplicate check를 작성한 후 다음과 같은 Mammal을 가리킨다는 것을 알았습니다. 그래서 great_circle을 호출하면 두 개 사이의 거리를 계산할 때 0의 목록을 얻습니다. 동일한 포유 동물을 비교합니다.연결된 항목에서 비슷한 항목 제거

void remove_duplicates() { 
int i,j; 
double distance; 
Mammal *next=head2.pointer; 
for(i=0;i<list_length2-1;i++) { 
    Mammal *check=next->pointer; 
    Duplicate *d=malloc(sizeof(Duplicate)); 
    d->mampointer=NULL; 
    d->number_of=0; 
    for(j=0;j<(list_length2-i)-1;j++) { 
     distance=great_circle(next->location, check->location); 
     if(distance<=0.02 && next->species==check->species) { 
      Mammal *a=next; 
      Mammal *b=check; 
      a->pointer=d->mampointer; 
      d->mampointer=a; 
      b->pointer=d->mampointer; 
      d->mampointer=b; 
      d->number_of++; 
      d->number_of++; 
     } 
     printf("%f\n",distance); 
     if (check->pointer!=NULL) { 
      check=check->pointer; 
     } 
    } 
    if(d->mampointer!=NULL) { 
     add_duplicate(create_duplicate(d)); 
    } 
    if (next->pointer!=NULL) { 
     next=next->pointer; 
    } 
} 
} 

다음에 발생하지 않아야 할 다음 메모리와 동일한 메모리를 가리키는 것으로 보입니다.

편집 : 내가 해결하기 위해 노력하고있어 문제는 다음과 같습니다

가 위도와 경도 좌표 여러 포유 동물이다

포유류의 일부

하지만 약간 다른과 여러 번보고되고있다 좌표,

나는이 중복을 찾아 '잘못된'좌표의 평균을 가진 단일 포유 동물로 대체해야합니다.

+1

Q : "중복을 삭제하는"알고리즘은 무엇입니까? 코드가 무엇을해야하는지 설명해 주시겠습니까? Q : "great_circle()"이란 무엇입니까? Q : 왜 "remove_duplicates()"안에 "add_duplicate()"합니까? Q : 해결하려는 문제는 정확히 무엇입니까? – FoggyDay

+0

자세한 내용보기 – Jim006Jam

+0

코드를 실제로 정리할 수 있다면 도움이 될 것입니다. 'check-> pointer'는 당신의 명명법이'check-> next'가 아니고'Mammal * next' 대신'Mammal * mPointer'이어야합니다. 또한 if (거리 = 0.02) && (다음 -> 종 = 체크 -> 종)이어야합니다. 종))'그것의 문장을 단락시키는 방법. – KillaBytes

답변

0

이 링크에서보세요 : http://www.cprogramdevelop.com/2252274/ 당신이 처음에 등 head2

처럼 중복 (혼란) 포인터없이 연결리스트를 순회가 Kozmik의 코멘트에 대한 check->pointercheck->next을해야 올바른 나타납니다 보면 도움이 될 것입니다 -하지만 다시 코드를 읽을 수 없게 만들지 않으면 중복 포인터를 피하십시오.

내가 연결된 목록을 통과하는 우아한 방법을 찾을 수는 '빈'노드를 허용하고 cur_node->next->item == NULL (나는 cur_node->pointer는 해당 노드에 저장하는 항목으로 다스 려되어 있으리라 믿고있어 여부를 확인해야하는 경우 cur_node->next == NULL보다는 중지하는 것입니다). 예를 들어

:

typedef struct node_s { 
    void * item; // the item you are storing in this node 
    node_s * next; // the next node 
} Node; 
typedef struct list_s { 
    void * head; 
    /* maybe some extra info like size etc */ 
} List; 

그런 다음 통과

은 간단합니다 :

List * list = malloc(sizeof *list); 
list->head = NULL; 

/* 
    create your first node etc, add to list in the obvious way. 
    ... 
    add more nodes 
    ... 
*/ 

//traversal 
Node ** head = &list->head; 
while (*head) { //we have a non-null node so keep going... 
    Node *cur_node = *head; 
    head = &cur_node->next; 
    /* do stuff with current node (even delete it!). */ 
} 

매우 간단 이러한 방법으로, 두 포인터에 대한 (headcur_node을) 걱정.