2014-01-07 3 views
-1

안녕하세요 여러분, 대부분의 프로그래밍이 C로 끝나는 인터뷰를 준비 중입니다. 객체가 어떻게 저수준 언어로 실제로 구현되는지 이해하기 위해 C로 연결된 목록 클래스를 구현하기로 결정했습니다. 이는 객체 지향 패러다임을 지원하지 않는다. 나는 컴파일에 관한 몇 가지 문제를 겪었으므로 C에 익숙한 사람이라면 누구든지 도와 주면된다 (나는 C로 프로그래밍 한 적이 없다). 아래 코드뿐만 아니라 컴파일 오류도 게시했습니다.C 프로그래밍 컴파일 문제

bash-4.1$ gcc -o LinkedList LinkedList.c 
LinkedList.c:6: error: expected specifier-qualifier-list before ‘node’ 
LinkedList.c: In function ‘main’: 
LinkedList.c:15: warning: incompatible implicit declaration of built-in function ‘malloc’ 
LinkedList.c:17: error: ‘List’ has no member named ‘next’ 
LinkedList.c: In function ‘insertEnd’: 
LinkedList.c:24: error: ‘List’ has no member named ‘next’ 
LinkedList.c:25: error: ‘List’ has no member named ‘next’ 
LinkedList.c:27: warning: incompatible implicit declaration of built-in function ‘malloc’ 
LinkedList.c:29: error: ‘List’ has no member named ‘next’ 
LinkedList.c:30: error: ‘List’ has no member named ‘next’ 
LinkedList.c: In function ‘PrintList’: 
LinkedList.c:36: error: ‘List’ has no member named ‘next’ 
+2

C를 배우고 실습하는데 많은 시간이 걸립니다. 모든 경고와 디버그 정보 ('gcc -Wall -g')를 활성화하고 디버거 ('gdb')를 사용하십시오. 또한 '#include ' –

답변

2

nextstruct node *next 당신과 함께 선언해야 회원에게 다음과 같이

//Creating a LinkedList in C 
//with 3 basic operations 
#include <stdio.h> 
typedef struct node { 
    int data; 
    node* next; 
} List; 

void insertEnd(List *node, int elem); 
void PrintList(List *node); 
/*void insertEnd(List *node, int elem); 
void remove(List *node, int elem);*/ 

int main() { 
    List *head = (List *)malloc(sizeof(List)); 
    head->data = 0; 
    head->next = NULL; 
    insertEnd(head, 3); 
    insertEnd(head, 4); 
    PrintList(head); 
} 
void insertEnd(List *node, int elem) { 
    while (node->next != NULL) { 
     node = node->next; 
    } 
    List *new_node = (List *)malloc(sizeof(List)); 
    new_node->data = elem; 
    new_node->next = NULL; 
    node->next = new_node; 
} 

void PrintList(List *node) { 
while (node) { 
    printf ("%i ->", node->data); 
    node = node->next; 
} 
} 

오류는 에 다음과 같이 선언되어 있습니다. :

그리고 프로그램에 버그가 없어 질 때까지 -Wall 플래그로 컴파일해야합니다.

#include <stdlib.h> 

// ... 

typedef struct node { 
    int data; 
    struct node *next; 
} List; 

// ... 
1

변화

typedef struct node { 
    int data; 
    node* next; 
} List; 

typedef struct node { 
    int data; 
    struct node* next; 
} List;