all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
1
Task/Singly-linked-list-Traversal/0DESCRIPTION
Normal file
1
Task/Singly-linked-list-Traversal/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
Traverse from the beginning of a singly-linked list to the end.
|
||||
4
Task/Singly-linked-list-Traversal/1META.yaml
Normal file
4
Task/Singly-linked-list-Traversal/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Iteration
|
||||
note: Data Structures
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defun traverse (xs)
|
||||
(if (endp xs)
|
||||
(cw "End.~%")
|
||||
(prog2$ (cw "~x0~%" (first xs))
|
||||
(traverse (rest xs)))))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
MODE STRINGLIST = STRUCT(STRING value, REF STRINGLIST next);
|
||||
|
||||
STRINGLIST list := ("Big",
|
||||
LOC STRINGLIST := ("fjords",
|
||||
LOC STRINGLIST := ("vex",
|
||||
LOC STRINGLIST := ("quick",
|
||||
LOC STRINGLIST := ("waltz",
|
||||
LOC STRINGLIST := ("nymph",NIL))))));
|
||||
|
||||
REF STRINGLIST node := list;
|
||||
WHILE node ISNT REF STRINGLIST(NIL) DO
|
||||
print((value OF node, space));
|
||||
node := next OF node
|
||||
OD;
|
||||
print(newline)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
var A:Node;
|
||||
//...
|
||||
for(var i:Node = A; i != null; i = i.link)
|
||||
{
|
||||
doStuff(i);
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
with Ada.Containers.Doubly_Linked_Lists;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Traversal_Example is
|
||||
package Int_List is new Ada.Containers.Doubly_Linked_Lists(Integer);
|
||||
use Int_List;
|
||||
procedure Print(Position : Cursor) is
|
||||
begin
|
||||
Put_Line(Integer'Image(Element(Position)));
|
||||
end Print;
|
||||
The_List : List;
|
||||
begin
|
||||
for I in 1..10 loop
|
||||
The_List.Append(I);
|
||||
end loop;
|
||||
-- Traverse the list, calling Print for each value
|
||||
The_List.Iterate(Print'access);
|
||||
end traversal_example;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
a = 1
|
||||
a_next = b
|
||||
b = 2
|
||||
b_next = c
|
||||
c = 3
|
||||
|
||||
traverse("a")
|
||||
return
|
||||
|
||||
traverse(element)
|
||||
{
|
||||
MsgBox % element . "= " . %element%
|
||||
name := element . "_next"
|
||||
while, %name%
|
||||
{
|
||||
element := %name%
|
||||
msgbox % %name% . "= " . %element%
|
||||
name := %name% . "_next"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
DIM node{pNext%, iData%}
|
||||
DIM a{} = node{}, b{} = node{}, c{} = node{}
|
||||
|
||||
a.pNext% = b{}
|
||||
a.iData% = 123
|
||||
b.iData% = 789
|
||||
c.iData% = 456
|
||||
|
||||
PROCinsert(a{}, c{})
|
||||
|
||||
PRINT "Traverse list:"
|
||||
pnode% = a{}
|
||||
REPEAT
|
||||
!(^node{}+4) = pnode%
|
||||
PRINT node.iData%
|
||||
pnode% = node.pNext%
|
||||
UNTIL pnode% = 0
|
||||
|
||||
END
|
||||
|
||||
DEF PROCinsert(here{}, new{})
|
||||
new.pNext% = here.pNext%
|
||||
here.pNext% = new{}
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
struct link *first;
|
||||
// ...
|
||||
struct link *iter;
|
||||
for(iter = first; iter != NULL; iter = iter->next) {
|
||||
// access data, e.g. with iter->data
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
(doseq [x xs] (println x)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(dolist (x list)
|
||||
(print x))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(loop for ref = list then (rest ref)
|
||||
until (null ref)
|
||||
do (print (first ref)))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
struct SLinkedNode(T) {
|
||||
T data;
|
||||
typeof(this)* next;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
alias N = SLinkedNode!int;
|
||||
auto lh = new N(1, new N(2, new N(3, new N(4))));
|
||||
|
||||
for (auto p = lh; p; p = p.next)
|
||||
write(p.data, " ");
|
||||
writeln();
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
import tango.io.Stdout;
|
||||
import tango.util.collection.LinkSeq;
|
||||
|
||||
void main() {
|
||||
auto m = new LinkSeq!(char[]);
|
||||
m.append("alpha");
|
||||
m.append("bravo");
|
||||
m.append("charlie");
|
||||
foreach (val; m)
|
||||
Stdout(val).newline;
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
uses system ;
|
||||
|
||||
type
|
||||
|
||||
// declare the list pointer type
|
||||
plist = ^List ;
|
||||
|
||||
// declare the list type, a generic data pointer prev and next pointers
|
||||
List = record
|
||||
data : pointer ;
|
||||
next : pList ;
|
||||
end;
|
||||
|
||||
// since this task is just showing the traversal I am not allocating the memory and setting up the root node etc.
|
||||
// Note the use of the carat symbol for de-referencing the pointer.
|
||||
|
||||
begin
|
||||
|
||||
// beginning to end
|
||||
while not (pList^.Next = NIL) do pList := pList^.Next ;
|
||||
|
||||
end;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
var linkedList := [1, [2, [3, [4, [5, [6, [7, null]]]]]]]
|
||||
|
||||
while (linkedList =~ [value, next]) {
|
||||
println(value)
|
||||
linkedList := next
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
var linkedList := makeLink(1, makeLink(2, makeLink(3, empty)))
|
||||
|
||||
while (!(linkedList.null())) {
|
||||
println(linkedList.value())
|
||||
linkedList := linkedList.next()
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
traverse [] = []
|
||||
traverse (x::xs) = x :: traverse xs
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
xs = [& x \\ x <- [1..1000]]//lazy list
|
||||
|
||||
traverse xs
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
: list-each ( linked-list quot: ( data -- ) -- )
|
||||
[ [ data>> ] dip call ]
|
||||
[ [ next>> ] dip over [ list-each ] [ 2drop ] if ] 2bi ; inline recursive
|
||||
|
||||
SYMBOLS: A B C ;
|
||||
|
||||
A <linked-list>
|
||||
[ C <linked-list> list-insert ] keep
|
||||
[ B <linked-list> list-insert ] keep
|
||||
|
||||
[ . ] list-each
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// traverse the linked list
|
||||
Node? current := a
|
||||
while (current != null)
|
||||
{
|
||||
echo (current.value)
|
||||
current = current.successor
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: last ( list -- end )
|
||||
begin dup @ while @ repeat ;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
: walk ( a xt -- )
|
||||
>r begin ?dup while
|
||||
dup cell+ @ r@ execute
|
||||
@ repeat r> drop ;
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
subroutine traversal(list,proc)
|
||||
type(node), target :: list
|
||||
type(node), pointer :: current
|
||||
interface
|
||||
subroutine proc(node)
|
||||
real, intent(in) :: node
|
||||
end subroutine proc
|
||||
end interface
|
||||
current => list
|
||||
do while ( associated(current) )
|
||||
call proc(current%data)
|
||||
current => current%next
|
||||
end do
|
||||
end subroutine traversal
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
subroutine printNode(data)
|
||||
real, intent(in) :: data
|
||||
write (*,*) data
|
||||
end subroutine
|
||||
|
||||
subroutine printAll(list)
|
||||
type(node), intent(in) :: list
|
||||
call traversal(list,printNode)
|
||||
end subroutine printAll
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
start := &Ele{"tacos", nil}
|
||||
end := start.Append("burritos")
|
||||
end = end.Append("fajitas")
|
||||
end = end.Append("enchilatas")
|
||||
for iter := start; iter != nil; iter = iter.Next {
|
||||
fmt.Println(iter)
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
map (>5) [1..10] -- [False,False,False,False,False,True,True,True,True,True]
|
||||
|
||||
map (++ "s") ["Apple", "Orange", "Mango", "Pear"] -- ["Apples","Oranges","Mangos","Pears"]
|
||||
|
||||
foldr (+) 0 [1..10] -- prints 55
|
||||
|
||||
traverse :: [a] -> [a]
|
||||
traverse list = map func list
|
||||
where func a = -- ...do something with a
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
procedure main ()
|
||||
ns := Node(1, Node(2, Node (3)))
|
||||
until /ns do { # repeat until ns is null
|
||||
write (ns.value)
|
||||
ns := ns.successor
|
||||
}
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
foo"0 {:"1 list
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
LinkedList<Type> list = new LinkedList<Type>();
|
||||
|
||||
for(Type i: list){
|
||||
//each element will be in variable "i"
|
||||
System.out.println(i);
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
LinkedList.prototype.traverse = function(func) {
|
||||
func(this);
|
||||
if (this.next() != null)
|
||||
this.next().traverse(func);
|
||||
}
|
||||
|
||||
LinkedList.prototype.print = function() {
|
||||
this.traverse( function(node) {print(node.value())} );
|
||||
}
|
||||
|
||||
var head = createLinkedListFromArray([10,20,30,40]);
|
||||
head.print();
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
to last :list
|
||||
if empty? bf :list [output first :list]
|
||||
output last bf :list
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
list = 1:10;
|
||||
for k=1:length(list)
|
||||
printf('%i\n',list(k))
|
||||
end;
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf('%d\n', list(:));
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Print /@ {"rosettacode", "kitten", "sitting", "rosettacode", "raisethysword"}
|
||||
->
|
||||
rosettacode
|
||||
kitten
|
||||
sitting
|
||||
rosettacode
|
||||
raisethysword
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
# let li = ["big"; "fjords"; "vex"; "quick"; "waltz"; "nymph"] in
|
||||
List.iter print_endline li ;;
|
||||
big
|
||||
fjords
|
||||
vex
|
||||
quick
|
||||
waltz
|
||||
nymph
|
||||
- : unit = ()
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for(node := head; node <> Nil; node := node->GetNext();) {
|
||||
node->GetValue()->PrintLine();
|
||||
};
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
RCListElement *current;
|
||||
for(current=first_of_the_list; current != nil; current = [current next] )
|
||||
{
|
||||
// to get the "datum":
|
||||
// id dat_obj = [current datum];
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
my $list = 1 => 2 => 3 => 4 => 5 => 6 => Mu;
|
||||
|
||||
loop (my $l = $list; $l; $l = $l.value) {
|
||||
say $l.key;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
use MONKEY_TYPING;
|
||||
augment class Pair {
|
||||
method traverse () {
|
||||
gather loop (my $l = self; $l; $l = $l.value) {
|
||||
take $l.key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
my $list = [=>] 'Ⅰ' .. 'Ⅻ', Mu;
|
||||
say ~$list.traverse;
|
||||
|
|
@ -0,0 +1 @@
|
|||
(mapc println '(a "cde" (X Y Z) 999))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(for X '(a "cde" (X Y Z) 999)
|
||||
(println X) )
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<@ LETCNSLSTLIT>public holidays|開國紀念日^和平紀念日^婦女節、兒童節合併假期^清明節^國慶日^春節^端午節^中秋節^農曆除夕</@>
|
||||
<@ OMT>From First to Last</@>
|
||||
<@ ITEFORSZELSTLIT>public holidays|
|
||||
<@ SAYLST>...</@><@ ACTMOVFWDLST>...</@>
|
||||
</@>
|
||||
<@ OMT>From Last to First (pointer is still at end of list)</@>
|
||||
<@ ITEFORSZELSTLIT>public holidays|
|
||||
<@ SAYLST>...</@><@ ACTMOVBKWLST>...</@>
|
||||
</@>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<# 指定构造列表字串>public holidays|開國紀念日^和平紀念日^婦女節、兒童節合併假期^清明節^國慶日^春節^端午節^中秋節^農曆除夕</#>
|
||||
<# 忽略>From First to Last</#>
|
||||
<# 迭代迭代次数结构大小列表字串>public holidays|
|
||||
<# 显示列表>...</#><# 运行移位指针向前列表>...</#>
|
||||
</#>
|
||||
<# 忽略>From Last to First (pointer is still at end of list)</#>
|
||||
<# 迭代迭代次数结构大小列表字串>public holidays|
|
||||
<# 显示列表>...</#><# 运行移位指针向后列表>...</#>
|
||||
</#>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Procedure traverse(*node.MyData)
|
||||
While *node
|
||||
;access data, i.e. PrintN(Str(*node\Value))
|
||||
*node = *node\next
|
||||
Wend
|
||||
EndProcedure
|
||||
|
||||
;called using
|
||||
traverse(*firstnode.MyData)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
for node in lst:
|
||||
print node.value
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
class LinkedList(object):
|
||||
"""USELESS academic/classroom example of a linked list implemented in Python.
|
||||
Don't ever consider using something this crude! Use the built-in list() type!
|
||||
"""
|
||||
def __init__(self, value, next):
|
||||
self.value = value;
|
||||
self.next = next
|
||||
def __iter__(self):
|
||||
node = self
|
||||
while node != None:
|
||||
yield node.value
|
||||
node = node.next;
|
||||
|
||||
lst = LinkedList("big", next=
|
||||
LinkedList(value="fjords",next=
|
||||
LinkedList(value="vex", next=
|
||||
LinkedList(value="quick", next=
|
||||
LinkedList(value="waltz", next=
|
||||
LinkedList(value="nymph", next=None))))));
|
||||
|
||||
for value in lst:
|
||||
print value,;
|
||||
print
|
||||
|
|
@ -0,0 +1 @@
|
|||
: traverse ( l- ) repeat @ 0; again ;
|
||||
|
|
@ -0,0 +1 @@
|
|||
last [ drop ] ^types'LIST each@
|
||||
|
|
@ -0,0 +1 @@
|
|||
last [ @d->name puts space ] ^types'LIST each@
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
head = ListNode.new("a", ListNode.new("b", ListNode.new("c")))
|
||||
head.insertAfter("b", "b+")
|
||||
|
||||
# then:
|
||||
head.each {|node| print node.value, ","}
|
||||
puts
|
||||
|
||||
# or
|
||||
current = head
|
||||
begin
|
||||
print current.value, ","
|
||||
end while current = current.succ
|
||||
puts
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
list$ = "now is the time for all good men"
|
||||
for lnk = 1 to 8
|
||||
print lnk;"->";word$(list$,lnk)
|
||||
next lnk
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def traverse1[T](xs: Seq[T]): Unit = xs match {
|
||||
case s if s.isEmpty => ()
|
||||
case _ => { Console.println(xs.head);
|
||||
traverse1(xs.tail)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
def traverse2[T](xs: Seq[T]) = for (x <- xs) Console.println(x)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(define (traverse seq func)
|
||||
(if (null? seq)
|
||||
'()
|
||||
(begin
|
||||
(func (car seq))
|
||||
(traverse (cdr seq) func))))
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
oo::define List {
|
||||
method for {varName script} {
|
||||
upvar 1 $varName var
|
||||
set elem [self]
|
||||
while {$elem ne ""} {
|
||||
set var [$elem value]
|
||||
uplevel 1 $script
|
||||
set elem [$elem next]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
set list {}
|
||||
foreach n {1 3 5 7 2 4 6 8} {
|
||||
set list [List new $n $list]
|
||||
}
|
||||
$list for x {
|
||||
puts "we have a $x in the list"
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1 2 3 4 5] [print] each
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
Private Sub Iterate(ByVal list As LinkedList(Of Integer))
|
||||
Dim node = list.First
|
||||
Do Until node Is Nothing
|
||||
node = node.Next
|
||||
Loop
|
||||
End Sub
|
||||
|
|
@ -0,0 +1 @@
|
|||
repeat Node:= Node(0) until Node=0
|
||||
Loading…
Add table
Add a link
Reference in a new issue