Singly-linked list/Element definition: Difference between revisions
Content added Content deleted
(Added Clean example) |
No edit summary |
||
Line 111: | Line 111: | ||
Due to Forth's lack of typechecking, 'b' in the above examples does not have to be an actual cell, but can instead be the head pointer of the list. |
Due to Forth's lack of typechecking, 'b' in the above examples does not have to be an actual cell, but can instead be the head pointer of the list. |
||
==[[Pop11]]== |
|||
[[Category:Pop11]] |
|||
List are built in into Pop11, so normally on would just use them: |
|||
;;; Use shorthand syntax to createlist. |
|||
lvars l1 = [1 2 three 'four']; |
|||
;;; Allocate a single list node, with value field 1 and the link field |
|||
;;; pointing to empty list |
|||
lvars l2 = cons(1, []); |
|||
;;; print first element of l1 |
|||
front(l1) => |
|||
;;; print the rest of l1 |
|||
back(l1) => |
|||
;;; Use index notation to access third element |
|||
l1(3) => |
|||
;;; modify link field of l2 to point to l1 |
|||
l1 -> back(l2); |
|||
;;; Print l2 |
|||
l2 => |
|||
If however one wants to definie equivalent user-defined type, one |
|||
can do this: |
|||
uses objectclass; |
|||
define :class ListNode; |
|||
slot value = []; |
|||
slot next = []; |
|||
enddefine; |
|||
;;; Allocate new node and assign to l1 |
|||
newListNode() -> l1; |
|||
;;; Print it |
|||
l1 => |
|||
;;; modify value |
|||
1 -> value(l1); |
|||
l1 => |
|||
;;; Allocate new node with initialized values and assign to link field |
|||
;;; of l1 |
|||
consListNode(2, []) -> next(l1); |
|||
l1 => |