/*头文件*/
#ifndef _List_H
#define _List_H
/* Place in the interface file */
typedef int ElementType;
typedef struct LNode
{
ElementType Element;
LNode *Next;
}LNode, *pLinkList;
typedef pLinkList List;
typedef LNode* Node;
List MakeEmpty( List L );
bool IsEmpty( List L );
int IsLast( Node P, List L );
Node Find( ElementType X, List L );
void Delete( ElementType X, List L );
Node FindPrevious( ElementType X, List L );
void Insert( ElementType X, List L, Node P );
void DeleteList( List L );
Node Header( List L );
Node First( List L );
Node Advance( Node P );
ElementType Retrieve( Node P );
#endif /* _List_H */
/* END */
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">/*源文件*/
#include "list.h"
#include <stdlib.h>
#include <stdio.h>
List MakeEmpty( List L )
{
if( L != NULL )
DeleteList( L );
L = (List)malloc(sizeof(LNode));
if( L == NULL )
printf( "Out of memory!\n" );
L->Next = NULL;
return L;
}
/* START: fig3_8.txt */
/* Return true if L is empty */
bool IsEmpty( List L )
{
return L->Next == NULL;
}
/* END */
/* START: fig3_9.txt */
/* Return true if P is the last position in list L */
/* Parameter L is unused in this implementation */
int IsLast( Node P, List L )
{
return P->Next == NULL;
}
/* END */
/* START: fig3_10.txt */
/* Return Position of X in L; NULL if not found */
Node Find( ElementType X, List L )
{
Node P;
P = L->Next;
while( P != NULL && P->Element != X )
P = P->Next;
return P;
}
/* END */
/* START: fig3_11.txt */
/* Delete from a list */
/* Cell pointed to by P->Next is wiped out */
/* Assume that the position is legal */
/* Assume use of a header node */
void Delete( ElementType X, List L )
{
Node P, TmpCell;
P = FindPrevious( X, L );
if( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */
free( TmpCell );
}
}
/* END */
/* START: fig3_12.txt */
/* If X is not found, then Next field of returned value is NULL */
/* Assumes a header */
Node FindPrevious( ElementType X, List L )
{
Node P;
P = L;
while( P->Next != NULL && P->Next->Element != X )
P = P->Next;
return P;
}
/* END */
/* START: fig3_13.txt */
/* Insert (after legal position P) */
/* Header implementation assumed */
/* Parameter L is unused in this implementation */
void Insert( ElementType X, List L, Node P )
{
Node TmpCell;
TmpCell = (Node)malloc(sizeof(LNode));
if( TmpCell == NULL )
printf( "Out of space!\n" );
TmpCell->Element = X;
TmpCell->Next = P->Next;
P->Next = TmpCell;
}
/* END */
#if 0
/* START: fig3_14.txt */
/* Incorrect DeleteList algorithm */
void DeleteList( List L )
{
Position P;
/* 1*/ P = L->Next; /* Header assumed */
/* 2*/ L->Next = NULL;
/* 3*/ while( P != NULL )
{
/* 4*/ free( P );
/* 5*/ P = P->Next;
}
}
/* END */
#endif
/* START: fig3_15.txt */
/* Correct DeleteList algorithm */
void DeleteList( List L )
{
Node P, Tmp;
P = L->Next; /* Header assumed */
L->Next = NULL;
while( P != NULL )
{
Tmp = P->Next;
free( P );
P = Tmp;
}
}
/* END */
Node Header( List L )
{
return L;
}
Node First( List L )
{
return L->Next;
}
Node Advance( Node P )
{
return P->Next;
}
ElementType Retrieve( Node P )
{
return P->Element;
}
int main()
{
return 0;
}