记录自己学习数据结构的历程(1)静态链表

最近在看《大话数据结构》,但里面还是有的东西没有弄懂,就是静态链表这里,感觉有一些难理解,但也尝试着自己敲了敲代码。不知道为啥敲了这么多行…但好在测试的数据都通过了??

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

#define maxsize 9

typedef int elemType;

typedef struct{
	elemType data;
	int next;
}SLL;

/*

当SLL[0].next==0			表满
当SLL[maxsize-1].next==0	表空 
当next域为-1				到达表尾 

*/

void initSLL(SLL *L);
int getLength(SLL *L);
int mallocSLL(SLL *L);
void freeSLL(SLL *L,int freeLoca);
int getLogicPos(SLL *L,int pos);
bool insertSLL(SLL *L,int pos,elemType num);
bool deleteSLL(SLL *L,int pos);
void testFunc(SLL *L);

void initSLL(SLL *L){
	int i;
	for(i=0;i<maxsize-1;i++){
		L[i].next=i+1;
	}
	L[maxsize-1].next=0;
	cout<<"初始化成功"<<endl;
	cout<<"......"<<endl;
}

int getLength(SLL *L){
	int length=0;
	int pos=L[maxsize-1].next;
	if(L[maxsize-1].next==0){
		length=0;
	}else{
		while(pos!=-1){
			length++;
			pos=L[pos].next;
		}
	}
	
	return length;
}

int mallocSLL(SLL *L){
	if(L[0].next==0){
		cout<<"表满"<<endl;
		cout<<"......"<<endl;
		return -1;
	}else{
		int newLoca;
		newLoca=L[0].next;
		L[0].next=L[newLoca].next;
		cout<<"已分配节点号:"<<newLoca<<endl;
		cout<<"......"<<endl;
		return newLoca;
	}
}

void freeSLL(SLL *L,int freeLoca){
	int logicPos;
	logicPos=getLogicPos(L,freeLoca);
	L[logicPos].next=L[0].next;
	L[0].next=logicPos;
	cout<<"已释放空间"<<endl;
	cout<<"......"<<endl;
}

int getLogicPos(SLL *L,int pos){
	int logicPos;
	int length;
	logicPos=L[maxsize-1].next;
	length=getLength(L);
	if(pos<=0||pos>=maxsize-1){
		cout<<"位置不合法"<<endl;
		cout<<"......"<<endl;
		return -2;
	}else if(pos>length){
		cout<<"不存在该位置"<<endl;
		cout<<"......"<<endl;
		return -2;
	}else if(logicPos==0){
		cout<<"表空"<<endl;
		cout<<"......"<<endl;
		return -2;
	}else if(pos>=1&&pos<=length){
		for(int timer=1;timer<pos;timer++){
			logicPos=L[logicPos].next;
		}
	}
	return logicPos;
}


bool insertSLL(SLL *L,int pos,elemType num){
	int length;
	length=getLength(L);
	if(pos<=0||pos>=maxsize-1){
		cout<<"插入位置非法"<<endl;
		cout<<"......"<<endl;
		return false;
	}else if(pos!=1&&pos>length+1){
		cout<<"插入位置非法"<<endl; 
		cout<<"......"<<endl;
	}else{
		int newLoca=mallocSLL(L);
		L[newLoca].data=num;
		if(length==0){
			L[newLoca].next=-1;
			L[maxsize-1].next=newLoca;
		}else if(L[getLogicPos(L,pos-1)].next==-1){
			L[newLoca].next=-1;
			L[getLogicPos(L,pos-1)].next=newLoca;
		}else{
			L[newLoca].next=L[getLogicPos(L,pos-1)].next;
			L[getLogicPos(L,pos-1)].next=newLoca;
		}
		
		return true;
	}
}

bool deleteSLL(SLL *L,int pos){
	int logicLoca;
	int length=getLength(L); 
	if(length==0){
		cout<<"表空,无可删除位置"<<endl;
		cout<<"......"<<endl;
		return false;
	}else if(pos<=0||pos>=maxsize-1){
		cout<<"删除位置非法"<<endl;
		cout<<"......"<<endl;
		return false;
	}else if(pos>length){
		cout<<"删除位置越上界"<<endl;
		cout<<"......"<<endl;
		return false;
	}else if(pos>=1&&pos<=length){
		if(length==1){
			freeSLL(L,pos);
			L[maxsize-1].next=0;
			return true;
		}else if(length!=1&&pos==length){
			int prePos=getLogicPos(L,pos-1);
			//cout<<"前驱节点指针域"<<prePos<<endl;
			freeSLL(L,pos);
			L[prePos].next=-1;
			return true;
		}else if(length!=1&&pos==1){
			int logicPos=getLogicPos(L,pos);
			L[maxsize-1].next=L[logicPos].next;
			L[logicPos].next=L[0].next;
			L[0].next=logicPos;
			return true;
		}else if(length!=1&&pos<length&&pos!=1){
			int logicPos=getLogicPos(L,pos);
			int prePos=getLogicPos(L,pos-1);
			L[prePos].next=L[logicPos].next;
			L[logicPos].next=L[0].next;
			L[0].next=logicPos;
			return true;
		}
	}
}

void testFunc(SLL *L){
	int length;
	int timer;
	length=getLength(L);
	int start;
	start=L[maxsize-1].next;
	cout<<"空闲表第一节点号为"<<L[0].next<<endl;
	for(timer=1;timer<=length;timer++){
		cout<<timer<<"号节点当前数据域为"<<L[start].data<<","<<"指针域为"<<L[start].next<<endl;
		start=L[start].next;
	}
	cout<<"非空闲表第一节点号为"<<L[maxsize-1].next<<endl;
	cout<<"当前表长为"<<length<<endl;
	cout<<"......"<<endl;
}

int main(void){
	SLL L[maxsize];
	initSLL(L);
	insertSLL(L,1,65);
	insertSLL(L,2,66);
	insertSLL(L,3,67);
	deleteSLL(L,2);
	insertSLL(L,3,69);
	
	
	testFunc(L);
	return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值