我是新来的C语言编程,我做一个链接列表,以便我能找到,显示, 插入,删除和节点进行排序;然而,编译器给我这些错误:警告:在#include指令的末尾额外的令牌
main.c:1:19: warning: extra tokens at end of #include directive [enabled by default]
/usr/lib/../lib64/crt1.o: In function
_start': (.text+0x20): undefined reference to
main' collect2: error: ld returned 1 exit status
我没有线索他们是什么。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void dump_list (struct data_node *current);
void dump_node (struct data_node *current);
struct data_node * find_node (struct data_node *p, int elem);
int delete (struct data_node **p_first, int elem);
struct data_node * insert (struct data_node **p_first, int elem, char *newname);
struct data_node {
char name [25];
int data;
struct data_node *next;
};
void dump_list (struct data_node *current) {
printf("Printing list:\n");
while (current != NULL) {
printf("%s: %d\n", current->name, current->data);
current=current->next;
} /* end while */
printf("\n");
}; /* end dump_list */
void dump_node (struct data_node *current) {
printf("Printing node: ");
if (current != NULL)
printf("%s: %d\n", current->name, current->data);
}; /* end dump_node */
struct data_node * find_node (struct data_node *p, int elem) {
while (p != NULL) {
if (elem == p->data)
return p;
p=p->next;
} /* end while */
if(p==NULL){
printf("Node not found\n");
return p;
}
}; /* end find_node */
int delete (struct data_node **p_first, int elem) {
int retval = 0;
struct data_node *current, *prev;
current=*p_first;
while (current != NULL && elem != current->data) {
prev=current;
current=current->next;
}
if (current == NULL) /* element not found */
return retval;
/* current now points to node to delete */
if (current == *p_first) /* delete 1st node */
*p_first = (*p_first)->next;
else /* link previous to next thus skipping over node to delete */
prev->next=current->next;
free(current);
retval=1;
return retval;
}; /* end delete */
struct data_node * insert (struct data_node **p_first, int elem, char *newname) {
struct data_node *new_node, *prev, *current;
current=*p_first;
while (current != NULL && elem > current->data) {
prev=current;
current=current->next;
} /* end while */
/* current now points to position *before* which we need to insert */
new_node = (struct data_node *) malloc(sizeof(struct data_node));
new_node->data=elem;
strcpy(new_node->name, newname);
new_node->next=current;
if (current == *p_first) /* insert before 1st element */
*p_first=new_node;
else /* now insert before current */
prev->next=new_node;
/* end if current == *p_first */
return new_node;
}; /* end insert */
int main (void) {
char key;
int newelem;
char newstring [25];
int removeelem;
int findelem;
struct data_node *first=NULL, *ptr;
printf ("Enter list command (+-flx): ");
scanf (" %c", &key);
int newelem;
char newstring [25];
int removeelem;
int findelem;
switch (key)
{
/* tyep +*/
case '+':
printf ("+ detected \n");
printf ("Enter key data: \n");
scanf ("%D", &newelem);
printf ("What string to store? \n");
gets(newstring);
insert(&first, newelem,newstring);
break;
/* tyep -*/
case '-':
printf ("- detected \n");
printf ("Enter key data: \n");
scanf ("%D", &removeelem);
delete(&first, removeelem);
if(delete(&first, removeelem) ==0)
printf ("Data not found. No deletion performed. \n");
break;
/* tyep f*/
case 'f':
printf ("f detected \n");
printf ("Enter key data: \n");
scanf ("%D", &findelem);
while (ptr != NULL)
{
ptr=find_node(first, findelem);
dump_node(ptr);
}
printf ("Node not found. \n");
break;
/* tyep l*/
case 'l':
printf ("l detected \n");
dump_list(first);
break;
/* tyep x*/
case 'x':
printf ("Goodbye. \n");
break;
/* no default case necessary here */
}
return (0);
} /* end main */
为什么你重复你的#includes和data_node的定义? –
不会在网络端将您的整个代码泵送给我们,但会将其减少到可以再现您的问题的最小示例。通过这样做,你可能会自己发现错误。很可能这甚至不是产生错误的代码,而是混淆了文件。 –
“main.c:1:19:warning:#include directive”结尾处的额外标记“意味着该行在第19行的末尾:也许是'#include'。它可能是一个像''\ r''这样的空格。有时这发生在混合编辑器中。如果需要,删除第19行并重新输入。 –
chux