InterfaceImylist<t>{ //O (1)//ADD an item at the beginning of the list. voidAddFirst (T item); //O (1)//ADD an item at the end of the list. voidaddlast (T itme); //O (1)//Remove the item from the list. If The list contains multiple//copies/references of the item, remove one of them. voidRemove (T item); //O (1)//Reverse the list. voidReverse ();}classMylist<t>: imylist<t>, ienumerable{Private BOOL_isreverse =false;/PrivateLinkedlist<t> _list =NewLinkedlist<t> ();//doubly linked list <br> PrivateDictionary<t, list<linkedlistnode<t>>> _dict =NewDictionary<t, list<linkedlistnode<t>>> ();//used to temporarily store node Public voidAddFirst (T item) {LinkedListNode<T> node = _isreverse?_list. AddLast (item): _list. AddFirst (item); Adddict (item, node); } Private voidAdddict (T item, linkedlistnode<t>node) {List<LinkedListNode<T>> samelist =NULL; if(!_dict. TryGetValue (Item, outsamelist)) {Samelist=NewList<linkedlistnode<t>>(); } samelist. ADD (node); _dict[item]=samelist; } Public voidaddlast (T item) {LinkedListNode<T> node = _isreverse?_list. AddFirst (item): _list. AddLast (item); Adddict (item, node); } Public voidRemove (T item) {List<LinkedListNode<T>> samelist =NULL; if(_dict. TryGetValue (Item, outsamelist)) { if(Samelist.count >0) {_list. Remove (samelist[0]); Samelist.removeat (0); _dict. Remove (item); } } } Public voidReverse () {_isreverse= !_isreverse;; } /// <summary> ///implementation of Iterators/// </summary> /// <returns></returns> PublicIenumerator<t>GetEnumerator () {LinkedListNode<T>node; if(!_isreverse) Node=_list. First; Elsenode=_list. Last; while(true) { if(node = =NULL) yield Break; yield returnnode. Value; if(!_isreverse) Node=node. Next; Elsenode=node. Previous; } } /// <summary> ///implementing iterations because he doesn't have a generic type./// </summary> /// <returns></returns>IEnumerator Ienumerable.getenumerator () {returnGetEnumerator (); }}
C # linkedlist<t> doubly linked list