Thread: prototype poblem

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    prototype poblem

    Hey guys i have something wrong with my prototype the ListNodePtr ListReverse() prototype how am i suppose to declare that prototype if it returns a pointer node?


    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int ListInsert();
    ListNodePtr ListReverse();
    void ListFree();
    void ListTraverse();
    void printSquared();
    
    #define LISTSIZE 6
    #define TRUE 1
    #define FALSE 0
    
    typedef struct ListNode * ListNodePtr;
    
    typedef struct ListNode
    {
      int data;
      ListNodePtr next;
    
    
    }ListNode;
    
    int main (void)
    {
       int data;
       int i;
       ListNodePtr head = NULL;
    
      for(i=0; i<LISTSIZE; i++)
      {
        data = rand();
        
        if(!ListInsert(head, data))
        {
          printf("Insertion of node falied\n");
          ListFree(head);
          return EXIT_FAILURE;
    
        } 
    
    
      }
    
      head = ListReverse(head);
      ListTraverse(head, printSquared);
      ListFree(head);
    
      return 0;
    }
    
    int ListInsert(ListNodePtr * head, int data)
    {
    
      ListNodePtr * newNode;
    
      newNode = malloc(sizeof(ListNode));
    
      if(!newNode)
      {
         printf("Cannnot allocate memory\n");
         return FALSE;
      }
    
       
      newNode->data = data;
      newNode->next = *head;
      *head = newNode;
    
    
     
      return 0;
    }
    
    ListNodePtr ListReverse(ListNodePtr head)
    {
    
      ListNodePtr  next, current, previous;
      current = NULL;
      previous = NULL;
      next = NULL;
    
    
    
      while(current !=NULL)
      {
        next = current -> next;
        current-> next = previous;
        previous = current;
        current = next;
      }
    
       return previous;
    }
    
    void ListFree(ListNodePtr head)
    {
      ListNodePtr current, next;
    
      current = head;
      next = NULL;
    
      while(current != NULL)
      {
        next = current->next;
        free(current);
        current = next;
      }
    
    }
    
    void printSquared(int data)
    {
    
      printf("%d\n",(data * data));
    
    }
    
    void ListTraverse(ListNodePtr head, void (*process)(int))
    {
    
      ListNodePtr current;
      
      current = head;
    
      while(current)
      {
         process(current->data);
         current = current->next;
      }
    
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're trying to use K&R style function declarators it seems, and that's a bad thing. Match your definitions and declarations, and you also need to declare functions that use a typedef after the typedef, or it won't be recognized. There are also a few other minor problems, and the formatting needs work. The following compiles, though I haven't tested it:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define LISTSIZE 6
    #define TRUE 1
    #define FALSE 0
    
    typedef struct ListNode * ListNodePtr;
    
    typedef struct ListNode
    {
      int data;
      ListNodePtr next;
    }ListNode;
    
    int ListInsert(ListNodePtr * head, int data);
    ListNodePtr ListReverse(ListNodePtr head);
    void ListFree(ListNodePtr head);
    void ListFree(ListNodePtr head);
    void printSquared(int data);
    void ListTraverse(ListNodePtr head, void (*process)(int));
    
    int main (void)
    {
      int data;
      int i;
      ListNodePtr head = NULL;
    
      for(i=0; i<LISTSIZE; i++)
      {
        data = rand();
    
        if(!ListInsert(&head, data))
        {
          printf("Insertion of node falied\n");
          ListFree(head);
          return EXIT_FAILURE;
        } 
      }
    
      head = ListReverse(head);
      ListTraverse(head, printSquared);
      ListFree(head);
    
      return 0;
    }
    
    int ListInsert(ListNodePtr * head, int data)
    {
      ListNodePtr newNode;
    
      newNode = malloc(sizeof(ListNode));
    
      if(!newNode)
      {
        printf("Cannnot allocate memory\n");
        return FALSE;
      }
    
      newNode->data = data;
      newNode->next = *head;
      *head = newNode;
    
      return 0;
    }
    
    ListNodePtr ListReverse(ListNodePtr head)
    {
      ListNodePtr  next, current, previous;
      current = NULL;
      previous = NULL;
      next = NULL;
    
      while(current !=NULL)
      {
        next = current -> next;
        current-> next = previous;
        previous = current;
        current = next;
      }
    
      return previous;
    }
    
    void ListFree(ListNodePtr head)
    {
      ListNodePtr current, next;
    
      current = head;
      next = NULL;
    
      while(current != NULL)
      {
        next = current->next;
        free(current);
        current = next;
      }
    }
    
    void printSquared(int data)
    {
      printf("%d\n",(data * data));
    }
    
    void ListTraverse(ListNodePtr head, void (*process)(int))
    {
      ListNodePtr current;
    
      current = head;
    
      while(current)
      {
        process(current->data);
        current = current->next;
      }
    }
    Also, it's generally a poor practice to hide levels of indirection behind a typedef. It confuses people.
    My best code is written with the delete key.

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Thnkx for that.. Would you have ny idea why it wont insert the node?

    I get insertion failes

    but it wont display the stack in gdb

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Would you have ny idea why it wont insert the node?
    Well, I may not be an expert, but I would wager that you're getting the error message because ListInsert always returns 0. If you step through execution of the program, you'll discover that it works just fine. The reversal algorithm doesn't quite do what you want though.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why put the prototype inside the function definition?
    By Overworked_PhD in forum C Programming
    Replies: 14
    Last Post: 01-11-2010, 01:48 PM
  2. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  3. Function prototype questions
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 05:27 PM
  4. Prototype syntax for sub-class constructor
    By starkhorn in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2004, 07:33 AM
  5. call to function without prototype
    By lgbeeb in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 12:20 PM