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