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

DS-链表学习

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

DS-链表学习

DS-链表学习

类实现链表

结点类的创建
class ListNode//结点类
{
public:
	int data;
	ListNode* next;
	ListNode()
	{
		next = NULL;
	}
	ListNode(int a)
	{
		data = a;
		next = NULL;
	}
};
链表类的创建
class linkList//链表类
{
public:
	ListNode* head;
	int len;
	linkList()//链表初始化
	{
		head = new ListNode();//头节点
		len = 0;//链表长度初始化为0
	}

	linkList(int n, int* arr)
	{
		head = new ListNode();
		len = n;
		ListNode* node = new ListNode [arr[0]];//申请第一个node结点的内存空间
		head = node;
		for (int i = 0; i < n; i++)
		{
			ListNode* newNode = new ListNode(arr[i]);
			node->next = newNode;
			node = node->next;
		}
	}

	~linkList()
	{
		ListNode* p, * q;
		p = head;
		while (p != NULL)
		{
			q = p;
			p = p->next;
			delete q;
		}
		len = 0;
		head = NULL;
	}
查找第i个位置的元素
	int LL_get(int i)
	{
		int j;
		ListNode* p;
		p = head->next;
		j = 1;
		while (p && j < i)
		{
			p = p->next;
			++j;
		}
		if (!p || j > i)
			return error;
		else
			return p->data;

	}
插入元素
	int LL_insert(int i, int item)
	{
		int j;
		ListNode* p,*s;
		p = head;
		j = 1;
		while (p && j < i)
		{
			p = p->next;
			++j;
		}
		if (!p || j > i)
			return error;

		s = new ListNode(item);
		s->data = item;
		s->next = p->next;
		p->next = s;
		return ok;
	}
删除元素
	int LL_del(int i)
	{
		int j;
		ListNode* p, * q;
		p = head;
		j = 1;
		while (p && j < i)
		{
			p = p->next;
			++j;
		}
		if (i<1 ||i>len)
			return error;
		q = p->next;
		p->next = q->next;
		return ok;
	}
链表元素输出
	void LL_display()
	{
		ListNode* p;
		p = head->next;
		while (p)
		{
			cout << p->data << " ";
			p = p->next;
		}
		cout << endl;
	}
例题:

实现:

#include
using namespace std;

#define ok 0
#define error -1

class ListNode//结点类
{
public:
	int data;
	ListNode* next;
	ListNode()
	{
		next = NULL;
	}
	ListNode(int a)
	{
		data = a;
		next = NULL;
	}
};

class linkList//链表类
{
public:
	ListNode* head;
	int len;
	linkList()//链表初始化
	{
		head = new ListNode();//头节点
		len = 0;//链表长度初始化为0
	}

	linkList(int n, int* arr)
	{
		head = new ListNode();
		len = n;
		ListNode* node = new ListNode [arr[0]];//申请第一个node结点的内存空间
		head = node;
		for (int i = 0; i < n; i++)
		{
			ListNode* newNode = new ListNode(arr[i]);
			node->next = newNode;
			node = node->next;
		}
	}

	~linkList()
	{
		ListNode* p, * q;
		p = head;
		while (p != NULL)
		{
			q = p;
			p = p->next;
			delete q;
		}
		len = 0;
		head = NULL;
	}

	int LL_get(int i)
	{
		int j;
		ListNode* p;
		p = head->next;
		j = 1;
		while (p && j < i)
		{
			p = p->next;
			++j;
		}
		if (!p || j > i)
			return error;
		else
			return p->data;

	}
	int LL_insert(int i, int item)
	{
		int j;
		ListNode* p,*s;
		p = head;
		j = 1;
		while (p && j < i)
		{
			p = p->next;
			++j;
		}
		if (!p || j > i)
			return error;

		s = new ListNode(item);
		s->data = item;
		s->next = p->next;
		p->next = s;
		return ok;
	}
	int LL_del(int i)
	{
		int j;
		ListNode* p, * q;
		p = head;
		j = 1;
		while (p && j < i)
		{
			p = p->next;
			++j;
		}
		if (i<1 ||i>len)
			return error;
		q = p->next;
		p->next = q->next;
		return ok;
	}
	void LL_display()
	{
		ListNode* p;
		p = head->next;
		while (p)
		{
			cout << p->data << " ";
			p = p->next;
		}
		cout << endl;
	}
};

int main()
{
	int n;
	cin >> n;
	int* arr = new int[n];
	for (int i = 0; i < n; ++i)
	{
		cin >> arr[i];
	}
	int status;
	linkList* list = new linkList(n, arr);
	list->LL_display();
	int i, item;

	cin >> i >> item;
	status=list->LL_insert(i, item);
	if (status == ok)
		list->LL_display();
	else
		cout << "error" << endl;

	cin >> i >> item;
	status = list->LL_insert(i, item);
	if (status == ok)
		list->LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	status = list->LL_del(i);
	if (status == ok)
		list->LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	status = list->LL_del(i);
	if (status == ok)
		list->LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	status = list->LL_get(i);
	if (status == ok)
		list->LL_display();
	else
		cout << "error" << endl;

	cin >> i;
	status = list->LL_get(i);
	if (status == error)
		cout << "error" << endl;
	else
		cout << status << endl;


	return 0;
}

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

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

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