List 链表
List
CBrother链表是双向链表,每个元素都知道自己前面的元素与后面的元素,可以快速删除于插入,但是无法直接通过下标访问。
函数 | 描述 | 参数 | 返回值 | 用法 |
---|---|---|---|---|
addFirst(obj) | 添加元素到链表头 | obj:要添加的元素 | 无 | myList.addFirst(5) |
addLast(obj) | 添加元素到链表尾 | obj:要添加的元素 | 无 | myList.addLast(4) |
size() | 获取链表长度 | 无 | 链表元素个数 | var nowsize = myList.size() |
popFirst() | 移除链表头元素,第二个节点设为头 | 无 | 被移除的元素 | var delobj = myList.popFirst() |
popLast() | 移除链表尾元素,倒数第二节点设为尾 | 无 | 被移除的元素 | var delobj = myList.popLast(1) |
clear() | 清空链表 | 无 | 无 | myList.clear() |
begin() | 开始从头遍历链表 | 无 | 无 | myList.begin() |
end() | 开始从尾遍历链表 | 无 | 无 | myList.end() |
get() | 遍历中获取当前节点 | 无 | 当前节点 | var obj = myList.get() |
pop() | 遍历中删除当前节点,跳入便利顺序的下一节点 | 无 | 被移除的元素 | myList.pop() |
next() | 遍历中跳入后面一个节点 | 无 | false:到最尾了,true:成功 | myList.next() |
fornt() | 遍历中跳入前面一个节点 | 无 | false:到头了,true:成功 | myList.fornt() |
insertFornt(obj) | 遍历中在前面插入一个元素 | obj:要添加的元素 | 无 | myList.insertFornt(5) |
insertNext(obj) | 遍历中在后面插入一个元素 | obj:要添加的元素 | 无 | myList.insertNext(5) |
例子:
function main(parm)
{
var myList = new List();
myList.addLast(5);
myList.addLast(4);
myList.addLast(3);
var len = myList.size();
print "myList size:" + len;
var idx = 0;
myList.begin();
do
{
print "myList " + idx + ":" + myList.get();
idx++;
} while(myList.next());
myList.addFirst(10);
myList.addFirst(11);
len = myList.size();
print "after addFirst. myList size:" + len;
idx = 0;
myList.begin();
do
{
print "myList " + idx + ":" + myList.get();
idx++;
} while(myList.next());
myList.popFirst();
myList.popLast();
len = myList.size();
print "after pop. myList size:" + len;
idx = len - 1;
myList.end(); //倒序遍历
do
{
print "myList " + idx + ":" + myList.get();
idx--;
} while(myList.fornt());
myList.clear();
len = myList.size();
print "after clear. myList size:" + len;
}
结果:
myList size:3
myList 0:5
myList 1:4
myList 2:3
after addFirst. myList size:5
myList 0:11
myList 1:10
myList 2:5
myList 3:4
myList 4:3
after pop. myList size:3
myList 2:4
myList 1:5
myList 0:10
after clear. myList size:0
例子2:
function main(parm)
{
var myList = new List();
myList.addLast(5);
myList.addLast(4);
myList.addLast(3);
var len = myList.size();
print "myList size:" + len;
var idx = 0;
myList.begin();
do
{
print "myList " + idx + ":" + myList.get();
idx++;
} while(myList.next());
myList.begin();
do
{
var obj = myList.get();
if(obj == 4)
{
myList.insertFornt(100); //4前面插入100 后面插入200
myList.insertNext(200);
break;
}
} while(myList.next());
var len = myList.size();
print "after insert.myList size:" + len;
idx = 0;
myList.begin();
do
{
print "myList " + idx + ":" + myList.get();
idx++;
} while(myList.next());
print "========================";
idx = 0;
myList.begin();
do
{
var obj = myList.get();
if(obj == 4)
{
myList.pop(); //正序遍历,自动跳入后面一个节点,所以要结束本地循环,因为继续执行myList.next()会多跳过一个节点,导致结果错误。
continue;
}
print "myList " + idx + ":" + myList.get();
idx++;
} while(myList.next());
len = myList.size();
print "after pop. myList size:" + len;
idx = len - 2; //因为下面要删除一个节点,所以多减了1,让序号打印正确
myList.end();
do
{
var obj = myList.get();
if(obj == 100)
{
myList.pop(); //倒序遍历,自动跳入后面一个节点,所以要继续执行myList.fornt()
}
print "myList " + idx + ":" + myList.get();
idx--;
} while(myList.fornt());
}
结果:
myList size:3
myList 0:5
myList 1:4
myList 2:3
after insert.myList size:5
myList 0:5
myList 1:100
myList 2:4
myList 3:200
myList 4:3
========================
myList 0:5
myList 1:100
myList 2:200
myList 3:3
after pop. myList size:4
myList 2:3
myList 1:200
myList 0:5
以上的遍历方法在多线程读的情况下也要加锁,比如线程1调用了next(),线程2又调用了begin(),那么线程1将获取不到自己想要的值。foreach可以解决这个问题。
foreach支持
foreach语句中的key表示当前遍历第几个元素,建议逻辑上不要依赖于这个值,value表示这个元素
function main(parm)
{
var myList = new List();
myList.addLast(5);
myList.addLast(4);
myList.addLast(3);
foreach(k,v : myList)
{
print "k:" + k + ",v=" + v;
}
}
结果:
k:0,v=5
k:1,v=4
k:2,v=3