0

I'm trying to further my understanding on Linked lists, nodes and pointers. I've written some code, received some compiling errors that I'm not sure how to fix and was hoping to get some direction here. Below is my code + compile errors at the bottom.

#include <stdlib.h>
#include <stdio.h>

typedef struct _node 
{
    int data;
    struct _node * next;
} node_t;

typedef struct 
{
    node_t * head;
    node_t * tail;  
} LL_t;

unsigned int removeNumber(LL_t * L, int target);

LL_t * LLcreate() {
    LL_t * ret = malloc(sizeof(LL_t));
    if (ret != NULL) {
        ret->head = NULL;
        ret->tail = NULL;
        }
    return ret;
}

// Adds a new element to the tail end of a list
void LLappend(LL_t * LL, int value) {
    node_t * newNode = malloc(sizeof(node_t));
    if (newNode != NULL) {
        newNode->data = value;
        LLappendNode(LL, newNode);
    }
}

void LLappendNode(LL_t * LL, node_t * newNode) {
    if (newNode != NULL) {
        newNode->next = NULL;
        if (LL->tail == NULL) {
            // empty list
            assert(LL->head == NULL);
            LL->head = newNode;
            LL->tail = newNode;
        } else {
            // non empty list
            LL->tail->next = newNode; // seg fault
            LL->tail = newNode;
        }
    }
}



// Post: removes data target from list L
unsigned int removeNumber(LL_t * LL, int target) {
int count=1;
node_t * curr = LL->head;

while(LL->head && LL->head->data == target){
    node_t * temp= LL->head;
    LL->head=LL->head->next;
    free(temp);
    count=0;
    }

while (curr->next!=NULL){
    node_t * temp = curr->next; 
    if (curr->next->data == target){
        curr->next = temp->next;
        curr=curr->next;
        free(temp); 
        count=0;
        return count;
        }
    }
}

int main(){
    int LL[6]={1,2,5,3,4,6};
    int i;
    LLcreate();
    for (i=0; i<10; i++)
        {
            LLappend(LL_t * LL, i);
        }
    for (i=0; i<10;i++)
        {
            printf("%d", LL[i]);
        }

}

Compiling errors

splcie.c:36:6: warning: conflicting types for 'LLappendNode' [enabled by default]
 void LLappendNode(LL_t * LL, node_t * newNode) {
      ^
splcie.c:32:9: note: previous implicit declaration of 'LLappendNode' was here
         LLappendNode(LL, newNode);
         ^
splcie.c: In function 'main':
splcie.c:84:22: error: expected expression before 'LL_t'
             LLappend(LL_t * LL, i);
                      ^
splcie.c:84:22: error: too few arguments to function 'LLappend'
splcie.c:28:6: note: declared here
 void LLappend(LL_t * LL, int value) {
      ^
3
  • 1
    Move the definition of LLappendNode() above LLappend(), or provide a prototype. Remove the LL_t* in LLappend(LL_t* LL, i); in main(). Commented Mar 24, 2016 at 20:11
  • You are calling LLappendNode from the function LLappend, before it has been defined. You must declare function prototypes at the top after the struct definitions, or define their implementation before they are called. Commented Mar 24, 2016 at 20:15
  • In main, you are trying to pass (also see syntax error noted above by @EOF) an integer array (which decays to pointer) to LLappend, which is expecting a pointer to a struct. Commented Mar 24, 2016 at 20:30

2 Answers 2

1

Error 1: expected expression before 'LL_t' LLappend(LL_t * LL, i);

Fix: wrap LL_t * in parenthesis while typecasting: LLappend((LL_t *) LL, i);

Error 2: warning: conflicting types for 'LLappendNode'

void LLappendNode(LL_t * LL, node_t * newNode) {
  ^

previous implicit declaration of 'LLappendNode' was here LLappendNode(LL, newNode);

Fix: provide a protoype/declration for LLappendNode before calling it here in LLappend:

void LLappendNode(LL_t * LL, node_t * newNode); //prototype

void LLappend(LL_t * LL, int value) {
    node_t * newNode = malloc(sizeof(node_t));
    if (newNode != NULL) {
        newNode->data = value;
        LLappendNode(LL, newNode);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

define LLappendNode before LLappend

and remove LL_t * in main

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.