诸如BASIC和FORTRAN等许多语言都不支持指针。如果需要链表而又不能使用指针,这时我们可以使用游标(cursor)实现法来实现链表。
在链表的实现中有两个重要的特点:
1,数据存储在一组结构体中。每一个结构体包含有数据以及指向下一个结构体的指针。
2,一个新的结构体可以通过调用malloc而从系统全局内存(global memory)得到,并可以通过free而被释放。
个人理解为用一个结点数组来模拟实现内存的分配和释放,在结构体数组中,保留一个表freelist作为备用单链表,用来alloc或free游标可用空间,该表用0作为表头。刚开始时,freelist就是整个结构体数组。
从不同的表头(Index)出发形成了不同的单链表。如果定义一个大小为10的游标空间,则其初始化状态应如下:
slot | Element | Next |
0 | 1 | |
1 | 2 | |
2 | 3 | |
3 | 4 | |
4 | 5 | |
5 | 6 | |
6 | 7 | |
7 | 8 | |
8 | 9 | |
9 | 0 |
Code is here~
.h 中的声明:
#ifndef CUSORLIST_H_INCLUDED #define CUSORLIST_H_INCLUDED typedef int PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; void InitializeCursorSpace(void); #define ElementType int //set the type of element as Integer // Functions List MakeEmpty(List L); List InitialList(); 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 Last(const List L); Position Advance(const Position P);//not implemented yet ElementType Retrive(const Position P);//not implemented yet #endif // CUSORLIST_H_INCLUDED
.c文件:
#include "CusorList.h" #include <stdio.h> #include <stdlib.h> struct Node{ ElementType Element; Position Next; }; #define SpaceSize 40 struct Node CursorSpace[SpaceSize]; // CursorSpace here is an array of Nodes void InitializeCursorSpace() { int i; for (i = 0; i < SpaceSize - 1; i++) { CursorSpace[i].Next = i + 1; } CursorSpace[SpaceSize - 1].Next = 0; // here "0" means "NULL" } static Position CursorAlloc(void) { // remember that Position(P) here is just a index of array, that's to say, an integer. 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; } int IsEmpty(const List L) { // Return true if L is empty return CursorSpace[L].Next == 0; } int IsLast(const Position P, const List L) { // return true if P is the last Position return CursorSpace[P].Next == 0; } Position Find(ElementType X, const List L) { // return Position of X in L; 0 if not found Position P = CursorSpace[L].Next; while (P && CursorSpace[P].Element != X) P = CursorSpace[P].Next; return P; } void Delete(ElementType X, List L) { // Delete the first occurrence of X from L Position P, tmp; P = FindPrevious(X, L); if (!IsLast(P,L)) { tmp = CursorSpace[P].Next; CursorSpace[P].Next = CursorSpace[tmp].Next; CursorFree(tmp); } } Position FindPrevious(ElementType X, const List L) { Position P = L; Position tmp = CursorSpace[P].Next; while (CursorSpace[tmp].Element != X && tmp) { tmp = CursorSpace[tmp].Next; P = CursorSpace[P].Next; } return P; } /*Insert(after legal position)*/ /*Header implementation assumed*/ /*Parameter L is unused in this implementation*/ void Insert(ElementType X, List L, Position P) { Position tmp; tmp = CursorAlloc(); if (0 == tmp) { printf("Out of space!"); } else { CursorSpace[tmp].Element = X; CursorSpace[tmp].Next = CursorSpace[P].Next; CursorSpace[P].Next = tmp; } } void DeleteList(List L) { Position P = CursorSpace[L].Next; Position tmp = P; while (tmp != 0) { P = CursorSpace[P].Next; CursorFree(tmp); if (P == 0) { break; } tmp = P; } CursorSpace[L].Next = 0; }
参考资料:http://blog.csdn.net/alps1992/article/details/38174289