我是C编程的新手,我正在做一个链表,以便我可以找到,显示,插入、删除和排序节点;但是,编译器给了我这些错误:
main.c:1:19:警告:指令末尾 #include 额外的令牌 [默认启用]
/usr/lib/../lib64/crt1.o:在函数
_start': (.text+0x20): undefined reference to
主 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: %dn", 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: %dn", 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 foundn");
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 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct data_node {
char name [25];
int data;
struct data_node *next;
};
您已经声明了两次结构data_node,并编写了两次 #include。
还需要从switch
块中移动变量定义并将它们放在main
的开头。
我说的台词是:
int newelem;
char newstring [25];
int removeelem;
int findelem;
自从我用 C 编程以来已经有一段时间了,但我似乎记得如果在函数之前定义函数声明,则不需要函数声明int main(void) {}
。我认为您的其他函数之一存在错误,导致编译器无法找到main
函数。试着把它们放在main
下面,看看会发生什么。