2016-10-27 3 views
3

내가하고있는 연결 목록에이 코드가 있습니다. 제거 기능을 추가하기 전에 잘 작동하고있었습니다. 추가 한 후에는 나중에 게시물의 오류가 표시됩니다. 나는 당신이 볼 수있는 것처럼 그것을 이미 초기화 했으므로 문제의 원인을 생각할 수 없다.기능에 대한 충돌 유형 "제거"

#include <stdio.h> 
#include <stdlib.h> 

struct node 
{ 
    int value; 
    struct node* next; 
}; 

typedef struct node Node; 
typedef Node* NodePtr; 

void push(int value, NodePtr *start); 
void add(int value, NodePtr *start); 
void pop(NodePtr* start); 
void remove(NodePtr* start); //line 16 
void traverse(NodePtr* start); 
int main(void) 
{ 
    NodePtr first = NULL; 
    push(2, &first); 
    add(3, &first); 
    printf("%d, %p\n", first -> value, first -> next); 
    printf("%d, %p\n", first -> next -> value, first -> next -> next); 

    push(4, &first); 
    add(5, &first); 
    printf("%d, %p\n", first -> value, first -> next); 
    printf("%d, %p\n", first -> next -> value, first -> next -> next); 

    pop(&first); 
    pop(&first); 
    printf("%d, %p\n", first -> value, first -> next); 
    printf("%d, %p\n", first -> next -> value, first -> next -> next); 

    remove(&first); 
    add(6, &first); 
    printf("%d, %p\n", first -> value, first -> next); 
    printf("%d, %p\n", first -> next -> value, first -> next -> next); 
    return 0; 
} 

//push node to beginning 
void push(int value, NodePtr *start) 
{ 
    NodePtr newStart = malloc(sizeof(Node)); 
    if(newStart == NULL) 
    { 
     return; 
    } 
    newStart -> value = value; 
    newStart -> next = *start; 
    *start = newStart; 
} 
//add node to end 
void add(int value, NodePtr *start) 
{ 
    NodePtr newNode = malloc(sizeof(Node)); 

    if(newNode == NULL) 
    { 
     return; 
    } 

    newNode -> value = value; 
    newNode -> next = NULL; 

    NodePtr current = *start; 

    while((current)->next != NULL) 
    { 
     current = current -> next; 
    } 

    current -> next = newNode; 
} 
//pop beginning node 
void pop(NodePtr* start) 
{ 
    NodePtr trash = *start; 
    (*start) = (*start)->next; 
    free(trash); 
} 

//remove last node 
void remove(NodePtr* start) //line 87 
{ 
    NodePtr current = *start; 

    while((current)->next != NULL) 
    { 
     if(current->next == NULL) 
     { 
      break; 
     } 
     current = current -> next; 
    } 
    NodePtr trash = current -> next; 
    current -> next = current; 
    free(trash); 
}  

//goes through list 
void traverse(NodePtr* start) 
{ 
    NodePtr current = *start; 
    while((current -> next) != NULL) 
    { 
     current = current -> next; 
    } 
} 

다음은 오류

~/C-Programs> make zelda 
cc -g -Wall -Wextra -lm -std=c99 zelda.c -o zelda 
zelda.c:16: error: conflicting types for ‘remove’ 
/usr/include/stdio.h:177: note: previous declaration of ‘remove’ was here 
zelda.c:87: error: conflicting types for ‘remove’ 
/usr/include/stdio.h:177: note: previous declaration of ‘remove’ was here 
make: *** [zelda] Error 1 

나는 그것이 내가 그것을 초기화하지만 맞춤법 오류/잘못된 매개 변수를 찾을 수 없습니다 어떻게 함께 할 수있는 뭔가가 생각하는 것입니다. 누구는 이유가 무엇인지 압니까?

+1

** ** typedef ** 포인터를 사용하지 마십시오! 손 치워! 이것은 아무 일도 아닙니다! 생각조차하지 마라. 벙커에 넣고, 광산으로 둘러싸고, 개들을 감시하십시오. 당신의 두뇌에서 위치를 채 웁니다! 이 모든 것을 잊어 버려라. - 그 경고가 너무 미묘하지 않다는 것을 희망하십시오 ... – Olaf

+0

@Olaf는 디버깅을 상당히 어렵게 만들었으므로 먼저 포인터를 typedef해서는 안되며 NodePtr 대신 Node *를 입력하는 것이 더 짧습니다. 이 IMO. – saeleko

+0

@ 루다 오타쿠 : 두 가지 모두 타당한 이유가 있지만 주된 이유는 아닙니다. 포인터는 자신의 유형입니다. 그것들을 typedefing하면 코드가 읽기 쉽고 (접두사/접미사가 있더라도) 오류가 발생하기 쉽습니다. 나쁘다 : 당신이 가리키는 객체를 한정 할 수 없기 때문에 적어도 두 개의'typedef '가 필요하다. 'const' 올바른 코드. 그것은 또한 불필요하게 네임 스페이스를 오염시킵니다. – Olaf

답변

6

remove()이라는 <stdio.h>에있는 C 표준 함수가 자신의 remove과 충돌합니다. 가장 쉬운 해결책은 함수의 이름을 my_remove()과 같은 것으로 변경하는 것입니다.

+0

고쳐 주셔서 감사합니다! – mkahihu

+1

이 답변으로 도움이 되었다면 [수락 고려하십시오] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). –

+3

@mkahihu 장래에 이런 종류의 문제를 발견하는 데 도움이되도록 오류 메시지에는 이전 선언이 "/usr/include/stdio.h"(표준 헤더 중 하나)에 있음을 알리는 줄이 포함되어 있습니다. 그 이름으로 표준 라이브러리에 함수가 있음을 알려야합니다. – Dmitri