C# 链表

链表是节点的列表,节点包含两部分:值和链接,其中值部分用于储存数据,链接部分用于指向下一个元素的地址,是引用 类型。

单链表

    public class LinkedList
{
private class Node
{
internal int value;
internal Node next;
public Node(int v,Node n)
{
value = v;
next = n;
}
public Node(int v)
{
value = v;
next = null;
}
}
private Node head;
private int count = 0; //Size of the List
public int Size()
{
return count;
}
//Empty Field
public bool Empty
{
get { return count == 0; }
}
///Insertion methods
//Insert element at the head
public void AddHead(int value)
{
head = new Node(value, head);
count++;
}
//Insertion of an element at the end
public void AddTail(int value)
{
var newNode = new Node(value);
var curr = head;
if (curr == null)
{
head = newNode;
count++;
return;
} while (curr.next != null)
curr = curr.next;
curr.next = newNode;
count++;
} //Tranversing and print all
public void Print()
{
var temp = head;
while (temp != null)
{
Console.Write(temp.value + " ");
temp = temp.next;
}
}
//Sorted Insert
public void SortedInsert(int value)
{
var newNode = new Node(value);
var curr = head;
if (curr == null || curr.value > value)
{
newNode.next = curr;
head = newNode;
return;
}
while(curr.next!=null && curr.value < value)
{
curr = curr.next;
}
newNode.next = curr.next;
curr.next = newNode;
}
//Search Element
public bool IsPresent(int data)
{
var temp = head;
while (temp != null)
{
if (temp.value == data)
return true;
temp = temp.next;
}
return false;
}
//Delete element
public int RemoveHead()
{
if (Empty)
throw new InvalidOperationException("EmptyListException");
int value = head.value;
head = head.next;
count--;
return value;
}
//Delete node from the linked list given its value
public bool DeleteNode(int delValue)
{
var temp = head;
if (Empty)
return false;
if (delValue == head.value)
{
head = head.next;
count--;
return true;
}
while (temp.next != null)
{
if (temp.next.value == delValue)
{
temp.next = temp.next.next;
count--;
return true;
}
else
temp = temp.next;
}
return false;
}
//Delete all the occurrence of particular value in the linked list
public void DeleteNodes(int delValue)
{
Node currNode = head;
Node nextNode;
if(currNode!=null && currNode.value == delValue)
{
head = currNode.next;
currNode = head;
}
while (currNode != null)
{
nextNode = currNode.next;
if(nextNode!=null && nextNode.value == delValue)
{
currNode.next = nextNode.next;
}
else
{
currNode = currNode.next;
}
}
}
//Delete all the elements of a linked list
public void FreeList()
{
head = null;
count = 0;
} //Reverse a linked list
public void Reverse()
{
Node curr = head;
Node next=null;
Node pre=null;
while (curr != null)
{
next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
head = pre;
}
//Recursively reverse a singly linked list
private Node reverseRecursUtil(Node currentNode,Node nextNode)
{
Node ret;
if (currentNode == null)
return null;
if (currentNode.next == null)
{
currentNode.next = nextNode;
return currentNode;
}
ret = reverseRecursUtil(currentNode.next, currentNode);
currentNode.next = nextNode; return ret;
} public void ReverseRecurse()
{
head=reverseRecursUtil(head, null);
}
public LinkedList CopyListReversed()
{
var ll = new LinkedList();
Node tempNode = null;
Node tempNode2 = null;
Node curr = head;
while (curr != null)
{
tempNode2 = new Node(curr.value, tempNode);
curr = curr.next;
tempNode = tempNode2;
}
ll.head = tempNode;
return ll;
}
public LinkedList CopyList()
{
var ll = new LinkedList();
Node headNode = null;
Node tailNode = null;
Node tempNode = null;
Node curr = head;
if (curr == null)
return null;
headNode = new Node(curr.value, null);
tailNode = headNode;//使得headNode的地址与tailNode的地址指向一样
curr = curr.next;
while (curr != null)
{
tempNode = new Node(curr.value, null);
tailNode.next = tempNode;
tailNode = tempNode;
curr = curr.next;
}
ll.head = headNode;
return ll;
}
public bool CompareList(LinkedList ll)
{
return compareList(head,ll.head);
} private bool compareList(Node head1,Node head2)
{
if (head1 == null && head2 == null)
return true;
else if ((head1 == null) || (head2 == null) || (head1.value != head2.value))
return false;
else
{
return compareList(head1.next, head2.next);
}
}
public int FindLength()
{
Node curr = head;
int count = 0;
while (curr!= null)
{
curr = curr.next;
count++;
}
return count;
}
//Find Node from beginning
public int NthFromBegining(int index)
{
var size = FindLength();
if (index > size - 1|| index<0)
throw new Exception("null element");
var curr = head;
var count = 0;
while(curr!=null && count < index)
{
curr = curr.next;
count++;
}
return curr.value;
}
public int NthFromEnd(int index)
{
int size = FindLength();
int startIndex;
if (size != 0 && index > size-1)
throw new Exception("null element"); startIndex = size - 1 - index;
return NthFromBegining(startIndex);
}
} class Program
{
public static void Main()
{
var ll = new LinkedList();
ll.AddHead(0);
ll.AddHead(1);
ll.AddHead(2);
ll.Print();
Console.WriteLine();
ll.ReverseRecurse();
ll.Print();
}

Reverse图解

基于递归的reverse实现

C#数据结构及算法之链表的更多相关文章

  1. Java数据结构和算法(四)--链表

    日常开发中,数组和集合使用的很多,而数组的无序插入和删除效率都是偏低的,这点在学习ArrayList源码的时候就知道了,因为需要把要 插入索引后面的所以元素全部后移一位. 而本文会详细讲解链表,可以解 ...

  2. C语言 - 基础数据结构和算法 - 企业链表

    听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...

  3. C语言 - 基础数据结构和算法 - 单向链表

    听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...

  4. Java数据结构和算法之链表

    三.链表 链结点 在链表中,每个数据项都被包含在‘点“中,一个点是某个类的对象,这个类可认叫做LINK.因为一个链表中有许多类似的链结点,所以有必要用一个不同于链表的类来表达链结点.每个LINK对象中 ...

  5. JavaScript 数据结构与算法3(链表)

    学习数据结构的 git 代码地址: https://gitee.com/zhangning187/js-data-structure-study 1.链表 本章学习如何实现和使用链表这种动态的数据结构 ...

  6. 数据结构和算法 – 8.链表

    8.1.数组存在的问题 在处理列表的时候数组是常用的数据结构.数组可以对所存储的数据项提供快速地存取访问,而且它很易于进行循环遍历操作.当然,数组已经是语言的一部分了,用户不需要使用额外的内存,也不需 ...

  7. JavaScript数据结构与算法(六) 链表的实现

    // 链表存储有序的元素集合,但不同于数组,链表中的元素在内存中并不是连续放置的.每个 // 元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成.下图展 // 示了一个链表的 ...

  8. js数据结构与算法--单链表的实现与应用思考

    链表是动态的数据结构,它的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用(也称指针或链接)组成. 现实中,有一些链表的例子. 第一个就是寻宝的游戏.你有一条线索,这条线索是指向寻找下一条线 ...

  9. 用Python实现的数据结构与算法:链表

    一.概述 链表(linked list)是一组数据项的集合,其中每个数据项都是一个节点的一部分,每个节点还包含指向下一个节点的链接(参考 <算法:C语言实现>). 根据结构的不同,链表可以 ...

  10. 数据结构与算法之链表-javascript实现

    链表的定义: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点 ...

随机推荐

  1. [Windows] 联发科秒开bl一键版(mtk)

    声明 不是所有的联发科都可 天机 8000 8100 9000等不行 已知 天机820 天机1000 mtk G90t 天机800 可以 其余自己测试 除了新款均可 第一步 下载软件 (是个压缩包需要 ...

  2. SSM - 狂神的项目示例

    出于对狂神的崇拜,总结SSM项目. 基本介绍 项目分层 基本介绍 项目名称:ssmbuild 介绍:通过书籍管理系统实现一个简单的SSM项目,可以作为其他Java Web项目的借鉴. 主要功能模块:查 ...

  3. 【BUUCTF】Easy MD5

    [BUUCTF]Easy MD5 (SQL注入.PHP代码审计) 题目来源 收录于:BUUCTF BJDCTF2020 题目描述 抓包得到提示 select * from 'admin' where ...

  4. 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库

    前言 今天大姚给大家分享一套基于 Material Design 规范实现的.开源(MIT license)且免费的 Blazor 和 Razor 通用组件库:MatBlazor. Blazor介绍 ...

  5. 基于正则化的图自编码器在推荐算法中的应用 Application of graph auto-encoders based on regularization in recommendation algorithms

    引言 看过的每一篇文章,都是对自己的提高.不积跬步无以至千里,不积小流无以成江海,积少成多,做更好的自己. 本文基于2023年4月6日发表于SCIPEERJ COMPUTER SCIENCE(PEER ...

  6. 内网环境部署Deepseek+Dify,构建企业私有化AI应用

    0.简介 公司为生产安全和保密,内部的服务器不可连接外部网络,为了可以在内网环境下部署,采用的方案为ollama(Docker)+Dify(Docker Compose),方便内网环境下迁移和备份,下 ...

  7. IDEA激活后提示We could not validate your license xxxx解决办法

    ​ 示例报错样式:每次激活单开都会不断弹出--很烦 解决思路是拦截jetbrains校验license的请求, 也就是找到校验license的接口屏蔽掉就行; > 别信网上的配置代理拦截 这样做 ...

  8. 大模型提示词(Prompt)模板推荐

    只有提示词写得好,与大模型的互动才能更高效.提示词不仅仅是与AI对话的起点,更是驱动模型产生高质量输出的关键因素.本文将介绍大模型提示词的概念.意义,并分享一些实用的提示词模板,帮助AI玩家更好地利用 ...

  9. MySQL中怎么分析性能?

    MySQL中主要有4种方式可以分析数据库性能,分别是慢查询日志,profile,Com_xxx和explain. 慢查询日志 先用下面命令查询慢查询日志是否开启, show variables lik ...

  10. ZeroTier简单使用

    在 CentOS 系统下,你可以使用以下命令行操作来管理 ZeroTier 网络和设备.首先,确保已经正确安装 ZeroTier 软件,你可以按照以下步骤进行安装: 安装 ZeroTier: Zero ...