链表 Linked List

这篇具有很好参考价值的文章主要介绍了链表 Linked List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

2024.3.15
芝士wa
参考视频:bilibli-数据结构-链表
“印度小哥讲得真好”


链表

  • 对于链表来说,存储数据需要两个部分,一是数据本身,二是指针,该指针指向下一个数据的地址,依次链接,直到最后一个元素,指针指向空(NULL)

链表 Linked List

  • 遍历的时间复杂度为O(n)

  • 插入的时间复杂度为O(n)

  • 删除的时间复杂度为O(n)


链表VS数组

  • 数组是连续存储空间,链表通过指针维系,存储数据并不连续

  • 数组可以通过下标访问元素,只需要O(1)的时间复杂度,而链表则必须按照顺序访问,因此时间复杂度为O(n/2) = O(n)

  • 数组的大小是固定的,在创建数组时确认

  • 优势:链表在添加或删除元素时,避免了不相关元素的复制移动,空间复杂度较小

  • 使用链表时,要格外注意指针问题


链表的C++实现

完整代码
#pragma once
#include <iostream>
using namespace std;


template<class T>
class Node
{
public:
	T data;
	Node* next;
};

template<class T>
class LinkedList
{
public:
	LinkedList();
	~LinkedList();
	Node<T>* head;
	
	int m_num;//记录节点个数
	//尾插法
	void Insert(T x);

	//在特定位置添加
	void Insert(T x, int n);

	//打印,遍历法
	void print();

	//递归方法打印
	void Rprint(Node<T>* p);

	//特定位置删除
	void remove(int n);

	//反转链表,迭代法
	void reverse();

	//反转链表,递归方法
	void Reverse(Node<T>* p);
};

template<class T>
LinkedList<T>::LinkedList()
{
	head = new Node<T>;
	head->next = NULL;
	this->m_num = 0;
}


template<class T>
LinkedList<T>::~LinkedList()
{
	Node<T>* ite = head;
	Node<T>* next;
	while (ite != NULL) {
		next = ite->next;
		delete ite;
		ite = next;
	}
	this->m_num = 0;
}




template<class T>//尾插法添加元素
void LinkedList<T>::Insert(T x)
{
	Node<T>* end = new Node<T>;
	end->data = x;
	end->next = NULL;

	if (head== NULL) {
		//链表为空
		head = end;

		this->m_num++;
		cout << "添加成功,当前节点数量:" << this->m_num << endl;
	}
	else {
		Node<T>* ite = head;
		//链表不为空,得到末尾end
		while (ite->next != NULL) {
			ite = ite->next;
		}
		//现在ite指向最后一个元素
		ite->next = end;
	}
	this->m_num++;
	cout << "添加成功,当前节点数量:" << this->m_num << endl;
}


template<class T>//特定位置添加元素
void LinkedList<T>::Insert(T x, int n)
{
	Node<T>* temp = new Node<T>;
	temp->data = x;
	temp->next = NULL;
	if (n == 1) {
		//在头节点之后添加
		temp->next = head->next;
		head = temp;

		this->m_num++;
		cout << "添加成功,当前节点数量:" << this->m_num << endl;
		return;
	}
	else if(n > 1 && n <= this->m_num){
		Node<T>* ite = head;
		//找到第n-1个元素,
		for (int i = 0; i <= n - 2; i++) {
			ite = ite->next;
		}
		temp->next = ite->next;
		ite->next = temp;
		this->m_num++;
		cout << "添加成功,当前节点数量:" << this->m_num << endl;
		return;
	}
	else {
		cout << "越界" << endl;
		return;
	}
}

template<class T>
void LinkedList<T>::print()
{
	Node<T>* ite = head;
	while (ite!= NULL) {
		cout << ite->data << "\t";
		ite = ite->next;
	}
	cout << endl;
}
template<class T>
void LinkedList<T>::Rprint(Node<T> * p)
{
	if (p == NULL) {
		return;
	}
	Rprint(p->next);
	cout << p->data << "\t";
}

template<class T>
void LinkedList<T>::remove(int n)
{
	Node<T>* temp = head;
	//第一个节点
	if (n == 1) {
		head = head->next;
		delete temp;

		this->m_num--;
		cout << "删除成功,当前节点数量:" << this->m_num << endl;
		return;
	}
	//第2~num之间删除节点
	else if (n > 1 && n <= this->m_num) {
		for (int i = 0; i < n - 2; i++) {
			temp = temp->next;
		}
	
		//现在temp指向的是第n-1个节点
		Node<T>* temp1 = temp->next;//指向第n个
		temp->next = temp1->next;
		delete temp1;

		this->m_num--;
		cout << "删除成功,当前节点数量:" << this->m_num << endl;
		return;
	}
	else {
		cout << "越界" << endl;
		return;
	}
	
}

template<class T>
void LinkedList<T>::reverse()
{
	Node<T>* current, * prev, * next;
	current = head;
	prev = NULL;
	while (current != NULL) {
		next = current->next;
		current->next = prev;
		prev = current;
		current = next;
	}
	head = prev;
}

template<class T>
void LinkedList<T>::Reverse(Node<T>* p)
{
	if (p->next == NULL) {
		head = p;
		return;
	}
	Reverse(p->next);
	p->next->next = p;
	p->next = NULL;
}

  • 模板,泛型
  • 内部维护链表长度
  • 分别用递归和迭代的方式实现了打印和反转
  • 进行了简单的越界判断处理




双向链表

链表 Linked List

template<class T>
class Node
{
  T data;
  Node* prev;
  Node* next;
};

总结

本文介绍了链表的概念和特性,并用C++实现了单链表。文章来源地址https://www.toymoban.com/news/detail-840923.html

到了这里,关于链表 Linked List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【LeetCode 算法】Linked List Cycle II 环形链表 II

    给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从

    2024年02月14日
    浏览(44)
  • Linked List

    补充知识 typedef 给类型换名字,比如 或者来一个结构体指针定义。 离散存储 离散的含义,任何一个点到其他点之间是有间距的。 n个节点离散分配,彼此通过指针相连接,每个节点只有一个前驱节点,每个节点只有一个后继节点,首节点没有前驱节点,尾节点没有后继节点

    2024年02月15日
    浏览(41)
  • Linked List Mock

    203. Remove Linked List Elements Solved Easy Topics Companies Given the  head  of a linked list and an integer  val , remove all the nodes of the linked list that has  Node.val == val , and return  the new head . 206. Reverse Linked List Solved Easy Topics Companies Given the  head  of a singly linked list, reverse the list, and return  the reversed

    2024年04月12日
    浏览(37)
  • PTA 1052 Linked List Sorting

    个人学习记录,代码难免不尽人意。 A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order. Inp

    2024年02月15日
    浏览(36)
  • 1074 Reversing Linked List (PAT甲级)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6. Input Specification: Each input file contains one test case. For each case, the first line

    2024年02月09日
    浏览(41)
  • LeetCode //C - 206. Reverse Linked List

    Given the head of a singly linked list, reverse the list, and return the reversed list.   Example 1: Input head = [1,2,3,4,5] Output [5,4,3,2,1] Example 2: Input head = [1,2] Output [2,1] Example 3: Input head = [] Output [] Constraints: The number of nodes in the list is the range [0, 5000]. -5000 = Node.val = 5000 From: LeetCode Link: 206. Reverse Linked

    2024年02月01日
    浏览(49)
  • LeetCode //C - 141. Linked List Cycle

    Given head , the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a p

    2024年02月11日
    浏览(47)
  • LeetCode142. Linked List Cycle II

    Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to (0-indexed). It

    2024年02月04日
    浏览(40)
  • 【set】个人练习-Leetcode-817. Linked List Components

    题目链接:https://leetcode.cn/problems/linked-list-components/description/ 题目大意:给出一个 vectorint nums ,其中有一些数字。再给出一个链表的头指针 head ,链表内的元素各不相同。如果链表中有某一段(长度大于等于1)的元素都在 nums 中出现过,那么就算一个component,求链表中的co

    2024年02月13日
    浏览(40)
  • LeetCode //C - 328. Odd Even Linked List

    Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the proble

    2024年01月25日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包