简体   繁体   中英

Linked List Implementation error in C

First of all sorry if my question is a little bit silly but it is really important to learn from these stupid mistakes specially when I'm learning something new like Linked List in C programming language which is why I'm here, I'm implementing a simple linked list using a seperate function that insert a node(element) in the start of list but This problem always happens, I'll show you the code and tell me if I'm doing something wrong and thanx alot:

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

typedef struct element{
    int nb;
    struct element *next;
}e;
e Insert(e hd,int x){
    e *temp = (e*)malloc(sizeof(e));
    temp->nb=x;
    temp->next = hd; /*It shows that the error is here, all what im doing here is that im letting the temp element points to whichever the head elements in pointing at so it can take it place as the first element)*/
    return temp; /*here I'm returning the @ of temp to the head node*/
}
int main(int argc, char *argv[]) {
    e *head=NULL;
    head=Insert(head,5);
    system("PAUSE");    
    return 0;
}

and what the error says is : incompatible types in assignment

Insert() should pass e* in and returns e* .

e* Insert(e* hd,int x){
    e *temp = malloc(sizeof(e));
    temp->nb=x;
    temp->next = hd;
    return temp;
}

Insert should be taking e* hd as argument and returning e* . The method should be:

e* Insert(e* hd,int x){...}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM