C# 双向链表

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

namespace csharp链表
{
    class Program
    {
        public class Node<T>
        {
            private Node<T> child;
            private Node<T> parent;
            private T data;
            public  Node(T e)
            {
                this.SetData(e);        //构造函数
            }
            public void SetChild(Node<T> e)
            {
                this.child = e;         //设置child
            }
            public void SetParent(Node<T> e)
            {
                this.parent = e;        //设置parent
            }
            public void SetData(T e)
            {
                this.data = e;
            }
            public T GetData
            {
                get
                {
                    return data;        //读写器
                }

            }
            public Node<T> GetParent
            {
                get
                {
                    return parent;      //读写器
                }

            }
            public Node<T> GetChild
            {
                get
                {
                    return child;       //读写器
                }

            }
        }
        static void Main(string[] args)
        {
         Node<string> e=   CreatList();
         Print(e);
         Console.ReadLine();
        }
        public static  Node<string> CreatList()
        {
            Node<string> Node = new Node<string>("a");
            Node<string> Node1 = new Node<string>("b");//建链表,a->b->c->d->e,返回c结点
            Node.SetChild(Node1);
            Node1.SetParent(Node);
            Node<string> Node2 = new Node<string>("c");
            Node1.SetChild(Node2);
            Node2.SetParent(Node1);
            Node<string> Node3 = new Node<string>("d");
            Node2.SetChild(Node3);
            Node3.SetParent(Node2);
            Node<string> Node4 = new Node<string>("e");
            Node3.SetChild(Node4);
            Node4.SetParent(Node3);
            return Node2;
        }
        public static void Print(Node<string> e)
        {
        Node<string> Node=e;
        while (Node != null)
        {
            Console.WriteLine(Node.GetData);//由下向上打印
            Node = Node.GetParent;
        }
        Node = e;
        while (Node != null)
        {
            Console.WriteLine(Node.GetData);//由下向上打印
            Node = Node.GetChild;
        }
       
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值