2011-10-20 3 views
2

내 Mac에서 NetBeans의 전체 프로젝트를 수행했지만 정상적으로 작동합니다. 그런 다음 터미널에 넣어서 학교용 ssh 서버에 제출하고 컴파일 오류가 발생합니다. netbeans가 C++로 컴파일 되었습니까? 나는 C에게 새로운 것이므로 어떤 도움도 분명히 감사하게 생각합니다. 여기에 코드가 있습니다.C의 연결된 목록에 대한 컴파일 오류

/* 
* File: LinkedList.c 
* Author: dpiganell 
* 
* Created on October 17, 2011, 2:31 PM 
*/ 

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

struct element{ 
    int i; 
    struct element *next; 
}; 

void insert(struct element**, struct element*); 
void purge (struct element*, struct element*); 
void printList(struct element*); 
void printListBackwards(struct element*); 

struct element *h; 

int main() 
{ 
    // HEAD 
    h = (element *) malloc(sizeof(element)); 
    h = NULL; 

    // NEW VALUE 
    struct element *n = NULL; 

    // POINTER TO THE POINTER OF HEAD 
    struct element **headPointer; 
    headPointer = (element **) malloc(sizeof(element)); 

    int a; 
    a = 1; 
    while(a >= 0) 
    { 
     n = (element *) malloc(sizeof(element)); 

     printf("\nPlease enter an integer value: "); 
     scanf("%d", &a); 

     if(a >= 0) 
     { 
      n->i = a; 
      n->next = NULL; 
      headPointer = &h; 

      insert(headPointer, n); 

     n = n++; 


     } 
     printList(h); 
     printListBackwards(h); 
    } 

    return 0; 
} 


void insert(struct element **head, struct element *n) 
{ 
    element *nptr, *pptr, *cptr; 
    // NEW POINTER, PREVIOUS POINTER, CURRENT POINTER 

    nptr = (element *) malloc(sizeof(element)); 

    int purged; 
    purged = -1; 
    if(nptr != NULL) 
    { 
     nptr->i = n->i; 
     nptr->next = NULL; 

     cptr = *head; 
     pptr = NULL; 
    } 

    while(cptr != NULL && n->i >= cptr->i) 
    { 
     if(cptr->i == n->i && purged != 1) 
     { 
      purged = 1; 
      purge(cptr, pptr); 
     } 
     else 
     { 
      pptr = cptr; 
      cptr = cptr->next; 
     } 
    } 

    if(pptr == NULL && purged < 0) 
    { 
     nptr->next = *head; 
     *head = nptr; 
    } 
    else if(purged < 0) 
    { 
     pptr->next = nptr; 
     nptr->next = cptr; 
    } 
} 


void purge(struct element *current, struct element *predecessor) 
{ 
    element *ptr = (element *) malloc(sizeof(element)); 

    // ERASING THE HEAD 
    if(predecessor == NULL) 
    { 
     if(current->next == NULL) 
     { 
      current->next = NULL; 
      current->i = NULL; 
      current = NULL; 
      free(current); 
      h = NULL; 
     } 
     else 
      memcpy(current, current->next, sizeof(element)); 
    } 

    // ERASING THE TAIL 
    else if(current->next == NULL) 
    { 
     current->i = NULL; 
     free(current->next); 
     free(current); 
     predecessor->next = NULL; 
    } 

    // ERASING ANYTHING IN THE MIDDLE OF THE LIST 
    else 
    { 
     ptr = current->next; 
     predecessor->next = ptr; 
     current->i = NULL; 
     free(current->next); 
    } 
} 

void printList(struct element *head) 
{ 
    if(head == NULL) 
     printf("The list is empty."); 
    else 
    { 
     struct element *ptr; 
     ptr = (element*) malloc(sizeof(element));  
     ptr = head; 

     int a; 
     a = 1; 
     printf("Forwards: "); 
     while(a > 0) 
     { 
      printf("%d ", ptr->i); 
      if((ptr->next) == NULL) 
       a = -1; 
      else 
       ptr = ptr->next; 

     } 
    } 
} 

void printListBackwards(struct element *ptr) 
{ 
    if(ptr == NULL) 
    { 
     // DO NOTHING BECAUSE IT WILL BE PRINTED ALREADY THAT IT IS EMPTIED 
    } 
    else 
    { 
     element *cptr = (element *) malloc(sizeof(element)); 
     cptr = ptr; 
     if(ptr->next == NULL) 
      printf("\nBackwards: %d ", ptr->i); 
     else 
     { 
      printListBackwards(ptr->next); 
      printf("%d ", ptr->i); 
     } 
    } 
} 

또한 여기에 오류가 있습니다. 그것은 잘 작동하고 netbeans에서 잘 컴파일합니다.

LinkedList.c:14: warning: useless keyword or type name in empty declaration 
LinkedList.c: In function `main': 
LinkedList.c:26: error: `element' undeclared (first use in this function) 
LinkedList.c:26: error: (Each undeclared identifier is reported only once 
LinkedList.c:26: error: for each function it appears in.) 
LinkedList.c:26: error: syntax error before ')' token 
LinkedList.c:34: error: syntax error before ')' token 
LinkedList.c:40: error: syntax error before ')' token 
LinkedList.c: In function `insert': 
LinkedList.c:65: error: `element' undeclared (first use in this function) 
LinkedList.c:65: error: `nptr' undeclared (first use in this function) 
LinkedList.c:65: error: `pptr' undeclared (first use in this function) 
LinkedList.c:65: error: `cptr' undeclared (first use in this function) 
LinkedList.c:68: error: syntax error before ')' token 
LinkedList.c: In function `purge': 
LinkedList.c:110: error: `element' undeclared (first use in this function) 
LinkedList.c:110: error: `ptr' undeclared (first use in this function) 
LinkedList.c:110: error: syntax error before ')' token 
LinkedList.c: In function `printList': 
LinkedList.c:153: error: `element' undeclared (first use in this function) 
LinkedList.c:153: error: syntax error before ')' token 
LinkedList.c: In function `printListBackwards': 
LinkedList.c:179: error: `element' undeclared (first use in this function) 
LinkedList.c:179: error: `cptr' undeclared (first use in this function) 
LinkedList.c:179: error: syntax error before ')' token 
+0

정의하지 않은 유형을 사용하려고합니다. –

+0

오류가 발생한 정확한 코드를 표시 하시겠습니까? 컴파일 할 때 첫 번째 오류는'element'가 선언되지 않았다는 것입니다 (C에서는 그 유형의 이름이'struct element'입니다). C++에서보고있는 오류를 재현 할 수 없습니다. –

+0

@KeithThompson C++에는 오류가 없습니다. 도움을 주셔서 감사하지만 문제는 답변되었습니다. 나는 그것을 C++ 컴파일러의 netbeans에서 컴파일하여 작동했습니다. 그러나 gcc를 사용했을 때 일을 정리하기 위해 작동하지 않았습니다. 모두에게 감사드립니다. –

답변

5

이 정의

struct element{ 
    int i; 
    struct element *next; 
}; 

struct element로 언급해야한다. 당신은 모든 곳이 아닌 어떤 곳에서 그렇게합니다. 알려진 관용구이다 : 그래서 당신은 유형의 앞에 struct을 배치 할 필요가 없습니다 elementstruct element로 타입 정의

typedef struct element { 
    int i; 
    struct element *next; 
} element; 

. 이것은 매우 일반적인 C++ 종류의 "this for you"이므로 struct은 더 이상 필요하지 않습니다.

4

그것은 C가, 당신이 베어 element을 사용할 수 없습니다, 그것은 struct element입니다.

1

귀하의 문제는 귀하의 신고 방법과 귀하의 struct element 사용 방법에 관한 것입니다.

typedef struct element{ 
    int i; 
    struct element *next; 
} element; 

보다 만족스러운 결과를 얻을 수 있습니다.

번체로 struct을 그대로 둘 수 있습니다. element을 사용하는 경우 어디든 이라고 쓰십시오.

은 BTW :

이것은 정확한 문제와 관련,하지만 난 (내 컴파일러 나에게 그것에 대해 경고를 주면서) 내가 공유하는 것이라고 생각되지 않는다; 당신은 가끔 쓰기 :

이 경우
current->i = NULL; 

, i는 정수입니다. C에서는 NULL이 전통적으로 포인터 유형에만으로 사용됩니다. 그래서 혼동하지, 당신이에 할당 변경해야 하나 : 실제로 i 어딘가에 (위의 문맥 제공하는 메모리를 가리 키도록 의미하는 경우,

current->i = 0; 

을 ... 나, 나는 그렇지 추정), 포인터 유형으로 선언하십시오. C에서

1

구조체를 그런 식으로 사용하려면 구조체를 typedef해야합니다. 그렇지 않으면 모든 곳에서 struct 키워드를 사용해야합니다.

typedef struct element { 
    int i; 
    element *next; 
} element; 

당신이 요소를 선언 할 때 : 확인 documentation

그래서 당신은 말하고 싶다. 그렇지 않으면 컴파일러는 사용할 때마다 해당 요소가 구조체라는 것을 알지 못합니다.

0
h = (element *) malloc(sizeof(element)); 
h = NULL; 
headPointer = (element **) malloc(sizeof(element)); 

이것은 첫 번째 오류입니다. 요소는 typedef가 아닌 struct 태그입니다. (이는 다른 사용자에 의해 처리되고있다.)

를 루프 :

headPointer = &h; 

두 포인터 (의 malloc() d)의 값이 할당되어있다. 다시 할당하면 이전에 malloc()이었던 것을 잃게됩니다.

'headPointer'자료는 좋은 생각입니다. 다른 포인터를 가리킬 수있는 포인터입니다. 이 함수를 사용하여 호출 된 함수가 호출자의 on 값을 변경하도록 할 수 있습니다. 하지만 당신의 사용법이 옳지 않습니다. 일반적으로 호출자가 수행해야합니다 함수가 반환 후

void my_function (struct clown **ppp) { 
    ... 
    *ppp = malloc (sizeof (struct clown)); 
    ... 
} 

는, 호출 측이 포인터를 사용할 수 있으며, 희망에 가리 킵니다 :

struct clown *pipo=NULL; 
... 
my_function(&pipo); 

를 함수 내부, 호출자의 포인터는 이상 변경 될 수 있습니다 새로운 광대. 그게 전부입니다.

관련 문제