I'm writing a linked list program for class that has a simple text based menu and I believe I am nearly done, it just wont recognize my "count" function as a function and I don't know why. Was hoping someone could point a something simple I'm overlooking.
The error comes up at line 70.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int datum;
struct node *next;
} node_t;
node_t * prepend(node_t *, int);
void print_list(node_t *);
node_t * append(node_t *, int);
node_t * delete(node_t *, int);
int contains(node_t *, int);
int count(node_t *, int);
int
main(int argc, const char **argv)
{
int check, count, selection, listnum;
node_t *list;
list = NULL;
printf("1. Prepend an Item\n");
printf("2. Append an Item\n");
printf("3. Delete an Item\n");
printf("4. Check for an Item\n");
printf("5. Count the number of items\n");
printf("6. Print the list\n");
printf("7. Quit\n");
printf("Choose an option: ");
scanf("%d", &selection);
do
{
switch(selection){
case 1:
printf("What number would you like to prepend?\n");
scanf("%d", &listnum);
list = prepend(list, listnum);
break;
case 2:
printf("What number would you like to append?\n");
scanf("%d", &listnum);
list = append(list, listnum);
break;
case 3:
printf("What number would you like to delete?\n");
scanf("%d", &listnum);
list = delete(list, listnum);
break;
case 4:
printf("What number would you like to check for?\n");
scanf("%d", &listnum);
check = contains(list, listnum);
printf("%d\n", check);
break;
case 5:
printf("What number would you like to count?\n");
scanf("%d", &listnum);
printf("Count is %d\n",count(list, listnum));
break;
case 6:
print_list(list);
break;
case 7:
/**/
break;
default:
printf("Invalid selection\n");
break;
}
} while (selection != 7);
return 0;
}
node_t *
prepend(node_t *head, int x)
{
node_t *nhead = (node_t*)malloc(sizeof(node_t));
nhead->datum = x;
nhead->next = head;
head = nhead;
return head;
}
void
print_list(node_t *head)
{
if (head == NULL){
printf("NULL");
} else {
printf("%d->",head->datum);
print_list(head->next);
}
}
node_t *
append(node_t *head, int x)
{
if(head == NULL){
head = (node_t*)malloc(sizeof(node_t));
head->datum = x;
head->next = NULL;
} else{
head ->next = append(head->next,x);
}
return head;
}
node_t *
delete(node_t *head, int x)
{
if (head == NULL){
return head;
} else if (head->datum==x){
node_t *tail = head->next;
head->next=NULL;
free(head);
return tail;
} else {
head->next = delete(head->next,x);
return head;
}
}
int
contains(node_t *head, int x)
{
if(head == NULL){
return 0;
} else if(head->datum == x){
return 1;
} else{
return contains(head->next, x);
}
}
int
count(node_t *head, int x)
{
node_t *ptr;
int count;
count = 0;
for(ptr = head; ptr != NULL; ptr = ptr->next){
if(ptr->datum == x){
count++;
}
}
return count;
}