2017-04-08 47 views
0

我得到賦值時將整數指針,未上線46和53,這兩條線兩邊有兩個星號鑄造錯誤,我用我的生命,我想不出爲什麼。鏈接列表對我來說是非常新的,我不知道它們是如何完全工作的。鏈接表指針不進行強制轉換錯誤

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

struct node_def 
{ 
    int data; 
    struct node_def *next; 
}; 
typedef struct node_def node; 

node *makeNode (int a); 
node *insertFront(node *head,node *new); 
void printList(node *head); 

int numNodes = 0; 

int main() 
{ 
    srand(time(0)); 
    int r = rand() % 10000; 
    int i = 0; 

    node *head = NULL; 
    node *tmp = NULL; 

    printf("How many nodes? ", numNodes); 
    scanf("%d", numNodes); 
    printf("\n"); 

    head = insertFront(head, tmp); 

    for(i = 0; i < numNodes; i++) 
    { 
     makeNode(r); 
     printList(head); 
    } 
    printf("\n"); 

    return 0; 
} 
node *makeNode (int a) 
{ 
    node *new = malloc(sizeof(node)); 
    **new = a;** 

    return new; 
} 
node *insertFront(node *head, node *new) 
{ 

    **new->data = head;** 
    new->next = new; 

    return 0; 
} 
void printList(node *head) 
{ 
    int j = 0; 
    for(j = 0; j < numNodes; ++j) 
    { 
     while (head != NULL) 
     { 
      printf(" %4d", head->data); 
      head = head->next; 
     } 
     if(j % 10 == 0) 
      printf("\n"); 
    } 
    return; 
} 

新=一個是爲了使新的節點,並從0爲它們分配一個隨機數 - 9999

+1

提示:什麼類型是'了'?它是一個指針嗎?同樣,「頭」是什麼類型?它是一個'int'嗎? –

+0

無論這些評論幫助我的,我已經試過所有我能想到的,使這兩條線停止給我的錯誤,從星號和&符號的對方,各種連擊開關變量的地方,沒有什麼工作。我需要更多的指導。 – Boo92

+0

'新=一個;' - >'如果(新){新建 - >數據= A; new-> next = NULL; }' – BLUEPIXY

回答

0

你嘗試分配R鍵新,但新是一個結構。 你讓一個指針結構:節點*新 你想要做什麼是新 - 分配R>數據,這是一個int。

node *insertFront(node *head, node *new) 
{ 
**new->data = head;** // ** is meaningless 
new->next = new; // new is a reserved key word, don't use it this way 
return 0; 
} 

你試圖做的是把一個NULL指針作爲你的列表頭。 只需按下元素進去你makeNode函數中。 插入這樣的:

void createNode(node *head) 
{ 
    Node *new_node = malloc(sizeof(Node*)); 
    new_node->data = rand() % 100000; 
    new_node->next = NULL; 
    if(head == NULL) 
     head = new_node; 
    else if(head != NULL) 
     //Here you have to adapt your list, search (linked list crud functions) 
} 

你有什麼指針是一個糟糕的理解。 希望它可以幫助兄弟