📄 dlinkedlist.as
字号:
else
head = dLinkedInsertionSortCmp(head, compareStringCaseSensitive);
}
else
{
head = dLinkedInsertionSort(head, b == 18);
}
}
else
{
if (b & 4)
{
if (b == 20)
head = dLinkedMergeSortCmp(head, compareStringCaseSensitiveDesc);
else
if (b == 12)
head = dLinkedMergeSortCmp(head, compareStringCaseInSensitive);
else
if (b == 28)
head = dLinkedMergeSortCmp(head, compareStringCaseInSensitiveDesc);
else
head = dLinkedMergeSortCmp(head, compareStringCaseSensitive);
}
else
if (b & 16)
head = dLinkedMergeSort(head, true);
}
}
}
else
head = dLinkedMergeSort(head);
}
/**
* Searches for an item in the list by using strict equality (===) and returns
* and iterator pointing to the node containing the item or null if the
* item was not found.
*
* @param obj The item to search for
* @param from A DListIterator object pointing to the node in the list from which to start searching for the item.
* @return An DListIterator object pointing to the node with the found item or null if no item exists matching the input data or the iterator is invalid.
*/
public function nodeOf(obj:*, from:DListIterator = null):DListIterator
{
if (from != null)
if (from.list != this)
return null;
var node:DListNode = (from == null) ? head : from.node;
while (node)
{
if (node.data === obj)
return new DListIterator(this, node);
node = node.next;
}
return null;
}
/**
* Searches for an item in the list, working backward from the last item,
* by using strict equality (===) and returns and iterator pointing to the node
* containing the item or null if the item wasn't found.
*
* @param obj The item to search for
* @param from A DListIterator object pointing to the node in the list from which to start searching for the item.
* @return A DListIterator object pointing to the node with the found item or null if no item exists matching the input data or the iterator is invalid.
*/
public function lastNodeOf(obj:*, from:DListIterator = null):DListIterator
{
if (from != null)
if (from.list != this)
return null;
var node:DListNode = (from == null) ? tail : from.node;
while (node)
{
if (node.data === obj)
return new DListIterator(this, node);
node = node.prev;
}
return null;
}
/**
* Adds nodes to and removes nodes from the list. This method modifies the list.
*
* @param start A DListIterator object pointing to the node where the insertion or deletion begins. The iterator
* is updated so it still points to the original node, even if the node now belongs to another list.
* @param deleteCount An integer that specifies the number of nodes to be deleted. This number includes
* the node referenced by the iterator. If no value is specified for the deleteCount parameter,
* the method deletes all of the nodes from the start start iterator to the tail of the list.
* If the value is 0, no nodes are deleted.
* @param args Specifies the values to insert into the list, starting at the iterator's node specified by the start parameter.
* Nodes
*
* @param return A DLinkedList object containing the nodes that were removed from the original list or null if the
* iterator is invalid.
*/
public function splice(start:DListIterator, deleteCount:uint = 0xffffffff, ...args):DLinkedList
{
if (start) if (start.list != this) return null;
if (start.node)
{
var s:DListNode = start.node;
var t:DListNode = start.node.prev;
var c:DLinkedList = new DLinkedList();
var i:int, k:int;
if (deleteCount == 0xffffffff)
{
if (start.node == tail) return c;
while (start.node)
{
c.append(start.node.data);
start.remove();
}
start.list = c;
start.node = s;
return c;
}
else
{
for (i = 0; i < deleteCount; i++)
{
if (start.node)
{
c.append(start.node.data);
start.remove();
}
else
break;
}
}
k = args.length;
if (k > 0)
{
if (_count == 0)
{
for (i = 0; i < k; i++)
append(args[i]);
}
else
{
var n:DListNode;
if (t == null)
{
n = prepend(args[0]);
for (i = 1; i < k; i++)
{
n.insertAfter(new DListNode(args[i]));
if (n == tail) tail = n.next;
n = n.next;
_count++;
}
}
else
{
n = t;
for (i = 0; i < k; i++)
{
n.insertAfter(new DListNode(args[i]));
if (n == tail) tail = n.next;
n = n.next;
_count++;
}
}
}
start.node = n;
}
else
start.node = s;
start.list = c;
return c;
}
return null;
}
/**
* Removes and appends the head node to the tail.
*/
public function shiftUp():void
{
var t:DListNode = head;
if (head.next == tail)
{
head = tail;
head.prev = null;
tail = t;
tail.next = null;
head.next = tail;
tail.prev = head;
}
else
{
head = head.next;
head.prev = null;
tail.next = t;
t.next = null;
t.prev = tail;
tail = t;
}
}
/**
* Removes and prepends the tail node to the head.
*/
public function popDown():void
{
var t:DListNode = tail;
if (tail.prev == head)
{
tail = head;
tail.next = null;
head = t;
head.prev = null;
head.next = tail;
tail.prev = head;
}
else
{
tail = tail.prev;
tail.next = null;
head.prev = t;
t.prev = null;
t.next = head;
head = t;
}
}
/**
* Reverses the linked list in place.
*/
public function reverse():void
{
if (_count == 0) return;
var mark:DListNode;
var node:DListNode = tail;
while (node)
{
mark = node.prev;
if (!node.next)
{
node.next = node.prev;
node.prev = null;
head = node;
}
else
if (!node.prev)
{
node.prev = node.next;
node.next = null;
tail = node;
}
else
{
var next:DListNode = node.next;
node.next = node.prev;
node.prev = next;
}
node = mark;
}
}
/**
* Converts the node data in the linked list to strings,
* inserts the given separator between the elements,
* concatenates them, and returns the resulting string.
*
* @return A string consisting of the nodes converted to
* strings and separated by the specified parameter.
*/
public function join(sep:*):String
{
if (_count == 0) return "";
var s:String = "";
var node:DListNode = head;
while (node.next)
{
s += node.data + sep;
node = node.next;
}
s += node.data;
return s;
}
/**
* Checks if a given item exists.
*
* @return True if the item is found, otherwise false.
*/
public function contains(obj:*):Boolean
{
var node:DListNode = head;
while (node)
{
if (node.data == obj) return true;
node = node.next;
}
return false;
}
/**
* Clears the list by unlinking all nodes
* from it. This is important to unlock
* the nodes for the garbage collector.
*/
public function clear():void
{
var node:DListNode = head;
head = null;
var next:DListNode;
while (node)
{
next = node.next;
node.next = node.prev = null;
node = next;
}
_count = 0;
}
/**
* Creates an iterator object pointing
* at the first node in the list.
*
* @returns An iterator object.
*/
public function getIterator():Iterator
{
return new DListIterator(this, head);
}
/**
* Creates a doubly linked iterator object pointing
* at the first node in the list.
*
* @returns A DListIterator object.
*/
public function getListIterator():DListIterator
{
return new DListIterator(this, head);
}
/**
* The total number of nodes in the list.
*/
public function get size():int
{
return _count;
}
/**
* Checks if the list is empty.
*/
public function isEmpty():Boolean
{
return _count == 0;
}
/**
* Converts the linked list into an array.
*
* @return An array.
*/
public function toArray():Array
{
var a:Array = [];
var node:DListNode = head;
while (node)
{
a.push(node.data);
node = node.next;
}
return a;
}
/**
* Returns a string representing the current object.
*/
public function toString():String
{
return "[DLinkedList > has " + size + " nodes]";
}
/**
* Prints out all elements in the list (for debug/demo purposes).
*/
public function dump():String
{
if (head == null) return "DLinkedList, empty";
var s:String = "DLinkedList, has " + _count + " node" + (_count == 1 ? "" : "s") + "\n|< Head\n";
var itr:DListIterator = getListIterator();
for (; itr.valid(); itr.forth())
s += "\t" + itr.data + "\n";
s += "Tail >|";
return s;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -