3. LinkedList

双向循环列表

1. 区分时候双向,看有没有prev 属性,有就是双向
2. 区分是否循环,看head.pref 是否等于null,等于则不是循环


1. 双向列表的话只是在中间都添加了prev,next.head的prev=null,last的next=null

双向列表

2. 循环链表是个圈,双向顺换是循环链表中的一种

循环链表

3. 双向循环列表是将head.prev=last,last.next=head ,变成了一个圈,但是并不会死循环,因为有head的存在,do{node=node.next;}while(node!=head)

双向循环列表

LinkListNode

LinkedList<T> list;
LinkedListNode<T> next;
LinkedListNode<T> prev;
T item; 

cotr

1. LinkedListNode(T value){item=value;}
2. LinkedListNode(LinkedList<T> list,T value){list=list,item=value;}

prop

1. List{get{return list;}}
2. Next{get{return next==null||next==list.head?null:next;}}  //这样写我很疑惑
3. Previous{Get{return prev==null||this==list.head?null:prev;}}
4. T Value{get;set;} //return item;

method

1. Invalidate()
    {
        // clear field
        list=null;
        next=null;
        prev=null;
    }

LinkedList内部 664行,中等

理解链表的第一步是它内部不是数组,是组合
如果自己来实现的话,永远要记得head=node的情况
LinkedListNode<T> head; //头结点
int count;      //结点数量
int version;    //版本

interface

一句话就是ICollection<T>,所以没有索引器
ICollection<T>,ICollection,IreadOnlyCollection<T>...

ctor

public LinkedList(){} //什么也没有
public LinkedList(IEnumerable<T> collection){} //foreach{AddLast(item);}

private ValidateNode(node);

1. 判断node是否为空
2. 判断node.List==this,不等说明篡改了,你可以用反射进行篡改啊

private ValidateNewNode(node)

1. 判断node是否为空
2. 判断node.List=null;

AddFirst(T value)

public LinkedListNode<T> AddFirst(T value){
    LinkedListNode<T> result=new LinkedListNode<T>(this,value);
    if(head==null){
        InternalInsertNodeToEmptyList(result);
    }else{
        InternalInsertNodeBefore(head,result);
        head=result;   //将新的node设置为head
    }
    return result;
}

private void InternalInsertNodeToEmptyList(LinkedListNode<T> result){
    Debug.Assert(head==null&&count==0,"Linked must be empty when this method is called!");  //在这里使用Debug.Assert() 不错
    //一个节点时,node中prev=next
    newNode.next=newNode;
    newNode.prev=newNode;
    head=newNode;               //设置头结点
    version++;
    count++;
}

private void InternalInsertNodeBefore(LinkedListNode<T> node,LinkedListNode<T> newNode){
    newNode.next=node;
    nextNode.prev=node.prev;
    node.prev.next=newNode;
    node.prev=newNode;
    version++;
    count++;
}

AddBefore(LinkedListNode node,T value)

public LinkedListNode<T> AddBefore(LinkedListNode<T> node,T value){
    ValidateNode(node);
    LinkedListNode<T> result =new LinkedListNode<T>(node.list,value);
    InternalInsertNodeBefore(node,result);  
    if(node == head){  //这种情况就是在原先的head前面插入数据,所以需要修改head
        head=result;
    }
    return result;
}

AddAfter(LinkedListNode node,T value)

public LinkedListNode<T> AddAfter(LinkedListNode<T> node,T value){
    ValidateNode(node);
    LinkedListNode<T> result =new LinkedListNode<T>(node.list,value);
    InternalInsertNodeBefore(node.next,result);  //和AddFirst()内部调用一样,这封装厉害,加法也是减价
    return result;
}

public LinkedListNode Find(T value) O(n)

1. EqualityComparer<T>.Default
2. null判断
3. Equals判断

Remove(T value)

1. Find(T value);
2. InternalRemoveNode(LinkedListNode<T> node)
    1. check all need check
    2. modify next,prev,将中间节点去掉
        node.prev.next=node.next;
        node.next.prev=node.prev;
        if(head==node){  //这个是容易忘记的
            head=node.next;
        }
    3. node.Invalidate()  //clear node
    4. count--;
    5. verson++;

Remove(LinkedListNode node) 同上

如果没有查询的话,你直接构造了一个LinkedListNode,你是不知道它的next,prev的

正向遍历

LinkedListNode<T> node = head;
do{
    node=node.next;  //因为这里是相当于node指向了另一块内存,而不是修改了原先内存中的东西,所以和原先无关
    ... do something
}while(node!=head);

反向遍历

LinkedListNode<T> last=head.prev;
LinkedListNode<T> node=last;
do{
    node=node.prev; //因为这里是相当于node指向了另一块内存,而不是修改了原先内存中的东西,所以和原先无关
    ... dosomething
}while(last!=node)

GetEnumerator()

{
    return new Enumerator(this);
}

public struct Enumerator:IEnumerator,IEnumerator,...

它限制了实现三个方法,
所以构造函数是非常重要的,负责传入this,也就是LinkedList
new Enumerator(this);

转载于:https://www.cnblogs.com/zhangrCsharp/p/7695575.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值