Last active
August 29, 2015 14:17
-
-
Save alphaKAI/3d6498f023647be3a064 to your computer and use it in GitHub Desktop.
Simple LinkedList written in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stdbool.h> | |
| /* List Structure */ | |
| /* int ListStruct */ | |
| #define TYPE int | |
| typedef struct _listNode { | |
| struct _listNode* prevNode; | |
| struct _listNode* nextNode; | |
| TYPE value; | |
| } listNode; | |
| typedef struct _list{ | |
| listNode* firstNode; | |
| listNode* lastNode; | |
| listNode* thisNode; | |
| } list; | |
| void addNode(list* parentList, TYPE newValue){ | |
| listNode* newNode = (listNode*)malloc(sizeof(listNode)); | |
| newNode->value = newValue; | |
| newNode->nextNode = NULL; | |
| if(parentList->lastNode != NULL){ | |
| parentList->lastNode->nextNode = newNode; | |
| newNode->prevNode = parentList->lastNode; | |
| parentList->lastNode = newNode; | |
| } else { | |
| parentList->firstNode = parentList->lastNode = newNode; | |
| newNode->prevNode = NULL; | |
| } | |
| } | |
| void freeList(list* parentList){ | |
| for(parentList->thisNode = parentList->firstNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->nextNode) | |
| free(parentList->thisNode); | |
| } | |
| unsigned long listLength(list* parentList){ | |
| unsigned long arraySize = 0; | |
| for(parentList->thisNode = parentList->firstNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->nextNode) | |
| arraySize++; | |
| return arraySize; | |
| } | |
| TYPE* toArray(list* parentList){ | |
| int* array; | |
| int count; | |
| unsigned long arraySize; | |
| arraySize = listLength(parentList); | |
| count = 0; | |
| array = (TYPE*)malloc(sizeof(TYPE) * arraySize); | |
| for(parentList->thisNode = parentList->firstNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->nextNode) | |
| array[count++] = parentList->thisNode->value; | |
| return array; | |
| } | |
| TYPE* toReArray(list* parentList){ | |
| int* array; | |
| int count; | |
| unsigned long arraySize; | |
| arraySize = listLength(parentList); | |
| count = 0; | |
| array = (TYPE*)malloc(sizeof(TYPE) * arraySize); | |
| for(parentList->thisNode = parentList->lastNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->prevNode) | |
| array[count++] = parentList->thisNode->value; | |
| return array; | |
| } | |
| bool findNode(list* parentList, TYPE key){ | |
| for(parentList->thisNode = parentList->firstNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->nextNode) | |
| if(parentList->thisNode->value == key) | |
| return true; | |
| return false; | |
| } | |
| listNode* pickupNode(list* parentList, TYPE key){ | |
| listNode* returnNode; | |
| for(parentList->thisNode = parentList->firstNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->nextNode) | |
| if(parentList->thisNode->value == key) | |
| returnNode = parentList->thisNode; | |
| return returnNode; | |
| } | |
| void printAll(list* parentList){ | |
| for(parentList->thisNode = parentList->firstNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->nextNode) | |
| printf("%d\n", parentList->thisNode->value); | |
| } | |
| void printReAll(list* parentList){ | |
| for(parentList->thisNode = parentList->lastNode; parentList->thisNode != NULL; | |
| parentList->thisNode = parentList->thisNode->prevNode) | |
| printf("%d\n", parentList->thisNode->value); | |
| } | |
| /* end of list struct implementation */ | |
| int main(){ | |
| int* array; | |
| int* array2; | |
| int i; | |
| list intList = {}; | |
| //add node | |
| for(i = 0; i < 5; i++) | |
| addNode(&intList, i); | |
| printf("printAll------\n"); | |
| printAll(&intList); | |
| printf("printReAll------\n"); | |
| printReAll(&intList); | |
| printf("toArray test------\n"); | |
| array = toArray(&intList); | |
| for(i = 0; i < listLength(&intList); i++) | |
| printf("%d\n", array[i]); | |
| printf("toReArray test------\n"); | |
| array2 = toReArray(&intList); | |
| for(i = 0; i < listLength(&intList); i++) | |
| printf("%d\n", array2[i]); | |
| printf("findNode test------\n"); | |
| if(findNode(&intList, 2)) | |
| printf("true\n"); | |
| else | |
| printf("false\n"); | |
| printf("pickupNode test------\n"); | |
| printf("%d\n", pickupNode(&intList, 2)->value); | |
| freeList(&intList); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment