线性表的链式表示和实现
结点在存储器中的位置是任意的,即逻辑上相邻的数据元素在物理上不一定相邻
 线性表的链式表示又称为非顺序映像或链式映像。
 用一组物理位置任意的存储单元来存放线性表的数据元素。
 这组存储单元既可以是连续的,也可以是不连续的,甚至是零散分布在内存中的任意位置上的。
 链表中元素的逻辑次序和物理次序不一定相同。
 
 
 
 
单链表、双链表、循环链表

 
 
 
 
 
链表(链式存储结构)的特点
1、结点在存储器中的位置是任意的,即逻辑上相邻的数据元素在物理上不一定相邻。
 2、访问时只能通过头指针进入链表,并通过每个结点的指针域依次向后顺序扫描其余结点(顺序存取法),所以寻找第一个结点和最后一个结点所花费的时间不等。
 
 
单链表的定义和表示

 
 
 
单链表的基本操作的实现☆☆☆☆

 用C++实现:
My_List* initList()
{
	My_List* temp = new My_List;
	temp->pnext = NULL;
	return temp;
}
 

bool ListEmpty(My_List* temp)
{
	if (temp->pnext == NULL)
	{
		return true;
	}
	else
	{
		return false;
	}
}
 

 
void My_DeleteAll(My_List* temp)
{
	My_List* p;
	while (temp)
	{
		p = temp;
		temp = temp->pnext;
		delete p;
	}
}
 

void My_ClearAll(My_List* temp)
{
	My_List* p;
	My_List* q;
	p = temp->pnext;
	while (p)
	{
		q = p->pnext;
		delete p;
		p = q;
	}
	temp->pnext = NULL;
}
 

 
int ListLength(My_List* temp)
{
	My_List* p;
	p = temp->pnext;
	int num = 0;
	while (p)
	{
		++num;
		p = p->pnext;
	}
	return num;
}
 
老师建议:基础且极其重要,必须熟练掌握。
 
 
 较复杂函数的实现:
 
 

bool GetRnum(My_List* temp, int i, My_List* &mycache)
{
	My_List* p;
	p = temp->pnext;
	int j = 1;
	while (p&&j<i)
	{
		p = p->pnext;
		++j;
	}
	if (!p || j>i)
	{
		return false;
	}
	mycache = p;
	return true;
}
 

 
 
 
int LocateR(My_List* temp, int change)
{
	int j = 1;
	My_List* p = temp->pnext;
	while (p&&p->pdata.My_num!=change)
	{
		p = p->pnext;
		++j;
	}
	if (p!=NULL)
	{
		return j;
	}
	else
	{
		return 0;
	}
}
 

 
 
bool ListInsertfromI(My_List* temp, int i, My_List* mycache)
{
	int j = 0;
	My_List* p = temp;
	while (p&&j<i-1)
	{
		p = p->pnext;
		++j;
	}
	if (!p || j>i - 1)
	{
		return false;
	}
	mycache->pnext = p->pnext;
	p->pnext = mycache;
	return true;
}
 

 
 
bool ListDelete(My_List* temp, int i)
{
	My_List* p = temp;
	int j = 0;
	while (p->pnext&&j<i-1)
	{
		p = p->pnext;
		++j;
	}
	if (!(p->pnext) || j>i - 1)
	{
		return false;
	}
	My_List* q = p->pnext;
	p->pnext = q->pnext;
	delete q;
	return true;
}
 
时间复杂度分析

 
 
 
 
bool FrontInsert(My_List* temp)
{
	My_List* insertcache = new My_List;
	while (true)
	{
		cout << "请输入您所要添加资源的编号>=" << endl;
		cin >> insertcache->pdata.My_num;
		if (cin.fail()) {
			cout << "您输入的不是一个整数,请重新输入:" << endl;
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			continue;
		}
		cout << "请输入您所要添加资源的名字>=" << endl;
		cin >> insertcache->pdata.My_name;
		if (cin.fail()) {
			cout << "您输入的不是一个正确的名字,请重新输入:" << endl;
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			continue;
		}
		cout << "请输入您所要添加资源的剩余量>=" << endl;
		cin >> insertcache->pdata.My_Resources;
		if (cin.fail()) {
			cout << "您输入的不是一个整数,请重新输入:" << endl;
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			continue;
		}
		break;
	}
	insertcache->pnext = temp->pnext;
	temp->pnext = insertcache;
	cout << "需要继续添加吗?1、需要;其他、不需要" << endl;
	int tempnum;
	cin >> tempnum;
	if (cin.fail()) {
		tempnum = 0;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	if (tempnum == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
 

 
bool AfterInsert(My_List* temp)
{
	My_List* insertcache = new My_List;
	while (temp->pnext)
	{
		temp = temp->pnext;
	}
	while (true)
	{
		cout << "请输入您所要添加资源的编号>=" << endl;
		cin >> insertcache->pdata.My_num;
		if (cin.fail()) {
			cout << "您输入的不是一个整数,请重新输入:" << endl;
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			continue;
		}
		cout << "请输入您所要添加资源的名字>=" << endl;
		cin >> insertcache->pdata.My_name;
		if (cin.fail()) {
			cout << "您输入的不是一个正确的名字,请重新输入:" << endl;
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			continue;
		}
		cout << "请输入您所要添加资源的剩余量>=" << endl;
		cin >> insertcache->pdata.My_Resources;
		if (cin.fail()) {
			cout << "您输入的不是一个整数,请重新输入:" << endl;
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
			continue;
		}
		break;
	}
	insertcache->pnext = NULL;
	temp->pnext = insertcache;
	cout << "需要继续添加吗?1、需要;其他、不需要" << endl;
	int tempnum;
	cin >> tempnum;
	if (cin.fail()) {
		tempnum = 0;
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
	}
	if (tempnum == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
 
最后代码运行如下:

 
 









