( Title: Single/Double-Ended Single-Linked List Examples File: sexample.fs Version: 1.2.3 Author: David N. Williams License: Public Domain Last revision: September 29, 2002 ANS Forth compatible except for: - Case sensitivity. - REQUIRED loads Forth source if it's not already loaded. - Assumption that zero does not occur as an address. Uses LOCALS| from the Locals extension word set. Please see the file NOTATION. Either of the following files also loads the single-linked node structure words in snode.fs. ) s" sslist.fs" REQUIRED s" dslist.fs" REQUIRED \ *** SNODES ( Single-linked node structure continuation for meal strings data: ) /snode-header 2 cells field: >fruit 2 cells field: >veg 2 cells field: >meat /snode-data: /meal ( We use the same node space for the single-ended [stack] example and the double-ended [queue] example. ) 5 ( nodes) /meal /snode-header make-nodes constant meal-nodes meal-nodes to current-nodes 0unused-nodes \ superfluous in this example \ *** SINGLE-ENDED, SINGLE-LINKED EXAMPLE make-send-list constant meal-stack meal-stack to current-list : empty-meal-stack ( -- ) meal-stack to current-list empty-send-list ; : meal-stack! ( -- ) ( See dexample.fs for an interpreted method. This word sets CURRENT-LIST. Note: We also set CURRENT-NODES to MEAL-NODES here, just in case dexample.fs is also loaded. Then CURRENT-NODES might have been set to LIQ-MEAL-NODES defined there. ) meal-stack to current-list meal-nodes to current-nodes ?new-node >r s" apple" r@ >fruit 2! s" tomato" r@ >veg 2! s" beafsteak" r@ >meat 2! r> ( node) ssprepend ?new-node >r s" orange" r@ >fruit 2! s" green beans" r@ >veg 2! s" pork chop" r@ >meat 2! r> ssprepend ; meal-stack! : .meal-stack ( -- ) ( Make MEAL-STACK the current list and print it from most recent to earliest. ) meal-stack to current-list current-list ( head) @ ( first) LOCALS| node | \ See dexample.fs for non-LOCALS example. BEGIN node WHILE cr ." Fruit: " node >fruit 2@ type ." ; Vegetable: " node >veg 2@ type ." ; Meat: " node >meat 2@ type node ( nx) @ to node REPEAT ; \ *** DOUBLE-ENDED SINGLE-LINKED EXAMPLE make-dend-list constant meal-queue meal-queue to current-list : empty-meal-queue ( -- ) meal-queue to current-list empty-dend-list ; : meal-queue! ( -- ) ( See dexample.fs for an interpreted method. Note: We also set CURRENT-NODES to MEAL-NODES here, just in case dexample.fs is also loaded. Then CURRENT-NODES might have been set to LIQ-MEAL-NODES defined there. ) meal-queue to current-list meal-nodes to current-nodes ?new-node >r s" apple" r@ >fruit 2! s" tomato" r@ >veg 2! s" beafsteak" r@ >meat 2! r> ( node) dsappend \ r> ( node) dsprepend \ stack version ?new-node >r s" orange" r@ >fruit 2! s" green beans" r@ >veg 2! s" pork chop" r@ >meat 2! r> dsappend ; \ r> dsprepend ; \ stack version meal-queue! : .meal-queue ( -- ) ( Make MEAL-QUEUE the current list, and print it from most recent to earliest. ) meal-queue to current-list meal-queue ( head) @ ( first) LOCALS| node | \ See dexample.fs for non-LOCALS example. BEGIN node WHILE cr ." Fruit: " node >fruit 2@ type ." ; Vegetable: " node >veg 2@ type ." ; Meat: " node >meat 2@ type node ( nx) @ to node REPEAT ;