Created
June 7, 2017 01:55
-
-
Save martyworm/067ddb8c6822fe10be4cc2476a3ec330 to your computer and use it in GitHub Desktop.
[c] attempting to create a simple linked list but am getting an extra node with value 0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//cant get rid of the zero | |
#include <stdlib.h> | |
#include <stdio.h> | |
#define MAXNODES 3 | |
typedef struct node | |
{ | |
int val; | |
struct node* next; | |
}node; | |
int main(void) | |
{ | |
node* first, *newn; | |
int x = 0; | |
first = malloc(sizeof(node)); | |
first->next = NULL; | |
newn = first; | |
//this initializes the nodes with data 1-whatever | |
for(int i = 0; i < MAXNODES; i++) | |
{ | |
newn->next = malloc(sizeof(node)); | |
newn->val = x + 1; | |
newn = newn->next; | |
newn->next = NULL; | |
x++; | |
} | |
newn = first; | |
printf("\n\n"); | |
while (newn) | |
{ | |
printf("node value: %i\n", newn->val); | |
newn = newn->next; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment