list.h
typedef int ElementType;
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List CreateList();
void DisposeList(List L);
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position P, List L);
Position Find(ElementType X, List L);
void Delete(ElementType X, List L);
Position FindPrevious(ElementType X, List L);
void Insert(ElementType X, List L, Position P);
void DeleteList(List L);
void SetAdvance(Position P, Position NextP);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
ElementType Retrieve(Position P);
#endif // LIST_H_INCLUDED
fatal.h
#ifndef FATAL_H_INCLUDED
#define FATAL_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr, "%sn", Str), exit(1)
#endif // FATAL_H_INCLUDED
list.c
实现假定带有头节点。
#include "list.h"
#include "fatal.h"
struct Node
{
ElementType Element;
Position Next;
};
List CreateList()
{
List L;
L = malloc(sizeof(struct Node));
if(L == NULL)
FatalError("Out of space!!!");
L->Next = NULL;
return L;
}
void DisposeList(List L)
{
MakeEmpty(L);
free(L);
}
List MakeEmpty(List L)
{
if(L == NULL)
Error("Must use CreateList first");
if(L->Next != NULL)
DeleteList(L);
return L;
}
/* Return true if L is empty */
int IsEmpty(List L)
{
return L->Next == NULL;
}
/* Return true if P is the last position in list L */
/* Parameter L is unused in this implementation */
int IsLast(Position P, List L)
{
return P->Next == NULL;
}
/* Return Position of X in L; NULL if not found */
责任编辑:小草