PB实现链表数据结构

初始化

创建实例并设置数据

n_linked_list ln_linked_list
any la_result[]

ln_linked_list = create n_linked_list
//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})
//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

插入数据

1.在链表头部插入数据

n_linked_list ln_linked_list
any la_result[]

ln_linked_list = create n_linked_list
//头部插入数据
ln_linked_list.unshift("Arthur")
ln_linked_list.unshift("Brandon")
ln_linked_list.unshift("Cersei")
ln_linked_list.unshift("Davos")
//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

2.在链表尾部插入数据

n_linked_list ln_linked_list
any la_result[]

ln_linked_list = create n_linked_list
//尾部插入数据
ln_linked_list.push("Arthur")
ln_linked_list.push("Brandon")
ln_linked_list.push("Cersei")
ln_linked_list.push("Davos")
//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述
3.在链表任意位置插入数据

n_linked_list ln_linked_list
any la_result[]

ln_linked_list = create n_linked_list
//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})
//在链表索引为3的元素前插入数据
ln_linked_list.add(3,"addNode")
//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

删除数据

1.删除并返回链表头部数据

n_linked_list ln_linked_list
any la_result[],la_shift

ln_linked_list = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})

//删除并返回头部数据
ln_linked_list.shift(la_shift)

//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

2.删除并返回链表尾部数据

n_linked_list ln_linked_list
any la_result[],la_pop

ln_linked_list = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})

//删除并返回尾部数据
ln_linked_list.pop(la_pop)

//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

3.删除并返回链表任意位置数据

n_linked_list ln_linked_list
any la_result[],la_del

ln_linked_list = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})

//删除并返回索引为3数据
ln_linked_list.del(3,la_del)

//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

查询数据

1.查询链表全部数据

n_linked_list ln_linked_list
any la_result[]

ln_linked_list = create n_linked_list
//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})
//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

2.遍历链表数据

n_linked_list ln_linked_list
any la_item
string ls_items

ln_linked_list = create n_linked_list
//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})

//遍历数据
do while ln_linked_list.fetchOne(la_item)
	ls_items += la_item + ";"
loop

return 0

在这里插入图片描述
3.查询链表长度

n_linked_list ln_linked_list
long ll_len

ln_linked_list = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})

//查询链表长度
ll_len = ln_linked_list.length()

return 0

在这里插入图片描述
4.查询链表任意位置的数据

n_linked_list ln_linked_list
any la_get

ln_linked_list = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})

//查询索引为3数据
ln_linked_list.get(3,la_get)

return 0

在这里插入图片描述

其他操作

1.连接链表

n_linked_list ln_linked_list,ln_linked_list2
any la_result[]

ln_linked_list = create n_linked_list
ln_linked_list2 = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})
ln_linked_list2.setValues({"Emily","Fermi","George","Henry","Ivy"})

//连接两个链表
ln_linked_list.concat(ln_linked_list2)

//查询链表全部数据
ln_linked_list.fetchAll(la_result)

return 0

在这里插入图片描述

2.切割链表

n_linked_list ln_linked_list,ln_linked_list2
any la_result1[],la_result2[]

ln_linked_list = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})

//在索引3位置前切割链表
ln_linked_list2 = ln_linked_list.cut(3)

//查询链表全部数据
ln_linked_list.fetchAll(la_result1)
ln_linked_list2.fetchAll(la_result2)

return 0

在这里插入图片描述
3.重置链表

n_linked_list ln_linked_list
any la_result1[],la_result2[]

ln_linked_list = create n_linked_list

//设置数据
ln_linked_list.setValues({"Arthur","Brandon","Cersei","Davos"})
//查询链表全部数据
ln_linked_list.fetchAll(la_result1)

//重置链表
ln_linked_list.reset()
//查询链表全部数据
ln_linked_list.fetchAll(la_result2)

return 0

在这里插入图片描述

源代码

发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)
这部分代码拷贝到文本编辑器,另存为 n_linked_node.sru

forward
global type n_linked_node from nonvisualobject
end type
end forward

global type n_linked_node from nonvisualobject
end type
global n_linked_node n_linked_node

type variables
public:
	n_linked_node prior_node
	n_linked_node next_node
	
	any value
end variables

forward prototypes
public subroutine concat (n_linked_node node)
end prototypes

public subroutine concat (n_linked_node node);this.next_node = node
node.prior_node = this
end subroutine

on n_linked_node.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_linked_node.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

这部分代码拷贝到文本编辑器,另存为 n_linked_list.sru

forward
global type n_linked_list from nonvisualobject
end type
end forward

global type n_linked_list from nonvisualobject
end type
global n_linked_list n_linked_list

type variables
private:
	n_linked_node first_node
	n_linked_node last_node
	
	n_linked_node prev_node
	
	long length
end variables

forward prototypes
public function long length ()
public function long pop (ref any value)
public function long shift (ref any value)
private function integer _get_node (long i, ref n_linked_node n)
public function integer get (long i, ref any value)
public function integer set (long i, any value)
public subroutine reset ()
public function n_linked_list cut (long i)
public function long add (long i, any value)
public function long push (any value)
public function long unshift (any value)
public subroutine fetchstart ()
public function boolean fetchone (ref any value)
public function long concat (n_linked_list linked_list)
public function long del (long i, ref any value)
public function long fetchall (ref any value[])
public function long setvalues (any value[])
end prototypes

public function long length ();return length
end function

public function long pop (ref any value);if not isvalid(last_node) then return -1

value = last_node.value

if first_node = last_node then
	destroy first_node
	destroy last_node
else
	last_node = last_node.prior_node
	destroy last_node.next_node
end if

length --
return length
end function

public function long shift (ref any value);if not isvalid(first_node) then return -1

value = first_node.value
if first_node = last_node then
	destroy first_node
	destroy last_node
else
	first_node = first_node.next_node
	destroy first_node.prior_node
end if

length --
return length
end function

private function integer _get_node (long i, ref n_linked_node n);if i > length then return -1

n_linked_node node
long ll_var

if i <= length / 2 then
	node = first_node
	ll_var = i
	do while ll_var > 1
		node = node.next_node
		ll_var --
	loop
else
	node = last_node
	ll_var = length - i + 1
	do while ll_var > 1
		node = node.prior_node
		ll_var --
	loop
end if

n = node
return 0
end function

public function integer get (long i, ref any value);n_linked_node node

if _get_node(i,node) < 0 then return -1

value = node.value
return 0
end function

public function integer set (long i, any value);n_linked_node node

if _get_node(i,node) < 0 then return -1

node.value = value
return 0
end function

public subroutine reset ();destroy first_node
destroy last_node
length = 0
end subroutine

public function n_linked_list cut (long i);n_linked_list linked_list
n_linked_node node,nil

linked_list = create n_linked_list

if _get_node(i,node) < 0 then return linked_list

linked_list.first_node = node
linked_list.last_node = last_node

if isvalid(node.prior_node) then
	node.prior_node.next_node = nil
	last_node = node.prior_node
else
	first_node = nil
	last_node = nil
end if
node.prior_node = nil

linked_list.length = length - i + 1
length = i - 1 

return linked_list
end function

public function long add (long i, any value);n_linked_node node,p,t

if i <= 1 then
	return unshift(value)
elseif i > length then
	return push(value)
end if

t = create n_linked_node
t.value = value

_get_node(i,node)
p = node.prior_node

p.concat(t)
t.concat(node)

length ++
return length
end function

public function long push (any value);n_linked_node node
node = create n_linked_node
node.value = value

if isvalid(last_node) then
	last_node.concat(node)
	last_node = node
else
	first_node = node
	last_node = node
end if

length ++
return length
end function

public function long unshift (any value);n_linked_node node
node = create n_linked_node
node.value = value

if isvalid(first_node) then
	node.concat(first_node)
	first_node = node
else
	first_node = node
	last_node = node
end if

length ++
return length
end function

public subroutine fetchstart ();n_linked_node nil
prev_node = nil
end subroutine

public function boolean fetchone (ref any value);if prev_node = last_node then return false

if isvalid(prev_node) then
	prev_node = prev_node.next_node
else
	if not isvalid(first_node) then return false
	prev_node = first_node
end if

value = prev_node.value
return true




end function

public function long concat (n_linked_list linked_list);n_linked_node fn,ln
long list_length

list_length = linked_list.length()
if list_length <= 0 then return length

linked_list._get_node(1,fn)
linked_list._get_node(list_length,ln)
last_node.concat(fn)
last_node = ln

length += list_length
return length
end function

public function long del (long i, ref any value);n_linked_node node,p,n

if _get_node(i,node) < 0 then return -1

if node = first_node then
	return shift(value)
elseif node = last_node then
	return pop(value)
else
	value = node.value
	p = node.prior_node
	n = node.next_node
	p.concat(n)
	destroy node
end if

return length
end function

public function long fetchall (ref any value[]);n_linked_node node
long i
any la_list[]

node = first_node
do while isvalid(node)
	i += 1
	la_list[i] = node.value
	node = node.next_node
loop

value = la_list
return i
end function

public function long setvalues (any value[]);reset()

long i,n
n_linked_node node[]

n = upperbound(value)
if n <= 0 then return 0

for i = 1 to upperbound(value)
	node[i] = create n_linked_node
	node[i].value = value[i]
next

first_node = node[1]
last_node = node[n]
length = n

if n <= 1 then return length

for i = 1 to n - 1
	node[i].concat(node[i + 1])
next

return length
end function

on n_linked_list.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_linked_list.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值