c - C程序中使用链表时出错

标签 c error-handling linked-list dynamic-memory-allocation singly-linked-list

我正在为类(class)开展一个额外学分项目,具体规范如下:

  • 您将编写一个 C 程序(您应该使用设计工具,但我不想看到它)
  • 程序将使用动态内存创建链表(不允许数组)
  • 该程序将存储无限数量的学生记录(仅受 RAM 限制)。
  • 学生记录将包含学生姓名和年龄……您可能需要添加两个额外字段才能完成此操作。
  • 该程序将为用户提供添加记录的方法。
  • 该程序将有一种方式让用户显示所有记录(仅在屏幕上,无需排序)。
  • 程序需要一种退出方式。

我已经完成了所有代码,但我遇到了这个讨厌的错误。这正是我在计算机上看到的:

1>linkedProject.obj : error LNK2019: unresolved external symbol _add referenced in    function _main
1>E:\Spring 2013\C Programing Class\linkedProject\Debug\linkedProject.exe : fatal error LNK1120: 1 unresolved externals

这是我的代码:

#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define pause system ("pause")

// prototype variables
struct node * initnode(char*, int);
void printnode(struct node*);
void printflist(struct node*);
void add(struct node*);
struct node* searchname(struct node*, char*);

struct node{
    char name[20];
    int age;
    struct node *next;
};

struct node *head = (struct node*) NULL;
struct node *end = (struct node*) NULL;

struct node* initnode(char *name, int age){
    struct node *ptr;
    ptr = (struct node*) calloc(1, sizeof(struct node));
    if(ptr == NULL) 
        return (struct node*) NULL;
    else {
        strcpy(ptr->name, name);
        ptr->age = age;
        return ptr;
     }
}

void printnode(struct node *ptr) {
    printf("Name -> %s\n", ptr->name);
    printf("Age -> %d\n", ptr->age);
}

void printlist (struct node *ptr) {
    while (ptr != NULL) {
        printnode(ptr);
        ptr = ptr->next;
    }
}



main() {
    char name[20];
    int age, choice = 1;
    struct node *ptr;
    while(choice != 3){
        system("cls");
        printf("1. Add a name\n");
        printf("2. List all names\n");
        printf("3. Exit");
        printf("\nEnter Menu Selection: ");
        scanf("%d", &choice);
        switch(choice) {
        case 1: printf("\nEnter a name: ");
            scanf("%s", &name);
            printf("Enter age: ");
            scanf("%d", &age);
            ptr = initnode(name, age);
            add (ptr);
            break;
        case 2: printlist(head);
            break;
        case 3: exit(3);
        default: printf("Invalid Entry");
        }// end of switch


    }// end of while

}// end of main

非常感谢所有帮助!!

最佳答案

链接器告诉您它找不到 add 函数。

您已声明:

void add(struct node*)

在你的原型(prototype)中,但你还没有在任何地方定义它。

关于c - C程序中使用链表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15918367/

相关文章:

c++ - 编译 C 和 C++ 代码时指定 -std

scala - 运行一个异步返回 Try 的函数

postgresql - 陷阱特定的命名唯一约束异常

c - "Access violation writing location"链表错误

c - c中链表的选择排序

C:for 循环与 scanf 的行为真的很奇怪

c - 从 C 中的动态数组列表中删除项目

python - 尝试使用Python使用MagickWand方法给图像加水印

sql - 来自 Powershell Invoke-Sqlcmd 的错误检测并不总是有效?

java - int/String 链表和变量的问题