C源码@数据结构与算法->表(Cursor )

/*
 * 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;
}
      
      



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值