#include #include struct Node { struct Node * next; int key; }; struct Node * push_front(struct Node * head, int key){ struct Node * newNode; 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 * newNode, * firstNode; newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->next = NULL; newNode->key = key; if(head==NULL){ return newNode; } // leta upp sista noden i gamla listan firstNode = head; while(head->next){ head = head->next; } head->next = newNode; return firstNode; } 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 nyckel för första noden, ta sedan bort första noden int pop_front(struct Node ** phead){ int tmp; tmp = (*phead)->key; // (**phead).key; *phead = (*phead)->next; return tmp; } int main(int argc, char **argv) { struct Node * listHead; int tmp; listHead = NULL; tmp = 5; while(tmp-- > 0) listHead = push_back(listHead, tmp); // lägg till ett första element ... listHead = push_front(listHead, 89); tmp = 9; while(tmp-- > 0) listHead = push_front(listHead, tmp); printList(listHead); tmp = pop_front(&listHead); freeList(listHead); printf("hello world\n"); return 0; }