日期:2014-05-16 浏览次数:20986 次
首先是链表的头文件,所有的函数和函数的功能解释都包含在这里: list.h
#ifndef _LIST_H_ #define _LIST_H_ #endif #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);
#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 = mall