线性表之静态链表

#include<stdio.h>
#include<stdlib.h>

//typedef 80 MAXSIZE;
#define MAXSIZE 20

typedef struct Node{
	int data;
	int cursor;
}Node,StaticList[MAXSIZE];

void InitialSpace(StaticList Space){
	int i;
	for(i = 0;i < MAXSIZE - 1; i++){
		Space[i].cursor = i + 1;
	}
	Space[MAXSIZE - 1].cursor = 0;
}
int Malloc_StaticList(StaticList Space){
	int index;
	index = Space[0].cursor;
	if(index){
		Space[0].cursor = Space[index].cursor;
		return index;
	}
	else{
		printf("Space Full, Failed!\n");
		exit(1);
	}
}
void Free_StaticList(StaticList Space,int index){
	Space[index].data = 0;
	Space[index].cursor = Space[0].cursor;
	Space[0].cursor = index;
}
int InitialStaticList(StaticList Space){
	int i;
	i = Malloc_StaticList(Space);
	Space[i].cursor = 0;
	return i;
}
void ClearStaticList(StaticList Space,int n_List){
	int temp_n_List = n_List;
	n_List = Space[n_List].cursor;
	int t_cursor;
	while(n_List != 0){
		t_cursor = Space[n_List].cursor;
		Space[n_List].data = 0;
		Free_StaticList(Space,n_List);
		n_List = t_cursor;
	}
	Space[temp_n_List].cursor = 0;
	printf("Clear Static List Executed!\n");
}
void StaticListEmpty(StaticList space,int n_List){
	if(space[n_List].cursor)
		printf("No Empty\n");
	else
		printf("Empty!\n");
}
int StaticList_Length(StaticList Space,int n_List){
	int count = 0;
	while(Space[n_List].cursor){
		count++;
		n_List = Space[n_List].cursor;
	}
	return count;
}
void InsertStaticList(StaticList Space,int n_List,int position,int e){
/*
 *
 *
 * */
	int length = StaticList_Length(Space,n_List);
	int copy_n_List = n_List;
	if(position < 1 || position > length + 1){
		printf("Insert funtion ERROR: position\n");
		exit(1);
	}
	int new_i = Malloc_StaticList(Space);
	if(new_i){
		Space[new_i].data = e;
		int count = 0,i;
		for(i = 1; i < position; i++){ //
			n_List = Space[n_List].cursor;
		}
		Space[new_i].cursor = Space[n_List].cursor;
		Space[n_List].cursor = new_i;
		printf("INFORMATION: Insert %d to List%d with position=%d executed!\n",e,copy_n_List,position);
	}
}
void DeleteStaticList(StaticList space,int n_List,int position){
	int length = StaticList_Length(space,n_List);
	int t_n_List = n_List;
	if(position < 1 || position > length){
		printf("function DeleteSataicList Error:position!\n");
	}
	else
	{
		int count = 0,result;
		while(count < position - 1){
			n_List = space[n_List].cursor;
			count++;
		}
		result = space[space[n_List].cursor].data;
		printf("deleteSataisList executed: element %d in position %d of List%d was deleted\n",result,position,t_n_List);
		int temp = space[n_List].cursor;
		space[n_List].cursor = space[space[n_List].cursor].cursor;
		Free_StaticList(space,temp);
	}
}
int GetStaticListElement(StaticList space,int n_List,int position){
	int count = 0;
	int temp_n_List = n_List;
	while(space[n_List].cursor && count < position){
		n_List = space[n_List].cursor;
		count++;
	}
	if(space[n_List].cursor == 0){
		printf("INFORMATION:get staticList element Error: position!\n");
	}
	else{
		int e;
		e = space[n_List].data;
		printf("GetStaticListElement with position=%d from List%d executed! the result is %d\n",position,temp_n_List,e);
	}
}
int LocateStaticList(StaticList space,int n_List,int e){
	n_List = space[n_List].cursor;
	while(n_List){
		if(space[n_List].data == e){
			return n_List;
		}
		n_List = space[n_List].cursor;
	}
	return 0;
}
void NextElement_SL(StaticList space,int n_List,int currentElement){
	if(LocateStaticList(space,n_List,currentElement) == 0)
		printf("function NextElement Error:currentElement%d dose not exist\n",currentElement);
	else if(space[LocateStaticList(space,n_List,currentElement)].cursor == 0)
			printf("function NextElement Error:the currentElement%d is last one of list%d\n",currentElement,n_List);
	else
		printf("the nextElement of %d is %d\n",currentElement,space[space[LocateStaticList(space,n_List,currentElement)].cursor].data);
}
void PriorElement_SL(StaticList space,int n_List,int currentElement){
	if(!LocateStaticList(space,n_List,currentElement))
		printf("function PriorElement Error:currentElement%d does not exist\n",currentElement);
	else if(space[n_List].cursor == LocateStaticList(space,n_List,currentElement))
		printf("function PriorElement Error:the currentElement%d is the first of the List%d\n",currentElement,n_List);
	else{
		n_List = space[n_List].cursor;
		int temp;
		while(space[n_List].data != currentElement){
			temp = n_List;
			n_List = space[n_List].cursor;
		}
		printf("the PriorElement of %d is %d\n",currentElement,space[temp].data);
	}
}
void DisplaySpace(StaticList Space){
	int i;
	for(i = 0;i < MAXSIZE; i++)
		printf("%4d",Space[i].cursor);
	printf("\n");
	for(i = 0;i < MAXSIZE; i++)
		printf("%4d",Space[i].data);
	printf("\n");
	for(i = 0;i < MAXSIZE; i++)
		printf("%4d",i);
	printf("\n");
}
/*****************************************************/
void CreateStaticList(StaticList space,int n_List,int length){
	if(length > FreeSpaceLength(space)){
		printf("No remaining Space!\n");
	}
	else
	{
		int i,new_index;
		printf("Create Static List %d:\n",n_List);
		for(i = 1;i <= length;i++){
			printf("--------enter %d element:  ",i);
			new_index = Malloc_StaticList(space);
			scanf("%d",&space[new_index].data);
			space[n_List].cursor = new_index;
			n_List = new_index;
		}
		space[new_index].cursor = 0;
	}
}
int FreeSpaceLength(StaticList space){
	int length = 0;
	int index = space[0].cursor;
	while(index){
		length++;
		index = space[index].cursor;
	}
	return length;
}
void setdatato0inordertodisplay(StaticList space){
	int i;
	for(i = 0;i < MAXSIZE; i++)
		space[i].data  =  0;
}
void main(){
	StaticList space;
	setdatato0inordertodisplay(space);
	InitialSpace(space);

	int list1 = InitialStaticList(space);
	int list2 = InitialStaticList(space);
	int list3 = InitialStaticList(space);

	printf("   SHOW SPACE\n");
	DisplaySpace(space);

	int length3 = StaticList_Length(space,list3);
	printf("\nthe length of list3 is %d\n",length3);
	CreateStaticList(space,list3,5);
	CreateStaticList(space,list2,25);
	DisplaySpace(space);

	length3 = StaticList_Length(space,list3);
	printf("the length of list3 is %d\n",length3);
	int length2 = StaticList_Length(space,list2);
	printf("the length of list2 is %d\n",length2);

	CreateStaticList(space,list2,3);
	DisplaySpace(space);
	length2 = StaticList_Length(space,list2);
	printf("the length of list2 is %d\n",length2);
	InsertStaticList(space,list3,4,666);

	DisplaySpace(space);
	printf("the length of list3 is %d\n",StaticList_Length(space,list3));
	
	GetStaticListElement(space,list3,3);
	GetStaticListElement(space,list3,9);
	GetStaticListElement(space,list1,1);
	GetStaticListElement(space,list2,1);
	
	if(LocateStaticList(space,list3,4)){
		printf("the position of element 4 in List3 is %d\n",LocateStaticList(space,list3,4));
	}else
		printf("locateStaticlist in list3 with 4 is Failed!\n");
	
	if(LocateStaticList(space,list3,2)){
		printf("the position of element 2 in List3 is %d\n",LocateStaticList(space,list3,4));
	}else
		printf("locateStaticlist in list3 with 2 is Failed!\n");

	printf("\n\ntest NextElemet\n");
	NextElement_SL(space,list3,9);
	NextElement_SL(space,list3,7);
	NextElement_SL(space,list3,3);

	printf("\n\ntest PriorElement\n");
	PriorElement_SL(space,list3,3);
	PriorElement_SL(space,list3,8);
	PriorElement_SL(space,list3,1);

	printf("\n");
	DeleteStaticList(space,list3,5);
	printf("\nthe length of list3 is %d\n",StaticList_Length(space,list3));
	DeleteStaticList(space,list2,35);
	printf("\nthe length of list2 is %d\n",StaticList_Length(space,list2));

	InsertStaticList(space,list3,2,888);
	DisplaySpace(space);

	ClearStaticList(space,list3);
	DisplaySpace(space);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值