嗨,我正准备面试,其中大部分编程都是用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' –