Author(s): David Trallero Mena.
Version: 1.11#222 (2004/5/24, 13:8:7 CEST)
Version of last change: 1.9#116 (2003/12/1, 22:4:57 CET)
This library allows the user to work with double linked lists. An index is used for referencing the current element in the list. Such index can be modified by next and prev predicated. The value of the current index can be obtained with top predicate
ddlist):- use_module(library(ddlist)).
ddlist)
Usage: null_list(NullList)
NullList is an empty list
A is a free variable.
(term_typing:var/1)
A is double linked list
(ddlist:ddlist/1)
Usage: next(ddlist(OldList), ddlist(NewList))
NewList is OldList but index is set to next element of current element of OldList. It satisfies next(A,B),prev(B,A)
Usage: prev(ddlist(OldList), ddlist(NewList))
NewList is OldList but index is set to previous element of current element of OldListop) of OldList
Usage: insert(ddlist(List), Element, ddlist(NewList))
NewList is like List but with Element inserted _BEFORE_ the current index It satisfies insert( X , A , Xp ) , delete( Xp , X ).
Usage: insert_top(ddlist(List), Element, ddlist(NewList))
Element as new top of NewList and push the rest of elements after it. It satisfies top( NewList , element )
Usage: insert_after(ddlist(List), Element, ddlist(NewList))
NewList is like List but with Element inserted _AFTER_ the current index It satisfies insert_after( X , A , Xp ) , delete_after( Xp , X ).
Usage: delete(ddlist(OldList), ddlist(NewList))
NewList does not have the previous element (top(prev)) of OldList
Usage: delete_top(ddlist(OldList), ddlist(NewList))
NewList does not have the current element (top) of OldList
Usage: delete_after(ddlist(OldList), ddlist(NewList))
NewList does not have next element to current element (top) of OldList
Usage: top(ddlist(List), Element)
Element is the element pointed by index
Usage: rewind(ddlist(OldList), ddlist(NewList))
NewList is the OldList but index is set to 0
Usage: forward(ddlist(OldList), ddlist(NewList))
NewList is the OldList but index is set to lentgh of NewList
Usage: length(ddlist(List), Len)
Len is the length of the List
Usage: length_next(ddlist(List), Len)
Len is the length from the current index till the end
Usage: length_prev(ddlist(List), Len)
Len is the length from the beginning till the current index
Usage: ddlist(X)
X is double linked list
ddlist)
Two simple examples of the use of the ddlist library package follow.
:- module( ddl1 , _ , [] ).
:- use_module( library(ddlist) ).
main(A,B):-
% L = []
null_list( L ),
% L = [1]
insert_after( L , 1 , L1 ),
% L = [1,2]
insert_after( L1 , 2 , L2 ),
% L = [1,3,2]
insert_after( L2 , 3 , L3 ),
% L = [1,3,2] => A = [1]
top( L3 , A ),
% L = [3,2]
next( L3 , PL3 ),
% L = [3,2] => A = [3]
top( PL3 , B ).
:- module( ddl2 , _ , [] ).
:- use_module( library(ddlist) ).
main(A,B):-
% L = []
null_list( L ),
% L = [1]
insert_after( L , 1 , L1 ),
% L = [1,2]
insert_after( L1 , 2 , L2 ),
% L = [1,2]
insert( L2 , 3 , L3 ),
% L = [3,1,2]
prev( L3 , PL3 ),
% L = [],
forward( PL3 , FOR ),
% L = [2]
prev( FOR , FOR1 ),
% L = [2] => A = 2
top( FOR1 , A ),
% L = [1,2]
prev( FOR1 , FOR2 ),
% L = [2]
delete_after( FOR2 , FOR3 ),
% L = [3,2]
prev( FOR3, FOR4 ),
% L = [3,2] => B = 3
top( FOR4 , B ).
Go to the first, previous, next, last section, table of contents.