2014-03-31 39 views
-1

我是新来的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 */ 
+0

为什么你重复你的#includes和data_node的定义? –

+0

不会在网络端将您的整个代码泵送给我们,但会将其减少到可以再现您的问题的最小示例。通过这样做,你可能会自己发现错误。很可能这甚至不是产生错误的代码,而是混淆了文件。 –

+0

“main.c:1:19:warning:#include directive”结尾处的额外标记“意味着该行在第19行的末尾:也许是'#include '。它可能是一个像''\ r''这样的空格。有时这发生在混合编辑器中。如果需要,删除第19行并重新输入。 – chux

回答

0
#include <stdio.h> 

#include <string.h> 

#include <stdlib.h> 





struct data_node { 

    char name [25]; 

    int data; 

    struct data_node *next; 

    }; 

您已声明结构data_node两次,写的#include两次。

+0

我的坏,我删除了重复,但仍然给了我同样的错误 – user2864813

+0

@ user2864813你定义结构data_node两次 – LearningC

0

您还需要从switch块中移动变量定义,并将它们放在main开头。

我谈论的线路有:

int newelem; 
char newstring [25]; 
int removeelem; 
int findelem; 
+0

我只是把他们在主更新上面的代码,但仍然给我的错误 – user2864813

+0

我看到你复制他们两次。至少,这是我在发布的代码中看到的。 –

+0

抱歉哪行是dups?我有非常糟糕的眼睛 – user2864813

0

它一直以来我用C已设定一段时间,但我好像记得,如果他们的int main(void) {}功能之前定义的函数声明是没有必要的。我认为你的其他函数中有一个错误会阻止编译器找到main函数。尝试将它们放在main以下,看看会发生什么。