栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++动态链表的创建

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++动态链表的创建

C++动态链表的创建—单链表 动态链表的创建: 第一步: 结点以及链表的定义:
#include 
using namespace std;

template
class Node
{
public:
	T data;							//当前节点的数据区域
	Node* next;					//当前节点指向下一节点地址区域
};
template
class LinkList
{
public:
	void Queue();					//实现链表的排序
	void Reverse();					//实现链表的转置
public:
	LinkList();						//创建一个空链表
	~LinkList();					//删除链表
	LinkList(T a[], int n);			//有参构造函数的链表创建
	int Length();					//返回链表的长度
	T Get(int i);					//返回第i个位置的值
	int Locate(T x);				//定位值x在链表中的位置i
	void Insert(int i, T x);		//在第i个位置插入值x
	void Delete(int i);				//删除链表中第i个位置的值
	void PrintInfo();				//遍历链表,打印链表的值
private:
	Node* first;					//链表的头节点。
};

第二步: 依次完成代码实现:创建空链表:

template
LinkList::LinkList()//创建一个空列表
{
	first = new Node;
	this->first->next = NULL;
}

第三步: 有参构造函数(头插法)

template
LinkList::LinkList(T a[],int n)//有参构造函数头插法
{
	first = new Node;
	first->next = NULL;
	for (int i = 0; i < n; i++)
	{
		Node*s = new Node;
		s->data = a[i];
		s->next = first->next;
		first->next = s;
	}
}

第四步: 删除链表

template
LinkList::~LinkList()//删除链表
{
	while (first != NULL)
	{
		Node* p = first;
		first = first->next;
		delete p;
	}
}

第五步: 获取链表的长度

template
int LinkList::Length()//返回链表的长度
{
	int length=0;
	Node *p;
	p = first->next;
	while (p != NULL)
	{
		length++;
		p = p->next;
	}
	return length;
}

第六步: 返回链表第i个位置的值

template
T LinkList::Get(int i)//返回第i个位置的值
{
		Node* p = first->next;
		int count = 1;
	while (p != NULL&&count!=i)
	{
		p = p->next;
		count++;
	}
	return (p->data);
}

第七步: 返回值x在链表中的位置

template 
int LinkList::Locate(T x)//返回x在链表中的位置i
{
	if (first == NULL || first->next == NULL)
		return 0;
	int count = 1;
	Node* p = first->next;
	while (p != NULL)
	{
		if (p->data == x)
			return count;
		p = p->next;
		count++;
	}
	return 0;
}

第八步: 在链表的第i个位置插入值x

template
void LinkList::Insert(int i, T x)			//在第i个位置插入值x
{
	int num = this->Length();
	if (i >num+1||i<1)
	{
		cout << "插入位置不符合范围!" << endl;
		return;
	}
	int count = 1;
	Node* p = first;
	while (p != NULL)
	{
		if (count == i)
		{
			Node* q = new Node;
			q->data = x;
			q->next = p->next;
			p->next = q;
		}
		p = p->next;
		count++;
	}
	return;
}

第九步: 删除链表第i个位置的元素:

template
void LinkList::Delete(int i)					//删除链表第i个位置的数据
{
	if (i<1 || i>this->Length()||first==NULL||first->next==NULL)
	{
		cout << "删除位置不存在!" << endl;
		return;
	}
	int count = 0;
	Node* p = first;
	while (p != NULL&&count!=i-1)
	{
		p = p->next;
		count++;
	}
	if (count == i - 1)
	{
		Node* q = p->next;
		p->next = q->next;
		delete q;
		return;
	}
}

第十步: 遍历链表,打印链表的值:

template
void LinkList::PrintInfo()		//遍历链表,打印链表的值
{
	Node *p = this->first->next;
	while (p != NULL)
	{
		cout << p->data << " ";
		p = p->next;
	}
}

附加:(链表的转置)

template
void LinkList:: Reverse()				//	实现链表反转
{
	if (first->next == NULL)
		return;
	else
	{
		Node * p = first->next;
		Node * q = NULL;
		first->next = NULL;
		while (p != NULL)
		{
			q=p->next;
			p->next = first->next;
			first->next = p;
			p = q;
		}
	}
	return;
}

<判断链表是否有序>:

template
int LinkList::Isorder()			//判断链表是否有序
{
	Node * p = first->next;
	Node * q = NULL;
	int flag = 1;
	if (p != NULL)
		q = p->next;
	while (p != NULL && q != NULL)
	{
		if (p->data > q->data)
		{
			flag == 0;
			break;
		}
	}
	return flag;
}

《实现链表的排序》:

template
void LinkList::Queue()							//实现列表的排序
{
	Node* p = first->next;
	Node* q = NULL;
	Node* s = NULL;
	Node* r = NULL;
	if (p)
	{
		q = p->next;
		p->next = NULL;
	}
	while (q)
	{
		r = q->next;
		s = first;
		while ((s->next) && (s->next->data) < (q->data))
		{
			s = s->next;
		}
		q->next = s->next;
		s->next = q;
		q = r;
	}

}

《删除多余元素》:

template
void LinkList::DeletePlus()				//	删除多余的元素
{
	Node* p = first->next;
	Node* q = NULL;
	Node* r = NULL;
	while (p!=NULL)
	{
		q = p;
		while (q->next != NULL)
		{
			if (p->data == q->next->data)
			{
				r = q->next;
				q->next = r->next;
				delete r;
			}
			else
				q = q->next;
		}
		p = p->next;
	}
}

菜鸟的第二天!闲暇之余康康

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/861859.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号