C#的链表操作[数据结构-线性表]
链式存储结构图解:
上图中,a1,是数据,紧跟着后面的d1是下一个节点的地址值。也就是一个节点的最后存储的是下一个节点的地址值,最后一个节点的存储的下一个地址值是null,代表链表结束。
1,定义链表类
/// <summary>
/// 单项链表
/// </summary>
public class Node
{
/// <summary>
/// 指针:链表节点的引用,指向下一个节点
/// </summary>
public Node next;
/// <summary>
/// 链表节点对象,即链表的内容
/// </summary>
public object data;
public Node(object data)
{
this.data = data;
next = null;
}
}
2,链表的操作类
/// <summary>
/// 链表操作
/// </summary>
public class LinkNode
{
/// <summary>
/// 链表头结点
/// </summary>
public Node head;
/// <summary>
/// 节点的位置
/// </summary>
private int pos = 0;
public LinkNode()
{
this.head = null;
}
/// <summary>
/// 链表是否为空
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public bool IsEmpty(Node node)
{
bool b = false;
if (node == null)
{
b = true;
}
return b;
}
/// <summary>
/// 节点数
/// </summary>
/// <returns></returns>
public long Length()
{
return Length(head);
}
/// <summary>
/// 节点数
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public long Length(Node node)
{
long len = 0;
while (node != null)
{
len++;
node = node.next;
}
return len;
}
/// <summary>
/// 增加节点
/// </summary>
/// <param name="o"></param>
/// <returns>头插法</returns>
public Node AddNodeHead(object o)
{
Node newNode = new Node(o);
if (IsEmpty(this.head))
{
this.head = newNode;
}
else
{
newNode.next = head;
head = newNode;
}
return this.head;
}
/// <summary>
/// 增加节点
/// </summary>
/// <param name="o"></param>
/// <returns>尾插法</returns>
public Node AddNodeAtLast(object o)
{
Node newNode = new Node(o);
Node p = head;
if (IsEmpty(head))
{
newNode.next = head;
head = newNode;
}
else
{
while (p.next != null)
{
p = p.next;
}
p.next = newNode;
}
return head;
}
/// <summary>
/// 在指定位置插入节点
/// </summary>
/// <param name="index">位置</param>
/// <param name="o">节点数据</param>
/// <returns></returns>
public Node Insert(int index, object o)
{
if (head == null)
{
head = null;
}
else
{
Node node = new Node(o);
long con = Length();//节点个数
if (index == 1)
{
node.next = head;
head = node;
}
else if (index == con)
{
Node p = head;
while (p.next != null)
{
p = p.next;
}
p.next = node;
}
else if (1 < index && index < con)
{
int pos = 1;
Node current = head;
Node previous = head;
while (pos != index)
{
previous = current;
current = current.next;
pos++;
}
node.next = current;
previous.next = node;
}
else
{
}
}
return head;
}
/// <summary>
/// 删除头节点
/// </summary>
/// <returns></returns>
public Node DeleteHead()
{
if (head != null)
{
head = head.next;
}
return head;
}
/// <summary>
/// 删除节点
/// </summary>
/// <returns></returns>
public Node Delete(int index)
{
int con = Convert.ToInt32(Length());
if (1 <= index && index <= con)
{
Node current = head;
Node previous = head;
int pos = 1;
while (pos != index)
{
pos++;
previous = current;
current = current.next;
}
if (current == head)
{
head = head.next;
}
else
{
previous.next = current.next;
}
}
return head;
}
/// <summary>
/// 反转链表
/// </summary>
/// <returns></returns>
public Node Reversal()
{
Node ReversalNode = head;
head = null;
while (ReversalNode != null)
{
AddNodeHead(ReversalNode.data);
ReversalNode = ReversalNode.next;
}
return head;
}
}
C#的链表操作[数据结构-线性表]的更多相关文章
- [数据结构-线性表1.2] 链表与 LinkedList<T>(.NET 源码学习)
[数据结构-线性表1.2] 链表与 LinkedList<T> [注:本篇文章源码内容较少,分析度较浅,请酌情选择阅读] 关键词:链表(数据结构) C#中的链表(源码) 可空类 ...
- [从今天开始修炼数据结构]线性表及其实现以及实现有Itertor的ArrayList和LinkedList
一.线性表 1,什么是线性表 线性表就是零个或多个数据元素的有限序列.线性表中的每个元素只能有零个或一个前驱元素,零个或一个后继元素.在较复杂的线性表中,一个数据元素可以由若干个数据项组成.比如牵手排 ...
- [置顶] ※数据结构※→☆线性表结构(stack)☆============栈 序列表结构(stack sequence)(六)
栈(stack)在计算机科学中是限定仅在表尾进行插入或删除操作的线性表.栈是一种数据结构,它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据.栈 ...
- C# 数据结构 线性表(顺序表 链表 IList 数组)
线性表 线性表是最简单.最基本.最常用的数据结构.数据元素 1 对 1的关系,这种关系是位置关系. 特点 (1)第一个元素和最后一个元素前后是没有数据元素,线性表中剩下的元素是近邻的,前后都有元素. ...
- Java数据结构-线性表之单链表LinkedList
线性表的链式存储结构,也称之为链式表,链表:链表的存储单元能够连续也能够不连续. 链表中的节点包括数据域和指针域.数据域为存储数据元素信息的域,指针域为存储直接后继位置(一般称为指针)的域. 注意一个 ...
- C语言 严蔚敏数据结构 线性表之链表实现
博主最近在考成都大学皇家计算机科学与技术专业,复习专业课数据结构,正好学习到线性结构中的线性表用链表这种存储结构来实现. 首先,数据结构包括1.数据的操作2.逻辑结构3.存储结构(数据结构三要素. 直 ...
- C数据结构 : 线性表 与 链表
一.线性表 一般表现为数组,使用一组地址连续的存储单元依次存储数据元素,如图: 它具有如下特点: 长度固定,必须在分配内存之前确定数组的长度. 存储空间连续,即允许元素的随机访问. 存储密度大,内存中 ...
- c数据结构 -- 线性表之 顺序存储结构 于 链式存储结构 (单链表)
线性表 定义:线性表是具有相同特性的数据元素的一个有限序列 类型: 1:顺序存储结构 定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构 算法: #include <stdio. ...
- 数据结构线性表(js实现)
最近在复习数据结构的过程中,发现基本上数据结构都是用C来实现的,自己之前学习的时候也是用C去写的,由于目前对js更为熟悉一些,所以这里选择使用js去实现其中的某些算法和结构.实际上算法和语言关系不大, ...
随机推荐
- 利用Web Services开发分布式应用
一.引言 在前面文章中分别介绍了MSMQ和.NET Remoting技术,今天继续分享.NET 平台下另一种分布式技术——Web Services 二.Web Services 详细介绍 2.1 We ...
- nexus 使用Raw Repositories 进行maven site 发布
实际项目中我们可能需要进行maven 项目的site 文档发布,一般的处理是生成之后,然后在进行发布到一个静态 服务器进行页面访问,nexus3 提供了一个Raw Repositories 很方便可以 ...
- std::function与std::bind 函数指针
function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却却比函数指针更加灵活,特别是函数指向类 的非静态成员函数时. std::function可以绑定到全局函数/类静态成员 ...
- VirtualBox的vdi映像导入遇到的uuid冲突问题 (转)
virtualbox导入vdi文件时出现下面的问题: 打开hard disk D:\software\GT5.0.0.vdi 失败 Cannot register the hard disk 'D ...
- 【linux】U盘安装启动出现press the enter key to begin the installation process 就不动弹了
今天在物理机上安装centOS6.5 64bit 系统的时候,出现了U盘安装启动出现press the enter key to begin the installation process 就不动 ...
- oracle/ms sql 系统表
sql server系统表详细说明 sysaltfiles 主数据库 保存数据库的文件 syscharsets 主数据库字符集与排序顺序 sysconfigures主数据库 配置选项 syscurco ...
- C#使用WebService
一.新建webservice 新建项目→asp.net Web服务应用程序 或者在现有项目中 点击右键 新建web服务程序asmx 只要在webservice类里面 的方法 标注为[WebMethod ...
- SQL Server 查询表的主键的两种方式
方式1: select b.column_name from information_schema.table_constraints a inner join information_schema. ...
- C语言的第二次实验报告
一.思路及方法 11-8 螺旋方阵 设计二维数组,通过对方阵的行和列进行特征分析找出其中规律,利用循环即可将方阵输出. 12-6 字符串转换成十进制整数 设计字符数组,用getchar函数逐个截取,并 ...
- CentOS6.5 安装mysql-5.7.9
转自:http://forrest-lv.iteye.com/blog/2260703 安装前,需要检查是否已经有mysql服务进程,是否已经装过mysql; 这点很重要,我之前安装CentOS的同 ...