#include #include struct Node { struct Node * next; int key; }; /* * Insert a new node first in the list. The argument head is assumed to be NULL * when the list is empty. */ struct Node * push_front(struct Node * head, int key){ struct Node * newNode = (struct Node*)malloc(sizeof(struct Node)); (*newNode).key = key; // newNode->key = key; newNode->next = head; return newNode; } struct Node * push_back(struct Node * head, int key){ struct Node * current; struct Node * newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->key = key; newNode->next = NULL; // speciall fall för tom lista if(head == NULL){ return newNode; } // ta oss till sista elementet current = head; while(current->next != NULL){ current = current->next; } current->next = newNode; return head; } void printList(struct Node * head){ while(head){ printf("%i ", head->key); head = head->next; } } void freeList(struct Node * head){ struct Node * nextNode; while(head){ nextNode = head->next; free(head); head = nextNode; } } // returnera key för först elementet, ta sedan bort första elementet. int pop_front(struct Node ** head){ int tmp; tmp = (*head)->key; *head = (*head)->next; return tmp; } int main(int argc, char **argv) { struct Node * listHead; int tmp = 9; listHead = NULL; freeList(listHead); listHead = push_front(listHead, 89); tmp = pop_front(&listHead); while(tmp-- > 0) listHead = push_front(listHead, tmp); tmp = 5; while(tmp-- > 0) listHead = push_back(listHead, tmp); printList(listHead); freeList(listHead); printf("hello world\n"); return 0; }