学习笔记------数据结构(C语言版) 静态链表

//Function realization.cpp

#include"predefined.h"
#include"StaticLinkedList.h"
extern SLinkList space;
void InitSpace_SL()
//算法2.14:将一维数组space中各分量链成一个备用链表,(*space)[0].cur为头指针。
{
	for(int i=0;ii) return ERROR;
	*e=space[k].data;
	return OK;
}

Status PriorElem_SL(int H,ElemType cur_e,ElemType *e)//前驱
{
	int i;
	i=LocateElem_SL(H,cur_e);
	if(i<=1) return FALSE;
	GetElem_SL(H,i-1,e);
	return OK;
}

Status NextElem_SL(int H,ElemType cur_e,ElemType *e)//后继
{
	int i,j;
	i=LocateElem_SL(H,cur_e);
	j=ListLength_SL(H);
	if(!i||i>=j) return FALSE;
	GetElem_SL(H,i+1,e);
	return OK;
}

Status ListInsert_SL(int *H,int i,ElemType e)//插入
{
	if(i<1||i>ListLength_SL(*H)+1) return FALSE;
	int j,k;
	k=1;
	j=*H;
	while(kListLength_SL(*H)) return FALSE;
	int j,k;
	k=1;
	j=*H;
	while(k


//main.cpp

#include"predefined.h"
#include"StaticLinkedList.h"
SLinkList space;
void visit(int i)
{
	printf("%d ",space[i].data);
}
int main()
{
	int H,n,m,i;
	ElemType e;
	Status s;
	printf("▼Function 1\n★函数InitSpace_SL()测试...\n");
	printf("▲初始化静态链表的备用空间SPACE...\n\n");
	InitSpace_SL();
	printf("▼Function 2\n★函数Malloc_SL()、InitList_SL(int *H)测试...\n");
	printf("▲初始化静态链表H,用Malloc_SL申请空间...\n\n");
	InitList_SL(&H);
	printf("▼Function 3\n★函数ListEmpty_SL(int H)测试...\n");
	s=ListEmpty_SL(H);
	s?printf("▲表H为空!!!\n\n"):printf("▲表H非空!!!\n\n");
	printf("▼Function 4\n★函数ListInsert_SL(int *H,int i,ElemType e)测试...\n");
	printf("▲请输入准备向H输入的个数:");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
        printf("▲请输入La第%d个数:",i);
		scanf("%d",&m);
		s=ListInsert_SL(&H,i,m);
		printf("▲插入成功?:%d   (1:成功 0:失败)\n",s);
	}
	printf("\n");
	printf("▼Function 5\n★函数ListTraverse_SL(int H,void (*visit)(int i))测试...\n");
	printf("▲表H的元素为:H={");
    ListTraverse_SL(H,visit);
	printf("}\n\n");
	printf("▼Function 6\n★函数ListLength_SL(int H)测试...\n");
	printf("▲表H的长度为:%d\n\n",ListLength_SL(H));
	printf("▼Function 7\n★函数Free_SL(int k)、ListDelete_SL(int *H,int i,ElemType *e)测试...\n");
	ListDelete_SL(&H,3,&e);
	printf("▲删除表H的第3个元素\"%d\",用Free_SL释放空间...\n",e);
	printf("▲表H的元素为:H={");
    ListTraverse_SL(H,visit);
	printf("}\n\n");
	printf("▼Function 8\n★函数GetElem_SL(int H,int i,ElemType *e)测试...\n");
	GetElem_SL(H,3,&e);
	printf("▲表H的第3个元素是\"%d\"\n\n",e);
	printf("▼Function 9\n★函数LocateElem_SL(int H,ElemType e)测试...\n");
	printf("▲元素8在H中的位序:\"%d\"\n\n",LocateElem_SL(H,8));
	printf("▼Function 10\n★函数PriorElem_SL(int H,ElemType cur_e,ElemType *e)测试...\n");
	PriorElem_SL(H,5,&e);
	printf("▲表H元素5的前驱为:%d\n\n",e);
	printf("▼Function 11\n★函数NextElem_SL(int H,ElemType cur_e,ElemType *e)测试...\n");
	NextElem_SL(H,5,&e);
	printf("▲表H元素5的后继为:%d\n\n",e);
	printf("▼Function 12\n★函数ClearList_SL(int H)测试...\n");
	printf("▲置空前:");
	ListEmpty_SL(H)?printf("表H为空!!!\n"):printf("表H非空!!!\n");
	ClearList_SL(H);
	printf("▲置空后:");
	ListEmpty_SL(H)?printf("表H为空!!!\n\n"):printf("表H非空!!!\n\n");
	printf("▼Function 13\n★函数DestroyList_SL(int *H)测试...\n");
	printf("▲销毁前:");
	H?printf("表H存  在!!!\n"):printf("表H不存在!!!\n");
	DestroyList_SL(&H);
	printf("▲销毁后:");
	H?printf("表H存  在!!!\n\n"):printf("表H不存在!!!\n\n");
	printf("▼Function 13\n★函数difference(int *S)测试...\n");
	difference(&H);
	printf("▲表H的元素为:H={");
    ListTraverse_SL(H,visit);
	printf("}\n\n");
}


//StaticLinkedList.h

#define MAXSIZE 1000
typedef struct
{
	ElemType data;
	int cur;
}component,SLinkList[MAXSIZE];
void InitSpace_SL();
int Malloc_SL();
void Free_SL(int k);
void difference(int *S);
int LocateElem_SL(int H,ElemType e);
Status InitList_SL(int *H);
Status ClearList_SL(int H);
Status DestroyList_SL(int *H);
Status ListEmpty_SL(int H);
int ListLength_SL(int H);
Status GetElem_SL(int H,int i,ElemType *e);
Status PriorElem_SL(int H,ElemType cur_e,ElemType *e);
Status NextElem_SL(int H,ElemType cur_e,ElemType *e);
Status ListInsert_SL(int *H,int i,ElemType e);
Status ListDelete_SL(int *H,int i,ElemType *e);
Status ListTraverse_SL(int H,void (*visit)(int i));
void visit(int i);

//predefined.h

#include "stdio.h"
#include "stdlib.h"
#define   TRUE           1
#define   FALSE          0
#define   OK             1
#define   ERROR          0
#define   INFEASIBLE    -1
#define   OVERFLOW      -2
typedef   int Status;
typedef   int ElemType;



注意:外部变量不要放在头文件中,否则容易出现重复定义外部变量,出现LNK2005错误。

你可能感兴趣的:(学习笔记---数据结构)