0
typedef struct node{        
int data; 
struct node *next; 
} Node, *NodePtr;

int main(…){
NodePtr firstNode = NULL;
…
}

NodePtr insertAtHead(NodePtr head, int data) {
/* create and fill the new node*/
NodePtr newNode = (NodePtr)malloc(sizeof(Node));  /*check that malloc does not return NULL, if yes – output an error and exit */
newNode->data = data;
newNode->next =NULL;
/* add it to the beginning of linked list*/
if  (firstNode==NULL) /*linked list is empty*/
    firstNode=newNode;
else {
    newNode->next = firstNode;
    firstNode = newNode; }
return firstNode; }

I received this code to base my homework on. I'm having issues passing the node pointer (firstNode). I get an error: conflicting types for 'insertAtHead'. I do see what I think is a problem in the definition. The first node is called head but everwhere else its called firstNode. I did make that change, I'm just lost as to how to pass this pointer. Just to show the original code we were given, I posted the code directly from the lecture notes. Thanks for the help in advance.

Kara
  • 6,115
  • 16
  • 50
  • 57
  • `firstNode = insertAtHead(firstNode, data);`. and the insertion code itself is utterly wrong. there is no `firstNode` in-scope of the function, and there shouldn't be. No `if's` are required in that function (except maybe to validate you [incorrectly-cast `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) worked). – WhozCraig Feb 06 '14 at 02:34
  • that is how i was calling it. is it possibly an error with my environment? – Charles Finley Feb 06 '14 at 02:36
  • `head` should be used within the body of the function where you have `firstNode` (or rename the parameter to `firstNode`). then lose the if-check, the line after it, and the else line. – WhozCraig Feb 06 '14 at 02:37
  • Based on the error message, I think the issue is in the `...` in `main`. "conflicting types for..." errors usually occur when something is declared twice, or in this case, referred to before it is defined. I think you'll remove the original error if you put your `insertAtHead` function above `main` – Scott Mermelstein Feb 06 '14 at 02:40
  • thanks scott, that got rid of my error, i thought it was just me trying to pass things wrong.... – Charles Finley Feb 06 '14 at 02:43

1 Answers1

0

You're coding the right solution, but doing it as if it isn't in a function. It should look like this:

#include <stdlib.h>

typedef struct node
{
    int data; 
    struct node *next; 
} Node, *NodePtr;

// prototypes
NodePtr insertAtHead(NodePtr head, int data);

int main()
{
    NodePtr head = NULL;
    head = insertAtHead(head, 1);
    head = insertAtHead(head, 2);

    // etc.
}

NodePtr insertAtHead(NodePtr head, int data) 
{
    NodePtr newNode = malloc(sizeof(*newNode));
    if (!newNode)
    {
        perror("failed to allocate node");
        exit(EXIT_FAILURE);
    }
    newNode->data = data;
    newNode->next = head;
    return newNode; 
}

Note (1) the placement of the function prototype. This will quell your first error. (2) the logic in the insertion function. this will solve the next error concerning an unknown variable firstNode as well as properly chain the node as the new list head, returning the new head for the caller.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • thanks for all the help, the prototype slipped my mind. i also didnt know there was an exit(EXIT_FAILURE); i just had it returning the unchanged firstNode. – Charles Finley Feb 06 '14 at 02:53
  • @CharlesFinley If the caller got back the original node they would be oblivious to the fact that the insertion failed. Honestly its the reason I prefer passing the head pointer by-address (a pointer-to-pointer) to modify it in-place, and returning a status `int` to indicate success or failure to the caller. Glad it works for you, btw. – WhozCraig Feb 06 '14 at 03:02