Assigning a value to an integer in a C linked list
Posted
by Drunk On Java
on Stack Overflow
See other posts from Stack Overflow
or by Drunk On Java
Published on 2010-06-15T22:03:32Z
Indexed on
2010/06/15
22:12 UTC
Read the original article
Hit count: 188
c
Hello all. I have a question regarding linked lists. I have the following structs and function for example.
struct node {
int value;
struct node *next;
};
struct entrynode {
struct node *first;
struct node *last;
int length;
};
void addnode(struct entrynode *entry) {
struct node *nextnode = (struct node *)malloc(sizeof(struct node));
int temp;
if(entry->first == NULL) {
printf("Please enter an integer.\n");
scanf("%d", &temp);
nextnode->value = temp;
nextnode->next = NULL;
entry->first = nextnode;
entry->last = nextnode;
entry->length++;
} else {
entry->last->next = nextnode;
printf("Please enter an integer.\n");
scanf("%d", nextnode->value);
nextnode->next = NULL;
entry->last = nextnode;
entry->length++;
}
}
In the first part of the if statement, I store input into a temp variable and then assign that to a field in the struct. The else branch, I tried to assign it directly which did not work. How would I go about assigning it directly?
Thanks for your time.
© Stack Overflow or respective owner