Linked List Head Is Always Null [closed]

余生长醉 提交于 2019-12-25 04:19:08

问题


I have a lab assignment where we have to create a linked list. I have written methods to achieve this. I want to be able to print out my linked list when I am testing it. I have a while loop that is supposed to traverse over all of the nodes, but the test condition always fails and I can't figure out why. I put test cases in to see if whenever I push a node onto the list, if the new head is null. Here is the code for my linked list:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"

struct lnode {
char* word;
int count; 
int line;
struct lnode* next;
};

struct lnode* head = NULL;

struct lnode* newNode(char *word, int line) {
struct lnode* tempnode;
char* new = word;
tempnode = (struct lnode *)malloc(sizeof(struct lnode));
tempnode->word = new;
tempnode->count = 1;
tempnode->line = line;
return tempnode;
}

void pushNode(struct lnode** head, struct lnode* node) {
if(head == NULL) {
    head = node;
    head = nodeGetNext(head);
    head = NULL;
}
else {
    node->next = head;
    node = nodeGetNext(node);
    node = head;
}
}

struct lnode* nodeGetNext(struct lnode* node) {
return node->next;
}

char* nodeGetWord(struct lnode* node) {
return node->word;
}

int main() {
struct lnode* a;
struct lnode* b;
struct lnode* c;
struct lnode* d;
struct lnode* e;
a = newNode("Hello", 0);
b = newNode("Bonjour", 1);
c = newNode("Hola", 2);
d = newNode("Bonjourno", 3);
e = newNode("Hallo", 4);
pushNode(head, a);
if(head == NULL)
    printf("YES");
pushNode(head, b);
if(head == NULL)
    printf("YES");
pushNode(head, c);
if(head == NULL)
    printf("YES");
pushNode(head, d);
if(head == NULL)
    printf("YES");
pushNode(head, e);
if(head == NULL)
    printf("YES");
printList();

return 0;
}

void printList() {
printf("Hello\n");
struct lnode *currentnode;

currentnode = head;

while (currentnode != NULL) {
    printf("Hello");
    printf("%s:\n",nodeGetWord(currentnode));
    currentnode = nodeGetNext(currentnode);
}
}

回答1:


in pushNode() you do: head = NULL;. head is a pointer to a pointer...

And to wrap it all up... on the next run, head is NULL again.....




回答2:


I will not spoil the correct answer (it looks like homework...?) But nevertheless here is a hint: these messages are given by the compiler:

prova.c:31:10: warning: assignment from incompatible pointer type [enabled by default]<br>
prova.c:32:5: warning: passing argument 1 of ‘nodeGetNext’ from incompatible pointer type [enabled  by default]<br>
prova.c:25:15: note: expected ‘struct lnode *’ but argument is of type ‘struct lnode **’
prova.c:32:10: warning: assignment from incompatible pointer type [enabled by default]<br>
prova.c:36:16: warning: assignment from incompatible pointer type [enabled by default]<br>
prova.c:38:10: warning: assignment from incompatible pointer type [enabled by default]
...

and this shows that something is probably wrong.

Furthermore, modifying the function argument head (and not the memory it points to) will have little effect... (hint, hint!)



来源:https://stackoverflow.com/questions/12556595/linked-list-head-is-always-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!