-
Linked List
Ok...I'm trying to grab (scanf) an itemName and a value (price) from the user - this program compiles, but when i try to run the program, it says :
1) linkList error LNK2019: unresolved external symbol "struct node * __cdecl search(struct node *,float)" (?search@@YAPAUnode@@PAU1@M@Z) referenced in function _main
2) linkList error LNK2019: unresolved external symbol "int __cdecl deletenode(struct node *,char * const)" (?deletenode@@YAHPAUnode@@QAD@Z) referenced in function _main
3) linkList error LNK2019: unresolved external symbol "int __cdecl insertnode(struct node *,char * const)" (?insertnode@@YAHPAUnode@@QAD@Z) referenced in function _main
Can anyone help? Here's my code:
Code:
//Declarations for Linked List
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DUMMY_TRAILER 100 /*Assume all valid numbers fall between 1 and 99*/
#define DUPLICATE 1
#define NEW_NODE 0
#define FOUND 1
#define NOT_FOUND 0
typedef struct node NODE;
struct node
{
char itemName[10];
float value;
NODE* next;
};
/* Function prototypes */
NODE* init_list(void);
int insertnode(NODE*, char []);
int deletenode(NODE*, char []);
NODE* search(NODE*, float value);
void traverse(NODE*);
/* Driver to test the functions */
int main()
{
NODE* list, *nodeptr;
char itemName[10];
float value;
int found, duplicate, number=0;
list = init_list();
while(printf("Enter an item and it's price, or -1 to quit: "), scanf("%d", "%c", &value, &itemName),
number != -1)
{
duplicate = insertnode(list, itemName);
if (duplicate)
{
printf("That item is already in the list.\n");
} /*end of if*/
} /*end of while*/
traverse(list); /*Print out the values in the list*/
while(printf("Enter an item and it's price, or -1 to quit: "), scanf("%d", "%c", &value, &itemName),
number != -1)
{
found = deletenode(list, itemName);
if (!found)
{
printf("Could not find the item with value of: %c\n", itemName);
} /*end of if*/
else
{
printf("Deleted %c\n", itemName);
} /*end of else*/
} /*end of while*/
traverse(list);
while(printf("Enter an item and it's price, or -1 to quit: "), scanf("%d", "%c", &value, &itemName),
number != -1)
{
nodeptr = search(list, value);
if (!nodeptr)
{
printf("Could not find node with value of: %c\n", itemName);
} /*end of if*/
else
{
printf("Node with value of %d was found!\n", nodeptr->value);
} /*end of else*/
} /*end of while*/
return 0;
} /*end of main*/
/* Initilization of an empty ordered linked list */
NODE* init_list(void)
{
NODE* list;
if ( (list = (NODE*)malloc(sizeof(NODE)) ) == NULL)
{
printf("Fatal malloc error in main\n");
exit(1);
} /*end of if*/
list->value = 0;
if ((list->next = (NODE*)malloc(sizeof(NODE))) == NULL)
{
printf("Fatal malloc error in main\n");
exit(2);
} /*end of if*/
list->next->value = DUMMY_TRAILER;
list->next->next = NULL;
return list;
} /*end of init_list*/
/* Insert nodes into ordered linked list */
int insertnode(NODE* list, float value, char itemName[])
{
NODE* current = list->next;
NODE* previous = list;
NODE* newptr;
while(value > current->value)
{
previous = current;
current = current->next;
} /*end of while*/
if (value == current->value)
{
return DUPLICATE;
} /*end of if*/
else
{
if ( (newptr = (NODE*)malloc(sizeof(NODE))) == NULL)
{
printf("Fatal malloc error in insert!\n");
exit(3);
} /*end of if*/
/*Fill up new node and link it in*/
newptr->value = value;
strcpy(newptr->itemName, itemName);
newptr->next = current;
previous->next = newptr;
return NEW_NODE;
} /*end of else*/
} /*end of insert*/
/*Delete a node from ordered linked list*/
int deletenode(NODE* list, int value)
{
NODE* current = list->next;
NODE* previous = list;
while(value > current->value)
{
previous = current;
current = current->next;
} /*end of while*/
if (value != current->value)
{
return NOT_FOUND;
} /*end of if*/
else
{
previous->next = current->next;
free(current);
return FOUND;
} /*end of else*/
} /*end of delete*/
/*Find a node in an ordered linked list*/
NODE* search(NODE* list, int value)
{
list = list->next; /*skip dummy header*/
while (value >= list->value)
{
if (value == list->value)
{
return list;
} /*end of if*/
list = list->next;
} /*end of while*/
return NULL;
} /*end of search*/
/*Traverse the nodes in an ordered linked list*/
void traverse(NODE* list)
{
list = list->next; /*Skip over dummy node*/
while(list->value != DUMMY_TRAILER)
{
printf("%d\n", list->value);
list = list->next;
} /*end of while*/
} /*end of traverse*/
-
Check the arguments that you have in your function prototypes, and then see if they match the arguments where you actually have the function code. You'll notice some discrepancies.
-
OK...my function prototypes are now:
Code:
/* Function prototypes */
NODE* init_list(void);
int insertnode(NODE*, float value, char itemName[]);
int deletenode(NODE*, float value, char itemName[]);
NODE* search(NODE*, float value, char itemName[]);
void traverse(NODE*);
Made sure everything corresponds....still the same errors?
-
You also forgot to look at what you're passing to the functions when you're calling them.
-
Your prototypes have changed, have you also changed the functions themselves and what they take. If so, then you are making function calls and not passing the correct amount of parameters. check out line 9, in the main() function. It only passes 2 parameters, the functions needs 3. Fix these and see what, if any errors you get.
Hint: You may need to write down your prototypes, your functions location and the parameters it takes, and each call to those functions, make them all match.