Thread: Linked list program, not recognizing a function

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    5

    Linked list program, not recognizing a function

    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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile at maximum warning level and/or get a better compiler with better diagnostic messages:
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:70:45: error: called object ‘count’ is not a function or function pointer
                     printf("Count is %d\n",count(list, listnum));
                                                 ^
    foo.c:25:16: note: declared here
         int check, count, selection, listnum;
                    ^
    make: *** [foo] Error 1
    Notice that you have a function and variable with the same name. The compiler is rightly confused. One, or both of those, should have a more descriptive name, such as a function count_all_the_things() and a variable count_of_things, or number_of_things.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    5
    Thank you! That fixed that problem.

    Now I've run it and seem to get stuck inside a single option. ("Now what would you like to append?" '5' "Now what would you like to append?"......"

    Is the do while loop unnecessary? I'm not super familiar with switch statements so I wasn't sure.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    5
    Nevermind, I realized I simply left out the menu from the do while loop, thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-08-2014, 02:52 PM
  2. Replies: 13
    Last Post: 09-22-2013, 10:34 PM
  3. delete_list function in linked list program
    By amazingdave5 in forum C Programming
    Replies: 4
    Last Post: 04-05-2012, 08:02 PM
  4. linked list noob - function create new list
    By dukysta in forum C Programming
    Replies: 5
    Last Post: 07-06-2007, 08:16 AM
  5. Replies: 12
    Last Post: 01-28-2006, 07:40 AM

Tags for this Thread