簡體   English   中英

C中的鏈表實現錯誤

[英]Linked List Implementation error in C

首先很抱歉,如果我的問題有點愚蠢,但是從這些愚蠢的錯誤中學習非常重要,特別是當我學習諸如C編程語言中的Linked List之類的新內容時,這就是我在這里實現的原因一個簡單的鏈表,該鏈表使用單獨的函數在列表的開頭插入一個node(element),但始終會發生此問題,我將向您展示代碼,並告訴我是否做錯了很多事情,而不是thanx:

#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;
}

錯誤的意思是:賦值中的類型不兼容

Insert()應該傳遞e*並返回e*

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

插入應該以e* hd作為參數並返回e* 該方法應為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM