I'm printing out the Head and Tail of the Linked List when an element is added, Fairly simple.
int main(){
struct node{
struct node* next;
struct node* previous;
double value;
};
struct LinkedList{
struct node* head;
struct node* tail;
};
void addValue(struct LinkedList* list,double newValue){
struct node newNode;
newNode.next = NULL;
newNode.value=newValue;
if(list->head == NULL){
newNode.previous=NULL;
list->head= &newNode;
list->tail=&newNode;
}
else
{
newNode.previous= list->tail;
list->tail->next= &newNode;
list->tail= &newNode;
}
printf("%f\n",list->head->value);
printf("%f\n",list->tail->value);
}
struct LinkedList l1;
l1.head=NULL;
l1.tail=NULL;
addValue(&l1,5);
addValue(&l1,6);
addValue(&l1,7);
addValue(&l1,8);
}
But the output I get is
5.000000 5.000000 6.000000 6.000000 7.000000 7.000000 8.000000 8.000000
Instead what I expect
5.000000 5.000000 5.000000 6.000000 5.000000 7.000000 5.000000 8.000000
Any idea why?
malloccommand instead of on the stack as currently is done.