/*
* 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 */
/*
* cursor.h
*/
#ifndef _CURSOR_H
#define _CURSOR_H
#ifndef NULL
#define NULL 0
#endif
typedef int ElementType;
#define SpaceSize 100
typedef int PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
#ifdef __cplusplus
extern "C" {
#endif
void InitializeCursorSpace();
int MakeEmpty(const List L);
void DeleteList(List L);
int IsEmpty(List L);
int IsLast(const Position P, const List L);
Position Find(ElementType X, const List L);
void Delete(ElementType X, List L);
Position FindPrevious(ElementType X, const 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 /* _CURSOR_H */
/*
* cursor.cpp
*/
#include "fatal.h"
#include "cursor.h"
struct Node
{
ElementType Element;
Position Next;
};
struct Node CursorSpace[SpaceSize];
/*
* Two lines:
* Line1: Keep a freelist of cells that are not in any list.
* The list will use cell 0 as a header.
* Line2: Keep a usedlist of cells that contain concrete elements.
* The list will use L as first cells. The Next of L indicate position of next cell.
*/
/*
* If there is no space available, our routine does the correct thing
* by setting P = 0.
*/
static Position CursorAlloc()
{
Position P;
P = CursorSpace[0].Next;
CursorSpace[0].Next = CursorSpace[P].Next;
return P;
}
static void CursorFree(Position P)
{
CursorSpace[P].Next = CursorSpace[0].Next;
CursorSpace[0].Next = P;
}
void InitializeCursorSpace()
{
int i;
for (i = 0; i < SpaceSize; ++i)
{
CursorSpace[i].Next = i + 1;
}
CursorSpace[SpaceSize - 1].Next = 0;
}
int MakeEmpty(List L)
{
if (L != NULL)
{
DeleteList(L);
}
L = CursorAlloc();
if (L == 0)
{
FatalError("Out of memory!");
}
CursorSpace[L].Next = 0;
return L;
}
void DeleteList(List L)
{
Position P, Tmp;
P = CursorSpace[L].Next; /* Header assumed */
CursorSpace[L].Next = 0;
while (P != 0)
{
Tmp = CursorSpace[P].Next;
CursorFree(P);
P = Tmp;
}
}
/*
* Return true if L is empty.
*/
int IsEmpty(List L)
{
return CursorSpace[L].Next == 0;
}
/*
* Return true if P is the last position in list L.
* Parameter L is unused in this implementation.
*/
int IsLast(const Position P, const List L)
{
return CursorSpace[P].Next == 0;
}
/*
* Return Position of X in L; 0 if not found.
* Uses a header noe.
*/
Position Find(ElementType X, const List L)
{
Position P;
P = CursorSpace[L].Next;
while (P && CursorSpace[P].Element != X)
{
P = CursorSpace[P].Next;
}
return P;
}
/*
* Delete from a list.
* Assume that the position is legal.
* Assume used of a header node.
*/
void Delete(ElementType X, List L)
{
Position P, TmpCell;
P = FindPrevious(X, L);
if (!IsLast(P, L)) /* Assume of header use */
{ /* X is found; delete it */
TmpCell = CursorSpace[P].Next;
CursorSpace[P].Next = CursorSpace[TmpCell].Next;
CursorFree(TmpCell);
}
}
/*
* If X is not found, then Next field of returned value is 0.
* Assumes a header.
*/
Position FindPrevious(ElementType X, const List L)
{
Position P;
P = L;
while (CursorSpace[P].Next &&
CursorSpace[CursorSpace[P].Next].Element != X)
{
P = CursorSpace[P].Next;
}
return P;
}
/*
* Insert(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 = CursorAlloc();
if (TmpCell == 0)
{
FatalError("Out of memory!");
}
CursorSpace[TmpCell].Element = X;
CursorSpace[TmpCell].Next = CursorSpace[P].Next;
CursorSpace[P].Next = TmpCell;
}
Position Header(List L)
{
return L;
}
Position First(List L)
{
return CursorSpace[L].Next;
}
Position Advance(Position P)
{
return CursorSpace[P].Next;
}
ElementType Retrieve(Position P)
{
return CursorSpace[P].Element;
}
/*
* testcursor.cpp
*/
#include
#include "cursor.h"
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;
InitializeCursorSpace();
L = MakeEmpty(NULL);
P = Header(L);
PrintList(L);
for (i = 0; i < 10; ++i)
{
Insert(i, L, P);
PrintList(L);
P = Advance(P);
}
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;
}