链表的游标实现

数据结构与算法分析——c语言描述 第三章  链表的游标实现


和普通的链表没什么区别,就是用数组来实现内存空间,并且存在两个链表,一个是未使用元素组成的链表,首元素是下标为0。另一个个就是使用中的数表。使用之前要main函数手动初始化。


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 creatList();
List MakeEmpty(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);
void DeleteList(List L);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
ElementType Retrieve(Position P);
#endif


cursor.c

#include"cursor.h"
#include<stdlib.h>
#include"fatal.h"

struct  Node
{
	ElementType Element;
	Position Next;
};

struct Node CursorSpace[SpaceSize];

static List CursorAlloc() {
	Position p;
	p = CursorSpace[0].Next;
	CursorSpace[0].Next = CursorSpace[p].Next;
	return p;
}

static void Recover(Position p) {
	CursorSpace[p].Next = CursorSpace[0].Next;
	CursorSpace[0].Next = p;
}

void InitializeCursorSpace(void) {
	for (int i = 0; i < SpaceSize - 1; i++)
		CursorSpace[i].Next = i+1;
	CursorSpace[SpaceSize - 1].Next = 0;
}

List creatList() {
	List L;
	L = CursorAlloc();
	if (L == 0)
		FatalError("Out of memory");
	CursorSpace[L].Next = 0;
	return L;
}


List MakeEmpty(List L) {
	if (L != 0){
		DeleteList(L);
		CursorSpace[L].Next = 0;
		return L;
	}
	else {
		L = CursorAlloc();
		if (L == 0)
			FatalError("Out of memory");
		CursorSpace[L].Next = 0;
		return L;
	}
}

int IsEmpty(List L) {
	return CursorSpace[L].Next == 0;
}

int IsLast(Position P, List L) {
	return CursorSpace[P].Next == 0;
}

Position Find(ElementType X, List L) {
	Position P;
	P = CursorSpace[L].Next;
	while (P != 0 &&CursorSpace[P].Element != X)
	{
		P = CursorSpace[P].Next;
	}
	return P;
}

void Delete(ElementType X, List L) {
	Position P;
	P = FindPrevious(X, L);
	if (!IsLast(P, L)) {
		Position TmpCell = CursorSpace[P].Next;
		CursorSpace[P].Next = CursorSpace[TmpCell].Next;
		Recover(TmpCell);
	}
}

Position FindPrevious(ElementType X, List L) {
	Position P;
	P = L;
	while (CursorSpace[P].Next != 0 && CursorSpace[CursorSpace[P].Next].Element != X)
		P = CursorSpace[P].Next;
	return P;
}

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

void DeleteList(List L) {
	Position p;
	p = CursorSpace[L].Next;
	CursorSpace[L].Next = 0;
	while (p != 0) {
		Position tmp;
		tmp = CursorSpace[p].Next;
		Recover(p);
		p = tmp;
	}
}

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


main.c

#include<stdio.h>
#include"cursor.h"

int main() {
	InitializeCursorSpace();
	List l = creatList();
	Insert(23333, l, l);
	printf("%d", Retrieve(First(l)));
}



你可能感兴趣的:(链表的游标实现)