嗨,我正準備面試,其中大部分編程都是用C語言完成的。我決定在C語言中實現一個鏈接列表類,以便了解如何以低級語言真正實現對象不支持面向對象的範例。我遇到了一些編譯問題,所以熟悉C的人請幫忙(我從來沒有用C編寫過)。我在下面發佈了我的代碼以及之後的編譯錯誤。C編程編譯問題
//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;
}
}
的錯誤如下:
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’
花幾天或幾周時間來學習C並練習它。啓用所有警告和調試信息('gcc -Wall -g')並使用調試器('gdb')。另外'#include' –