/*
* fatal.h
* Header file for print error message.
*/
#ifndef _FATAL_H
#define _FATAL_H
#include
#include
#define Error(str) FatalError(str)
#define FatalError(str) fprintf(stderr, "%s\n", str), exit(-1)
#endif /* _FATAL_H */
/*
* list.h
* Header file for linked list
*/
#ifndef _LIST_H
#define _LIST_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef NULL
#define NULL (0)
#endif
typedef int ElementType;
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List MakeEmpty(List L);
void DeleteList(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);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
ElementType Retrieve(Position P);
#ifdef __cplusplus
}
#endif
#endif /* _LIST_H */
/*
* list.cpp
* Implementation file for linked list
*/
#include
#include "fatal.h"
#include "List.h"
struct Node
{
ElementType Element;
Position Next;
};
/*
* Function to make linked list that L points empty.
* If L points to a concrete linked list, first delete it.
*/
List MakeEmpty(List L)
{
if (L != NULL)
{
DeleteList(L);
}
L = (List)malloc(sizeof(struct Node));
if (L == NULL)
{
FatalError("Out of memory!");
}
L->Next = NULL;
return L;
}
/*
* Function to delete linked list that L points to.
*/
void DeleteList(List L)
{
Position P, Tmp;
P = L->Next; /* Header assumed */
L->Next = NULL;
while (P != NULL)
{
Tmp = P->Next;
free(P);
P = Tmp;
}
}
/*
* Function to test whether a linked list is empty.
* Return true if L is empty.
*/
int IsEmpty(List L)
{
return L->Next == NULL;
}
/*
* Function to test whether current position is the last in a linked list.
* 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;
}
/*
* Function to find element.
* Return Position of X in L; NULL if not found.
*/
Position Find(ElementType X, List L)
{
Position P;
P = L->Next;
while (P != NULL && P->Element != X)
{
P = P->Next;
}
return P;
}
/*
* Function to delete frist occurrence of X from a linked list.
* Assume use of a header node.
*/
void Delete(ElementType X, List L)
{
Position P, TmpCell;
P = FindPrevious(X, L);
/* If L points to a empty list, FindPrevious will return L,
* and then IsLast will return true. */
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);
}
}
/*
* Function to find position of previous elememt.
* If X is not found, then Next field of returned position is NULL.
* Assume a header
*/
Position FindPrevious(ElementType X, List L)
{
Position P;
P = L;
while (P->Next != NULL && P->Next->Element != X)
{
P = P->Next;
}
return P;
}
/*
* Function to Insert element into L after legal position P.
* Header implementation assumed.
* Parameter L is unused in this implementation.
*/
void Insert(ElementType X, List L, Position P)
{
Position TmpCell;
TmpCell = (Position)malloc(sizeof(struct Node));
if (TmpCell == NULL)
{
FatalError("Out of memory!");
}
TmpCell->Element = X;
TmpCell->Next = P->Next;
P->Next = TmpCell;
}
/*
* Function to get header
*/
Position Header(List L)
{
return L;
}
/*
* Function to get first position
*/
Position First(List L)
{
return L->Next;
}
/*
* Function to advance postion to next.
*/
Position Advance(Position P)
{
return P->Next;
}
/*
* Function to get element of current position.
*/
ElementType Retrieve(Position P)
{
return P->Element;
}
/*
* testlist.cpp
* Implementation file for test.
*/
#include
#include "list.h"
/*
* Function to print every element in linked list.
*/
void PrintList(const List L)
{
Position P;
if (IsEmpty(L))
{
printf("Empty list.\n");;
}
else
{
P = Header(L);
do
{
P = Advance(P);
printf("%d ", Retrieve(P));
} while (!IsLast(P, L));
printf("\n");
}
}
int main()
{
List L;
Position P;
int i;
L = MakeEmpty(NULL);
P = Header(L);
PrintList(L);
for (i = 0; i < 10; ++i)
{
Insert(i, L, P);
P = Advance(P);
}
PrintList(L);
for (i = 0; i < 10; i += 2)
{
Delete(i, L);
}
for (i = 0; i < 10; ++i)
{
if (((i & 0x01) == 0) && (Find(i, L) != NULL))
{
printf("Find fails!\n");
}
}
PrintList(L);
DeleteList(L);
return 0;
}