Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save martyworm/067ddb8c6822fe10be4cc2476a3ec330 to your computer and use it in GitHub Desktop.
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
//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