首先是链表的头文件,所有的函数和函数的功能解释都包含在这里: list.h
代码可在anycodes在线编译测试
#ifndef _LIST_H_
#define _LIST_H_
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct node;
typedef struct node* pNode;
typedef int dataType;
typedef pNode list;
typedef pNode position;
list InitList();
//create list by a array
list CreateList(dataType* a, int L);
int ListLength(list L);
//the header is still remain.
list MakeEmpty(list L);
bool IsEmpty(list L);
//check whether the node x is the last item of the list.
bool IsLastByPos(list L, position x);
//check whether the node is the last item of the list by index.
bool IsLastByIndex(list L, int index);
//find the item by data
position Find(list L, dataType x);
//find the item by index
position FindByIndex(list L, int index);
//find the previous item by data
position FindPrevious(list L, dataType x);
//delete the first item which it's data is equal with x
void Delete(list L, dataType x);
//delete the item by the index
void DeleteByIndex(list L, int index);
//insert by index
void InsertByIndex(list L, int i, dataType x);
//insert the new item after the item p
void InsertByPos(list L, position p, dataType x);
//delete all the item of the list, but remain the header
void DeleteList(list L);
position Header(list L);
position First(list L);
position Advance(position p);
//print all the data of the list
void PrintList(list L);
#endif
函数实现:list.c
#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 item
position 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;
else
tmp->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;
}