链表问题
- C/C++ code
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define OK 0#define ERROR -1typedef int Status;typedef int ElemType; // waring : data == number!!!typedef struct Node{ ElemType data; struct Node * next;}Node, * Pnode;//==========================void status_( int a );Status InitList( Pnode head );Status InsertFirstList( Pnode head, Pnode & p );//Status InsertLastList( Pnode head );Status ViewList( Pnode Fhead );int main(){ int status, choice; Pnode head = NULL; Pnode Fhead = NULL; status = InitList( head ); status_( status ); puts( "The List has been Initlize" ); puts( "No 1 InsertFirstList"); puts( "No 2 InsertLastList" ); printf( "choice:___\b\b" ); scanf( "%d", &choice ); switch( choice ) { case 1: status = InsertFirstList( head, Fhead ); status_( status ); break; case 2: //status = InsertLastList( ); //status_( status ); break; case 3: status = ViewList( Fhead ); status_( status ); break; default: puts( "wrong choice!! " ); break; } return 0;}//=============================void status_( int a ){ if( ERROR == a ) printf( "wrong!\n" ); if( OK == a ) printf( "OK!\n" );}Status InitList( Pnode & head ){ head = NULL; return OK;}Status InsertFirstList( Pnode head, Pnode & p ){ int data; Pnode Pnew; Pnew = ( Pnode )malloc( sizeof(Node) ); if( !Pnew ) exit(-1); // malloc fail!!! exit printf( "input a data: " ); scanf( "%d", &data ); Pnew->next = head; // insert from head Pnew->data = data; p = Pnew; return OK;}Status ViewList( Pnode Fhead ){ Pnode p; p = Fhead; while( p != NULL ) { p = p->next; printf( "%d", p->data ); } return OK;}error LNK2001: unresolved external symbol "int __cdecl InitList(struct Node *)" (?InitList@@YAHPAUNode@@@Z)
fatal error LNK1120: 1 unresolved externals
没有语法错误,这是什么错误啊?
[解决办法]
问题出在你的函数InitList,在前向引用声明的时候所声明的版本,和你实际实现的版本是不一致的,一个有引用,一个没有引用。
- C/C++ code
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define OK 0#define ERROR -1typedef int Status;typedef int ElemType; // waring : data == number!!!typedef struct Node { ElemType data; struct Node * next;} Node, *Pnode;//==========================void status_(int a);[b][color=#FF0000]Status InitList(Pnode &head);[/color][/b]Status InsertFirstList(Pnode head, Pnode & p);//Status InsertLastList( Pnode head );Status ViewList(Pnode Fhead);int main() { int status, choice; Pnode head = NULL; Pnode Fhead = NULL; status = InitList(head); status_(status); puts("The List has been Initlize"); puts("No 1 InsertFirstList"); puts("No 2 InsertLastList"); printf("choice:___\b\b"); scanf("%d", &choice); switch (choice) { case 1: status = InsertFirstList(head, Fhead); status_(status); break; case 2: //status = InsertLastList( ); //status_( status ); break; case 3: status = ViewList(Fhead); status_(status); break; default: puts("wrong choice!! "); break; } return 0;}//=============================void status_(int a) { if (ERROR == a) printf("wrong!\n"); if (OK == a) printf("OK!\n");}Status InitList(Pnode & head) { head = NULL; return OK;}Status InsertFirstList(Pnode head, Pnode & p) { int data; Pnode Pnew; Pnew = (Pnode) malloc(sizeof(Node)); if (!Pnew) exit(-1); // malloc fail!!! exit printf("input a data: "); scanf("%d", &data); Pnew->next = head; // insert from head Pnew->data = data; p = Pnew; return OK;}Status ViewList(Pnode Fhead) { Pnode p; p = Fhead; while (p != NULL) { p = p->next; printf("%d", p->data); } return OK;}