数据结构(C#)--双向链表

 

双向链表的定义以及常用的操作

 

  1 namespace  DounlyLinkedlist
  2 ExpandedBlockStart.gifContractedBlock.gif {
  3    //定义双向链表的结点
  4    public class Node
  5ExpandedSubBlockStart.gifContractedSubBlock.gif    {
  6        public Object Element;
  7        public Node FLink;
  8        public Node BLink;
  9
 10        public Node()
 11ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 12            Element = null;
 13            FLink = null;
 14            BLink = null;
 15        }

 16
 17        public Node(Object element)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 19            Element = element;
 20            FLink = null;
 21            BLink = null;
 22        }

 23
 24    }

 25
 26    //链表操作的类
 27    public class LinkedList
 28ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 29        //此处和原书也不一致
 30        public Node Header;
 31
 32        public LinkedList()
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 34            Header = new Node("Header");
 35            Header.FLink = null;
 36            Header.BLink = null;  
 37        }

 38
 39        //查找结点
 40        private Node Find(Object item)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 42            Node Current = new Node();
 43            Current = Header;
 44            while (Current.Element != item)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 46                Current = Current.FLink;
 47            }

 48            return Current;
 49        }

 50
 51        //插入结点
 52        public void InsertNode(Object item,Object postionItem)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 54            Node Current = new Node();
 55            Node NewItem = new Node(item);
 56            Current = Find(postionItem);
 57            if (Current != null)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 59                NewItem.FLink = Current.FLink;
 60                NewItem.BLink = Current;
 61                Current.FLink = NewItem;
 62            }

 63        }

 64
 65        //删除结点
 66        public void Remove(Object item)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 68            Node P = Find(item);
 69            if (P.FLink != null)
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 71                P.BLink.FLink = P.FLink;
 72                P.FLink.BLink = P.BLink;
 73                P.BLink = null;
 74                P.FLink = null;
 75            }

 76            
 77        }

 78
 79
 80        //查找双向链表最后一个结点元素
 81        private Node FindLast()
 82ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 83            Node Current = new Node();
 84            Current = Header;
 85            while (!(Current.FLink == null))
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 87                Current = Current.FLink;
 88            }

 89            return Current;
 90        }

 91
 92
 93        //逆向打印双向链表
 94        public void PrintReverse()
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 96            Node Current = new Node();
 97            Current = FindLast();
 98            while (!(Current.BLink == null))
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            {
100                Console.WriteLine(Current.Element);
101                Current = Current.BLink;
102            }

103        }

104
105        //打印双向链表
106        public void Print()
107ExpandedSubBlockStart.gifContractedSubBlock.gif        {
108            Node Current = new Node();
109            Current = Header;
110            while (!(Current.FLink == null))
111ExpandedSubBlockStart.gifContractedSubBlock.gif            {
112                Console.WriteLine(Current.FLink.Element);
113                Current = Current.FLink;
114            }

115        }

116    }

117}

 

具体调用代码:

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1static void Main(string[] args)
 2ExpandedBlockStart.gifContractedBlock.gif        {
 3            DounlyLinkedlist.Node FirstNode = new DounlyLinkedlist.Node("Tommy");
 4            DounlyLinkedlist.Node SecondNode = new DounlyLinkedlist.Node("Wei");
 5            DounlyLinkedlist.Node ThirdNode = new DounlyLinkedlist.Node("Chencaixia");
 6            DounlyLinkedlist.Node FourthNode = new DounlyLinkedlist.Node("weiwei");
 7            DounlyLinkedlist.LinkedList MyDoublrLink = new DounlyLinkedlist.LinkedList();
 8            MyDoublrLink.Header.FLink = FirstNode;
 9            MyDoublrLink.Header.BLink = null;
10            FirstNode.FLink = SecondNode;
11            FirstNode.BLink = MyDoublrLink.Header;
12            SecondNode.FLink = ThirdNode;
13            SecondNode.BLink = FirstNode;
14            ThirdNode.FLink = FourthNode;
15            ThirdNode.BLink = SecondNode;
16            FourthNode.BLink = ThirdNode;
17            FourthNode.FLink = null;
18            MyDoublrLink.InsertNode("test""Chencaixia");
19            MyDoublrLink.Remove("Wei");
20            MyDoublrLink.Print();
21            MyDoublrLink.PrintReverse();
22            Console.WriteLine(MyDoublrLink.Header.FLink.Element);
23
24            Console.ReadLine();
25
26        }

 

转载于:https://www.cnblogs.com/chencaixia/archive/2008/01/19/1045391.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值