-1

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);
}
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Matt Altepeter
  • 317
  • 2
  • 5
  • 17

2 Answers2

2

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.....

Gung Foo
  • 13,392
  • 5
  • 31
  • 39
0

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!)

Littm
  • 4,923
  • 4
  • 30
  • 38
  • Okay..well I removed all of the warnings and I still get NULL for head in my test cases – Matt Altepeter Sep 24 '12 at 02:11
  • modifying head (and not *head) was the biggest mistake... doing this you are just modifying a temporary variable in the stack of the function, you can't expect the list to be modified! – Maurizio Monge Sep 25 '12 at 15:41