双向链表及C#的实现

双向链表图解

程序实现

下面的实现中没有使用头节点(即头节点就是首节点)

Node类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Node<T>
    {
        T data; //数据域
        Node<T> next;   //后继节点
        Node<T> pre;    //前驱节点

        internal Node<T> Next { get => next; set => next = value; }
        internal Node<T> Pre { get => pre; set => pre = value; }
        public T Data { get => data; set => data = value; }

        //构造器
        public Node(T value, Node<T> next, Node<T> pre)
        {
            Data = value;
            Next = next;
            Pre = pre;
        }

        //构造器
        public Node(T value)
        {
            Data = value;
        }

        //构造器
        public Node(Node<T> next,Node<T> pre)
        {
            Next = next;
            Pre = pre;
        }

        //构造器
        public Node(Node<T> next)
        {
            Next = next;
        }

        //构造器
        public Node()
        {
            Data = default(T);
            Next = null;
            Pre = null;
        }
    }
}

DoubleList类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class DoubleList<T>
    {
        Node<T> head; //头节点

        internal Node<T> Head { get => head; set => head = value; }

        //构造器
        public DoubleList() { Head = null; }

        /// <summary>
        /// 链表判空
        /// </summary>
        /// <returns>为空返回true</returns>
        public bool IsEmpty() { return Head == null; }

        /// <summary>
        /// 取链表长度
        /// </summary>
        /// <returns>链表长度</returns>
        public int GetLenth()
        {
            //链表为空
            if (IsEmpty()) { return 0; }
            //只有一个元素
            if (Head.Next == null) { return 1; }
            //遍历链表
            Node<T> c = Head;
            int i = 1;
            while (c.Next != null)
            {
                c = c.Next;
                ++i;
            }
            return i;
        }

        /// <summary>
        /// 在末尾添加新元素
        /// </summary>
        public void Append(T value)
        {
            Node<T> item = new Node<T>(value);

            //链表为空时,直接覆盖首节点
            if (IsEmpty()) { Head = item; return; }

            //只有一个元素时,直接挂载
            if (Head.Next == null)
            {
                Head.Next = item;
                item.Pre = Head;
                return;
            }

            //遍历至末尾添加
            Node<T> c = Head;
            while (c.Next != null) { c = c.Next; }
            c.Next = item;
            item.Pre = c;
        }

        /// <summary>
        /// 在指定位置后插入元素
        /// </summary>
        /// <param name="value">元素值</param>
        /// <param name="i">元素位置</param>
        public void Insert(T value, int i)
        {
            //插入位置大于链表总长度
            if (GetLenth() < i)
            {
                Console.WriteLine("插入位置不正确");
                return;
            }

            Node<T> c = new Node<T>(value);//新节点

            //插入到头节点位置
            if (i == 1)
            {
                Head.Next.Pre = c;  //头节点的后继节点的前驱指向新节点
                c.Next = Head.Next; //新节点的后继指向头节点的后继节点
                c.Pre = Head;   //新节点的前驱指向头节点
                Head.Next = c;  //头节点的后继指向新节点
                return;
            }

            //遍历插入
            int j = 1;
            Node<T> cur = Head;
            while (cur.Next != null && i > j)
            {
                cur = cur.Next;
                ++j;
            }
            if (j == i)
            {
                cur.Next.Pre = c;   //当前节点的后继节点的前驱指向新节点
                c.Next = cur.Next;  //新节点的后继指向当前节点
                c.Pre = cur;    //新节点的前驱指向当前节点
                cur.Next = c;   //当前节点的后继指向新节点
            }
            else
            {
                Console.WriteLine("位置错误");
            }
        }

        /// <summary>
        /// 删除指定位置的元素
        /// </summary>
        /// <param name="i"></param>
        public void Delete(int i)
        {
            if (GetLenth() < i)
            {
                Console.WriteLine("位置不正确");
                return;
            }

            //删除首节点
            if (i == 1)
            {
                Head = Head.Next;
                Head.Pre = null;
                return;
            }

            //遍历删除
            Node<T> c = Head;
            int j = 1;
            while (c.Next != null && j < i)
            {
                c = c.Next;
                ++j;
            }
            if (j == i)
            {
                //如果是最后一个节点,直接将其前驱节点的后继节点置为空,将其前驱节点置为空
                if (c.Next == null)
                {
                    c.Pre.Next = null;
                    c.Pre = null;
                }
                else
                {
                    c.Next.Pre = c.Pre;
                    c.Pre.Next = c.Next;
                }
            }
            else {
                Console.WriteLine("位置错误");
            }
        }

        /// <summary>
        /// 根据位置获取元素值
        /// </summary>
        /// <param name="i">元素位置</param>
        /// <returns>元素值</returns>
        public T GetElem(int i)
        {
            if (GetLenth() < i)
            {
                Console.WriteLine("位置不正确");
                return default(T);
            }

            if (i == 1)
            {
                return Head.Data;
            }

            Node<T> c = Head;
            int j = 1;
            while (c.Next != null && j < i)
            {
                c = c.Next;
                ++j;
            }
            if (i == j)
            {
                return c.Data;
            }
            else
            {
                Console.WriteLine("位置错误");
                return default(T);
            }
        }

        /// <summary>
        /// 根据元素值,获取元素位置
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public int GetPos(T data)
        {
            if (IsEmpty())
            {
                Console.WriteLine("链表为空");
                return -1;
            }

            if (Head.Data.Equals(data))
            {
                return 1;
            }

            Node<T> c = Head;
            int i = 1;
            int length = GetLenth();
            while (c != null && !c.Data.Equals(data) && i <= length)
            {
                c = c.Next;
                ++i;
            }
            if (i <= length)
                return i;
            else
                return -1;
        }
    }
}

Program类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            DoubleList<string> link = new DoubleList<string>();
            link.Append("123");
            link.Append("456");
            link.Append("789");

            //link.Insert("qwe", 1);
            //link.Insert("asd", 2);
            //link.Delete(2);
            //Console.WriteLine(link.GetElem(1));
            Console.WriteLine(link.GetPos("789"));

            Node<string> c = link.Head;
            Console.WriteLine("---正序输出");

            while (c.Next!=null) {
                Console.WriteLine(c.Data);
                c = c.Next;
            }
            Console.WriteLine(c.Data);

            Console.WriteLine("---逆序输出");
            while (c != null)
            {
                Console.WriteLine(c.Data);
                c = c.Pre;
            }
            Console.WriteLine("---长度");
            Console.WriteLine(link.GetLenth());
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容