数据结构——线性表学习总结3(静态链表实现)

前言:本篇文章绝大部分来自《数据结构与算法分析C语言描述》中关于“链表的游标实现”部分,要理解游标这个概念就是起静态链表指针的作用的,还有静态链表里为什么要准备一个“备用”的链表,静态链表如何实现"malloc()"和"free()"的功能的,下面开始:

一、链表游标实现的声明

/* 链表的游标实现,静态链表 */
 //链表游标实现的声明

#ifndef _Cursor_H
#define _Cursor_H 
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 //链表的最大长度 

typedef  int ElemType; //定义一个元素型 
typedef int List;  //确定数组的头元素,即可确定数组  

typedef int PtrToNode;  //定义一个指针型 
typedef PtrToNode Position;

/*  Place in the implementation file */
struct Node
{
	ElemType Elem;  //数据域 
	Position Next;  //指针域 
};
struct Node CursorSpace[MAXSIZE];  //结构数组 

void InitCursorSpace(void);  //建立新的静态链表
List MakeEmpty( List L );  //L取值在1到MAXSIZE之间,不能取到0,l[0]是备用链表的头结点 
int IsEmpty( const List L );
int IsLast( const Position P, const List L );
Position Find( ElemType X, const List L );
void Delete( ElemType X, List L);
Position FindPrevious( ElemType X,const List L);
void Insert( ElemType X ,List L ,Position P );
void DeleteList( List L);
Position Header( const List L);
Position First( const List L);
Position Advance( const Position P);
ElemType Retrieve( const Position P);

#endif    /* _Cursor_h */

二、Cursor.h函数实现

/* 相关函数实现 */

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

/* 实现InitCursorSpace() */
/* 将一维数组CursorSpace中各分量链成一个备用链表
 ,CursorSpace[0].Next作为头指针 */
/* “0 ”表示空指针 */ 
void InitCursorSpace(void) 
{
	int i;
	
	for( i = 0; i < MAXSIZE -1; ++i)
	CursorSpace[i].Next = i + 1;  //初始化备用链表
	CursorSpace[MAXSIZE-1].Next = 0; //链表的头结点 
	 
}

/* 实现malloc()函数的功能 */ 
static Position
CursorAlloc( void ) 
{
	Position P;
	
	
	P = CursorSpace[0].Next;
	CursorSpace[0].Next = CursorSpace[P].Next;
	
	return P;
 }
  
 /* 实现free()功能  */ 
static void
CursorFree( Position P)
{
	CursorSpace[P].Next = CursorSpace[0].Next;
	CursorSpace[0].Next = P;
}

/* 测试一个链表是否为空——游标实现 */
/* Return true if L is empty */
int
IsEmpty( List L)
{
	return CursorSpace[L].Next == 0;
 } 
 
 /* 测试P是否为链表末尾的函数——游标实现 */
 /* Return true if P is the last position in list L */
 int
 IsLast( Position P,List L)
 {
 	return CursorSpace[P].Next == 0; 
  } 
  
/* Find——游标实现 */
/* Return Position of X in L;0 if not find */
/* Uses a header node */

Position 
Find( ElemType X, List L)
{
	Position P;
	
	P = CursorSpace[L].Next;
	while( P && CursorSpace[P].Elem != X)
		P = CursorSpace[P].Next;
		
	return P;
  }  
  
/* 对链表进行删除操作Delete——游标实现 */
/* Delete first occurence of X from a list */
/* Assume use a header node */
void 
Delete(ElemType X,List L)
{
	Position P,TmpCell;
	
	P= Find(X,L);
	
	if(!IsLast( P,L))
	{
		TmpCell = CursorSpace[P].Next;
		CursorSpace[P].Next = CursorSpace[TmpCell].Next;
		CursorFree(TmpCell);
	}
	
	
 } 
  
/* 对链表插入操作Insert——游标实现 */
/* Insert after legal position P */
/* Header implementation assumed */

void
Insert(ElemType X, List L, Position P)
{
	Position TmpCell ;
	
	TmpCell = CursorAlloc();
	if( TmpCell == 0)
		printf("Out of space!!!");
	
	CursorSpace[ TmpCell].Elem = X;
	CursorSpace[TmpCell].Next = CursorSpace[P].Next;
	CursorSpace[P].Next = TmpCell;
	
	
  }  

三、测试用主函数

* 测试用主函数 */ 
#include <stdio.h>
#include <stdlib.h>
#include"Cursor.h"

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

 ElemType Display(  List L);
int main(void) {
	
	InitCursorSpace();
	printf("从第一个位置插入1到10\n") ;
	int i;
    int L=1; 
	for(i=1;i<=10;i++)
	Insert( i,  1,  i); 
	/*Insert(1,1,1) ;
	Insert(2,1,2) ;
	Insert(3,1,3) ;
	Insert(4,1,4) ;
	Insert(5,1,5) ;*/
	
	CursorSpace[11].Next = 0;
	CursorSpace[12].Next = 0;
	
	 /* printf("%5d\n",CursorSpace[1].Elem); */
	Display( L) ; 
	
	printf("删除第8个位置上的数\n"); 
	Delete( 8, 1);
	Display( L) ; 
	
	printf("在第8个位置插入666\n") ;
	Insert(666,  1, 8);
	Display( L) ; 

	
	
	
	return 0;
}

 ElemType Display(  List L){          //输出表 
	if(L<0 || L > MAXSIZE-1)
	return ERROR;
	
	
	Position P;
	
	P = CursorSpace[L].Next;
	int i;
	for(i=1;i<10;i++)
		printf("**%d",CursorSpace[i].Elem);
/*	while( P =!0 ) 
		{
			P= CursorSpace[P].Next;
			printf("**%d",CursorSpace[P].Elem);
		} */
		
		printf("\n");
		
	return OK;
}

主函数功能没有实现,仅供参考;
并且这里的List L和CursorSpace L是有很大区别的,一个是在原来的表里取一部分元素链成一个表,后一个是直接创建了一个新表。

发布了9 篇原创文章 · 获赞 9 · 访问量 250

猜你喜欢

转载自blog.csdn.net/Star_jiang/article/details/105303002
今日推荐