带头节点单链表的所有操作(目前我所想到的),linux纯C实现
首先是链表的头文件,所有的函数和函数的功能解释都包含在这里: list.h
#include "list.h" typedef struct node { dataType data;pNode next;}node;list InitList(){list head = malloc(sizeof(node));head->next = NULL;return head;}list CreateList(dataType* a, int L){list l = InitList();int i = 0;if(L < 1) {//printf("The length is too short!\n");return l;}for(;i<L;i++)InsertByIndex(l, i, *(a+i));return l;}int ListLength(list L){if(!(L->next)) return 1;int l = 1;position p = L->next;while(p->next){l++;p = p->next;}return l;}list MakeEmpty(list L){if(L != NULL)DeleteList(L);L = malloc(sizeof(node));if(L == NULL){printf("Out of Space!\n");exit(1);}L->next = NULL;return L;}bool IsEmpty(list L){ return L->next == NULL; }bool IsLastByPos(list L, position p){return p->next == NULL;}bool IsLastByIndex(list L, int index){position p = FindByIndex(L, index);return p->next == NULL;}//if there is no specified item, then return null pointer.//if there is serval specified item, then return the first item which the same data of x.position Find(list L, dataType x){position p;p = L->next;while(p != NULL && p->data != x)p = p->next;return p;}position FindByIndex(list L, int index){position p = L;int i = index;if(IsEmpty(L) || index <= 0){printf("The input is error!\n");exit(1);}for(;i>0;i--){if(p->next != NULL)p = p->next;else{printf("The index is bigger than the length of the list!\n");exit(1);}}return p;}//if there is no specified item, then return the last item of the list.//or if there are several specified items, then return the previous item of the first itemposition FindPrevious(list L, dataType x){position p;p = L;while(p->next != NULL && p->next->data != x)p = p->next;return p;}void Delete(list L, dataType x){position p = FindPrevious(L, x);if(!IsLastByPos(L, p)){p->next = p->next->next;free(p->next);}}void DeleteByIndex(list L, int index){position p = FindByIndex(L, index-1);if(p->next == NULL){printf("The index is too big!\n");exit(1);}p->next = p->next->next;free(p->next);}void InsertByIndex(list L, int i, dataType x){position p = L;position tmp;bool isEmpty = IsEmpty(L);tmp = malloc(sizeof(node));if(tmp == NULL){printf("Out of Space!\n");exit(1);}tmp->data = x;if(!isEmpty)p = FindByIndex(L, i);if(isEmpty || (p->next == NULL))tmp->next = NULL;elsetmp->next = p->next;p->next = tmp;}void InsertByPos(list L, position p, dataType x){position tmp;tmp = malloc(sizeof(node));if(tmp == NULL){printf("Out of Space!\n");exit(1);}tmp->data = x;tmp->next = p->next;p->next = tmp;}//delete the list, but the header is still remain.void DeleteList(list L){position p, tmp;p = L->next;L->next = NULL;while(p != NULL){tmp = p->next;free(p);p = tmp;}}position Header(list L){return L;}position First(list L){return L->next;}position Advance(position p){return p->next;}void PrintList(list L) {if(IsEmpty(L)){printf("The list is empty!\n");return;}position p = L;while(p->next){printf("%d ", p->next->data);p = p->next;}}int main(){int a[10] = {12,4354,657,876,2433,23,18,943,54,8};int L = sizeof(a) / sizeof(int);int i = 1;list l = CreateList(a, L);Delete(l, 80);PrintList(l);printf("\n");return 0;}