#include #include struct Node { int key; struct Node *nextNode; }; // push element struct Node* insertAtFront(struct Node *listStart, int key) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->nextNode = listStart; listStart = newNode; newNode->key = key; return listStart; } // pop element int popFromFront(struct Node **pListStart) { struct Node* newListStart; int returnKey; newListStart = (*pListStart)->nextNode; returnKey = (*pListStart)->key; free(*pListStart); *pListStart = newListStart; return returnKey; } // delete first occurance of a node with a matching key. // // obs, denna kommer inte fungera om elementet ligger först i elementet. // denna funktion tar bort första noden med en matchande nyckel void deleteNodeWithKeyMatch(struct Node *listStart, int key) { struct Node *currentNode, *lastNode; currentNode = listStart->nextNode; lastNode = listStart; while( currentNode = currentNode->nextNode ) { if(currentNode->key == key) { lastNode->nextNode = currentNode->nextNode; free(currentNode); break; } lastNode = currentNode; } return; } int main(int argc, char* argv[]) { char a; struct Node* currentNode; // allocate memory for a node on the heap struct Node* listStart = (struct Node*)malloc(sizeof(struct Node)); // Add a first item listStart->nextNode = NULL; listStart->key = 23; // add some more nodes listStart = insertAtFront(listStart, 89 ); a=10; while(a-- > 0) listStart = insertAtFront(listStart, a); // poppa 5 noder och skriv ut dem a = 5; while(listStart != NULL && a-- > 0) { printf(" %d", popFromFront(&listStart)); } // Skriv ut de resterande noderna två rader nedanför currentNode = listStart; printf("\n\n"); while(currentNode != NULL) { printf(" %d", currentNode->key); currentNode = currentNode->nextNode; } // waiting for a keystroke before terminating a = getchar(); return 0; }