警告:#include指令末尾的额外令牌

时间:2014-03-31 18:06:52

标签: c pointers struct linked-list

我是C编程的新手,我正在做一个链表,这样我才能找到,显示, 插入,删除和排序节点;但是,编译器给我这些错误:

  

main.c:1:19:警告:#include指令末尾的额外令牌[默认启用]

     

/ usr / lib /../ lib64 / crt1.o:在函数_start': (.text+0x20): undefined reference to main'   collect2:错误:ld返回1退出状态

我没有线索是什么。

#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 */

3 个答案:

答案 0 :(得分:0)

#include <stdio.h>

#include <string.h>

#include <stdlib.h>





struct data_node {

   char name [25];

   int data;

   struct data_node *next;

   };

你已经两次声明了struct data_node并且写了#include两次。

答案 1 :(得分:0)

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

我所说的话是:

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

答案 2 :(得分:0)

自从我用C编程以来已经有一段时间了,但我似乎记得如果在int main(void) {}函数之前定义它们,则不需要函数声明。我认为您的其他一个函数中存在错误,导致编译器无法找到main函数。尝试将它们放在main下方,看看会发生什么。