【11-1】链表

来源:互联网 发布:js定义json数组并赋值 编辑:程序博客网 时间:2024/06/05 10:16

一 链表

1.定义

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。

链表由一系列节点(链表中每一个元素称为节点)组成,节点可以在运行时动态生成。每个节点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个节点地址的指针域。

2.种类

(1) 单向链表

链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始。

这里写图片描述

(2) 双向链表

每个数据节点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个节点开始,都可以很方便地访问它的前驱节点和后继节点。

这里写图片描述

(3) 循环链表

表中最后一个节点的指针域指向头节点,整个链表形成一个环。

这里写图片描述

3.基本操作

(1) 插入新节点

这里写图片描述

(2)删除节点

这里写图片描述

4.创建一个链表的简单实例

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CH_11_Demo_DEMO{    public class Node    {        public object element;        public Node link;        public Node()        {            link = null;            element = null;        }        public Node (object o)        {            element = o;            link = null;        }    }   public  class LinkedList    {        public Node header;        public  int count;        public Node current;        public LinkedList()        {            header = new Node("header");            count = 0;            current = null;        }        public void InsterFirst(Node n) // 在开头插入新数据        {            n.link = header.link;            header.link = n;            count++;        }        public void InsertLast(Node n) // 在末尾插入新数据        {            current = header;            while (current .link !=null)            {                current = current.link;            }            current.link = n;            n.link = null;            count++;        }        public void Remove(Node n)        {            current = header;            while (current.link  != n)            {                current = current.link;            }            current.link = current.link.link;            count--;        }        public bool  Find(object o)        {            current = header.link ;            while (current.element != o)            {                if (current.link != null)                {                    current = current.link;                }                else                {                    Console.WriteLine("There are no " + o.ToString());                    return false;                }                        }            return true;        }        public Node FindFirst()        {            return header.link;        }        public Node FindLast()        {            current = header ;            while (current.link !=null)            {                current = current.link;            }            return current;        }        public Node PreviousItem(Node n)        {            current = header;            while (current.link != n)            {                current = current.link;            }            return current;        }        public void InsertBefore(Node insertone ,Node beforeone)        {            current = this.PreviousItem(beforeone);            insertone.link = current.link;            current.link = insertone;            count++;        }        public void InsertAfter(Node insertone,Node afterone)        {            current = header;            while (current != afterone.link)            {                current = current.link;            }            insertone.link = afterone.link;            afterone.link = insertone;            count++;        }        public void Clear()        {            header.link = null;        }    }}

二 泛型LinkedList类和泛型LinkedListNode类

1.泛型LinkedListNode类

(1)定义

表示泛型LinkedList类中的节点,不能被继承。

(2)属性

List : 获取包含该节点的泛型LinkedList。

Next: 获取LinkedList中的下一个节点。

Previous:获取LinkedList中的上一个节点。

Value: 获取包含在节点中的值。

0 0