3.线性表-cursor

时间:2022-02-02 05:32:57

fatal.h

#include <stdio.h>
#include <stdlib.h> #define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr, "%s\n", Str), exit(1)

cursor.h

typedef int ElementType;
#define SpaceSize 100 #ifndef _Cursor_H
#define _Cursor_H typedef int PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position; void InitializeCursorSpace(void); List MakeEmpty(List L);
int IsEmpty(const 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);
void DeleteList(List L);
Position Header(const List L);
Position First(const List L);
Position Advance(const Position P);
ElementType Retrieve(const Position P); #endif

cursor.c

#include "cursor.h"
#include <stdlib.h>
#include "fatal.h" /* Place in the interface file */
struct Node
{
ElementType Element;
Position Next;
}; struct Node CursorSpace[SpaceSize]; static Position CursorAlloc(void)
{
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(void)
{
int i; for (i = 0; i < SpaceSize; i++)
CursorSpace[i].Next = i + 1;
CursorSpace[SpaceSize - 1].Next = 0;
} List MakeEmpty(List L)
{
if (L != 0)
DeleteList(L);
L = CursorAlloc();
if (L == 0)
FatalError("Out of memory!");
CursorSpace[L].Next = 0;
return L;
} /* Return true if L is empty */
int IsEmpty(const 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 node */
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 use of a header node */
void Delete(ElementType X, List L)
{
Position P, TmpCell; P = FindPrevious(X, L); if (!IsLast(P, L)) /* Assumption 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 space!!!"); CursorSpace[TmpCell].Element = X;
CursorSpace[TmpCell].Next = CursorSpace[P].Next;
CursorSpace[P].Next = TmpCell;
} /* Correct DeleteList algorithm */
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;
}
} Position Header(const List L)
{
return L;
} Position First(const List L)
{
return CursorSpace[L].Next;
} Position Advance(const Position P)
{
return CursorSpace[P].Next;
} ElementType Retrieve(const Position P)
{
return CursorSpace[P].Element;
}

testcurs.c

#include <stdio.h>
#include "cursor.h" void PrintList(const List L)
{
Position P = Header(L); if (IsEmpty(L))
printf("Empty list\n");
else
{
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(0);
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 = 10; i < 15; i++)
{
Insert(i, L, P);
PrintList(L);
P = Advance(P);
} for (i = 0; i < 10; i++)
if (Find(i, L) == 0)
printf("Element %d Find fails\n", i); printf("Finished deletions\n"); PrintList(L); DeleteList(L); return 0;
}

函数调用关系图(Call graph)

3.线性表-cursor