head File:
LinkedList.h
/************************************ * * * Author: Chen Jiezhu * *November 2017* * * * * *************************************/ /**Prevent duplicate references**/ #ifndef _LINKEDLIST_H #define _LINKEDLIST_H #endif #define new(Class) (Class*)malloc(sizeof(Class)) #define findMethodByObject(Object, Method, ...) Object->Method(Object, __VA_ARGS__) #ifdef __cplusplus extern "C" { #endif typedef struct LinkedListItem ListItem; typedef struct LinkedListHead ListHead; struct LinkedListItem{ void* content; ListItem *prev, *next; }; struct LinkedListHead{ ListItem *headItem; ListItem * lastItem; int length; void (*removeItem)(ListHead* head, ListItem* item); void (*removeItemByPosition)(ListHead* head, int pos); void (*add)(ListHead* head, void* content); void* (*get)(ListHead* head, int pos); void (*insert)(ListHead* head, void* content, int pos); }; void removeItem(ListHead* head, ListItem* item); void removeItemByPosition(ListHead* head, int pos); void add(ListHead* head, void* content); void* get(ListHead* head, int pos); void insert(ListHead* head, void* content, int pos); #ifdef __cplusplus } #endif #include "LinkedList.c"
Implementation file:
LinkedList.c
/************************************ * * * Author: Chen Jiezhu * *November 2017* * * * * *************************************/ void removeItem(ListHead* head, ListItem* item){ ListItem *cursor = head->headItem; if(item == NULL) return; /**If you want to delete the head node**/ if(head->headItem == item){ head->headItem = head->headItem->next; head->headItem->prev = NULL; if(cursor->content != NULL) free(cursor->content); free(cursor); head->length --; return; } /**If you want to delete the trailing node**/ if(head->lastItem == item){ head->lastItem = head->lastItem->prev; head->lastItem->next = NULL; if(item->content != NULL) free(item->content); free(item); head->length --; return; } /**Other nodes**/ while(cursor != NULL){ if(cursor == item){ cursor->next->prev = cursor->prev; cursor->prev->next = cursor->next; if(item->content != NULL) free(item->content); free(item); head->length --; return; } cursor = cursor->next; } } void removeItemByPosition(ListHead* head, int pos){ ListItem *cursor = head->headItem; /**If you want to delete the head node**/ if(pos == 0){ head->headItem = head->headItem->next; head->headItem->prev = NULL; if(cursor->content != NULL) free(cursor->content); free(cursor); head->length --; return; } /**If you want to delete the trailing node**/ if(pos == head->length - 1){ head->lastItem = head->lastItem->prev; head->lastItem->next = NULL; if(cursor->content != NULL) free(cursor->content); free(cursor); head->length --; return; } /**Other nodes**/ for(; pos > 0; pos--){ if(cursor == NULL) return ; cursor = cursor->next; } cursor->next->prev = cursor->prev; cursor->prev->next = cursor->next; if(cursor->content != NULL) free(cursor->content); free(cursor); head->length --; return; } void add(ListHead* head, void* content){ if(content == NULL) return; /**If there is no item in the table, create the first item**/ if(head->headItem == NULL){ head->headItem = new(ListItem); head->lastItem = head->headItem; head->headItem->prev = NULL; head->headItem->next = NULL; head->headItem->content = content; } else{ ListItem* newItem = new(ListItem); /** The pre-order address of the newly added item is the last item in the current list**/ newItem->prev = head->lastItem; newItem->next = NULL; newItem->content = content; /**The new item becomes the last item in the list, and the last item before becomes the penultimate item**/ head->lastItem->next = newItem; head->lastItem = newItem; } head->length ++; } void* get(ListHead* head, int pos){ ListItem *cursor = head->headItem; for(; pos > 0; pos--){ if(cursor == NULL) return NULL; cursor = cursor->next; } return cursor->content; } void insert(ListHead* head, void* content, int pos){ ListItem *cursor = head->headItem; /**If inserted into the first item**/ if(pos == 0){ if(head != NULL){ ListItem* newItem = new(ListItem); newItem->content = content; newItem->prev = NULL; head->headItem->prev = newItem; newItem->next = head->headItem; head->headItem = newItem; head->length ++; return; } } /** If inserted to the last item**/ if(pos == head->length - 1){ if(head != NULL){ ListItem* newItem = new(ListItem); newItem->content = content; newItem->next = NULL; head->lastItem->next = newItem; newItem->prev = head->lastItem; head->lastItem = newItem; head->length ++; return; } } /**other**/ for(; pos > 1; pos--){ if(cursor == NULL) return ; cursor = cursor->next; } { ListItem* newItem = new(ListItem); newItem->content = content; newItem->next = cursor->next; newItem->prev = cursor; cursor->next->prev = newItem; cursor->next = newItem; head->length ++; } } void destory(ListHead* head){ if(head == NULL) return; ListItem* cursor = head->headItem; ListItem* next = NULL; int i=0; while(cursor != NULL){ next = cursor->next; if(cursor->content != NULL) free(cursor->content); cursor->content = NULL; free(cursor); cursor = NULL; cursor = next; //printf("clear:%d\n", i++); } free(head); //printf("done\n"); } ListHead* createList(){ ListHead* head = new(ListHead); /**New entry has no content**/ head->headItem = NULL; head->length = 0; head->removeItem = removeItem; head->removeItemByPosition = removeItemByPosition; head->add = add; head->get = get; head->insert = insert; return head; }
Example of use:
#include "stdio.h" #include "stdlib.h" #include "LinkedList.h" int main(){ int i; ListHead* list = createList(); /**Add item display**/ for(i = 0; i < 100; i++){ int* content = new(int); *content = i; findMethodByObject(list, add, (void*)content); } /**Subtract item display**/ findMethodByObject(list, removeItem, list->headItem->next); findMethodByObject(list, removeItem, list->headItem->next); findMethodByObject(list, removeItem, list->lastItem); printf("list length:%d\n", list->length); findMethodByObject(list, removeItemByPosition, 0); printf("list length:%d\n", list->length); { int* content = new(int); *content = 9988; findMethodByObject(list, insert, content, 90); } *(int*)findMethodByObject(list, get, 91) = 8899; for(i = 0; i < list->length ; i++){ printf("%d\t", *((int*)findMethodByObject(list, get, i))); } destory(list); printf("done\n"); return 0; }