list.h
#ifndef _LIST_H_
#define _LIST_H_
struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
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);
ElementType Retrieve(Position P);
#endif
list.c
#include<stdio.h>
#include<stdlib.h>
#include"list.h"
struct Node
{
ElementType Element;
Position Next;
};
int IsEmpty(List L)
{
return L->Next == NULL;
}
int IsLast(Position P, List L)
{
return P->Next == NULL;
}
Position Find(ElementType X, List L)
{
Position P;
P = L->Next;
while (P != NULL &&P->Element != X)
P = P->Next;
return P;
}
void Delete(ElementType X, List L)
{
Position P, TmpCell;
P = FindPrevious(X, L);
if (!IsLast(P, L))
{
TmpCell = P->Next;
P->Next = TmpCell->Next;
free(TmpCell);
}
}
Position FindPrevious(ElementType X, List L)
{
Position P;
P = L;
while (P->Next != NULL && P->Next->Element != X)
P = P->Next;
return P;
}
void Insert(ElementType X, List L, Position P)
{
Position TmpCell;
TmpCell = (Position)malloc(sizeof(struct Node));
if (TmpCell == NULL)
{
printf("FAILURE!\n");
exit(EXIT_FAILURE);
}
TmpCell->Element = X;
TmpCell->Next = P->Next;
P->Next = TmpCell;
}
void DeleteList(List L)
{
Position P, Tmp;
P = L->Next;
L->Next = NULL;
while (P != NULL)
{
Tmp = P->Next;
free(P);
P = Tmp;
}
}
ElementType Retrieve(Position P)
{
if (P != NULL)
return P->Element;
return NULL;
}