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

 

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

namespace  DounlyLinkedlist
{
    
//定义双向链表的结点
    public class Node
    
{
        
public Object Element;
        
public Node FLink;
        
public Node BLink;

        
public Node()
        
{
            Element 
= null;
            FLink 
= null;
            BLink 
= null;
        }


        
public Node(Object element)
        
{
            Element 
= element;
            FLink 
= null;
            BLink 
= null;
        }


    }


    
//链表操作的类
    public class LinkedList
    
{
        

        public Node Header;

        
public LinkedList()
        
{
            Header 
= new Node("Header");
            Header.FLink 
= null;
            Header.BLink 
= null;  
        }


        
//查找结点
        private Node Find(Object item)
        
{
            Node Current 
= new Node();
            Current 
= Header;
            
while (Current.Element != item)
            
{
                Current 
= Current.FLink;
            }

            
return Current;
        }


        
//插入结点
        public void InsertNode(Object item,Object postionItem)
        
{
            Node Current 
= new Node();
            Node NewItem 
= new Node(item);
            Current 
= Find(postionItem);
            
if (Current != null)
            
{
                NewItem.FLink 
= Current.FLink;
                NewItem.BLink 
= Current;
                Current.FLink 
= NewItem;
            }

        }


        
//删除结点
        public void Remove(Object item)
        
{
            Node P 
= Find(item);
            
if (P.FLink != null)
            
{
                P.BLink.FLink 
= P.FLink;
                P.FLink.BLink 
= P.BLink;
                P.BLink 
= null;
                P.FLink 
= null;
            }

            
        }



        
//查找双向链表最后一个结点元素
        private Node FindLast()
        
{
            Node Current 
= new Node();
            Current 
= Header;
            
while (!(Current.FLink == null))
            
{
                Current 
= Current.FLink;
            }

            
return Current;
        }



        
//逆向打印双向链表
        public void PrintReverse()
        
{
            Node Current 
= new Node();
            Current 
= FindLast();
            
while (!(Current.BLink == null))
            
{
                Console.WriteLine(Current.Element);
                Current 
= Current.BLink;
            }

        }


        
//打印双向链表
        public void Print()
        
{
            Node Current 
= new Node();
            Current 
= Header;
            
while (!(Current.FLink == null))
            
{
                Console.WriteLine(Current.FLink.Element);
                Current 
= Current.FLink;
            }

        }

    }

}

 

具体调用代码:

 

static   void  Main( string [] args)
        
{

            DounlyLinkedlist.Node FirstNode 
= new DounlyLinkedlist.Node("Tommy");
            DounlyLinkedlist.Node SecondNode 
= new DounlyLinkedlist.Node("Wei");
            DounlyLinkedlist.Node ThirdNode 
= new DounlyLinkedlist.Node("Chencaixia");
            DounlyLinkedlist.Node FourthNode 
= new DounlyLinkedlist.Node("weiwei");
            DounlyLinkedlist.LinkedList MyDoublrLink 
= new DounlyLinkedlist.LinkedList();
            MyDoublrLink.Header.FLink 
= FirstNode;
            MyDoublrLink.Header.BLink 
= null;
            FirstNode.FLink 
= SecondNode;
            FirstNode.BLink 
= MyDoublrLink.Header;
            SecondNode.FLink 
= ThirdNode;
            SecondNode.BLink 
= FirstNode;
            ThirdNode.FLink 
= FourthNode;
            ThirdNode.BLink 
= SecondNode;
            FourthNode.BLink 
= ThirdNode;
            FourthNode.FLink 
= null;
            MyDoublrLink.InsertNode(
"test""Chencaixia");
            MyDoublrLink.Remove(
"Wei");
            MyDoublrLink.Print();
      
            Console.ReadLine();
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值