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. Linux - 安装JDK(.tar.gz)

    1.上传 jdk-8u351-linux-x64.tar.gz 到 /opt/module 目录下并解压 tar -zxvf jdk-8u351-linux-x64.tar.gz -C /opt/mo ...

  2. Win10 非正常断电重启后出现长时间磁盘100%活动占用

    老毛病,以往半个小时左右会正常,这次上电1s发现风扇被卡马上关机,在启动结果硬生生卡了1h:应该是触发了微软某些后台的某些服务进程,记录一下 可疑涉事进程记录: svchost:Win服务主进程,是层 ...

  3. 【Loongson】支持AXI总线接口

    概述 支持axi接口.但其实没有burst,没有cache,没有tlb,所以仿真起来全是空泡,冲突转发相关功能正确性就测不出来. 从sram改为axi:等待时间从一拍到看信号握手 主要更改/bug处: ...

  4. react使用插件配置px转换为rem

    react使用插件postcss-pxtorem配置px自动转换rem 1.下载postcss-pxtorem插件 npm install postcss postcss-pxtorem --save ...

  5. 程序员必看 Linux 常用命令(重要)

    文件操作命令 find find 用于在指定目录下查找文件或子目录,如果不指定查找目录,则在当前目录下查找 命令格式:find path -option [-print] [ -exec/-ok co ...

  6. ModuleNotFoundError: No module named '_sqlite3' when Python3

    前言 运行 python 报错:ModuleNotFoundError: No module named '_sqlite3' 解决 重新编译安装 python ./configure --enabl ...

  7. Golang 入门 : 浮点数

    浮点数介绍 Go语言提供了两种精度的浮点数:float32 和 float64.它们的算术规范由IEEE754浮点数国际标准定义,该浮点数规范被所有现代的CPU支持. 这些浮点数类型的范围可以从很微小 ...

  8. go gin Next()方法

    示例 gin Next()使用方法 package main import ( "fmt" "github.com/gin-gonic/gin" "n ...

  9. directory 用于数据泵 导入、导出创建的目录。

    1.查询directory目录 select * from dba_directories; 2.创建或者修改 directory目录 create or replace directory 目录名称 ...

  10. 【SpringMVC】使用 @RequestMapping 映射请求

    使用 @RequestMapping 映射请求 Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求 在控制器的类定义及方法定义处都可标注 @Requ ...