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;
}
}
}
}
C# 双向链表
最新推荐文章于 2024-09-12 17:54:37 发布