还是在写作业的时候,发现的问题 : )
So, 作业是写一个单链表,所以我打开vscode,输入了如下代码:
#include <stdio.h>
#include <stdlib.h>
struct List
{
int data;
struct List *next;
}Node, *Link;
void insertSorted(Link *head, int newData){
Link newNode = (Link) malloc(sizeof(Node));
if(newNode == NULL){
printf("Memory Error");
return;
}
// Initialize the node
newNode->data = newData;
newNode->next = NULL;
if(*head == NULL || (*head)->data >= newData){
newNode->next = *head;
*head = newNode;
return;
}
Link current = *head;
while(current->next != NULL && current->next->data < newData){
current = current->next;
}
// Insert the node
newNode->next = current->next;
current->next = newNode;
}
void printlist(Link head){
while(head != NULL){
printf("%d",head->data);
head = head->next;
}
printf("\n");
}
int main(){
Link head = NULL;
insertSorted(&head, 1);
insertSorted(&head, 2);
insertSorted(&head, 3);
insertSorted(&head, 4);
insertSorted(&head, 6);
printlist(head);
insertSorted(&head, 5);
printlist(head);
return 0;
}
然后报错就悄然发生了。。。
————>
查看错误
————>
expected declaration specifiers or '...' before 'Link'gcc
这段令人琢磨不透的报错就出来了 0.o
------>解决方法:
结构体定义错误,struct List不被gcc理解
应写成------>typedef struct List
And there you go,没有报错,程序正常运行。
新手时用的codeblock根本不会有这个问题啊。
至此,问题解决。