blob: c9dff5b41eac15cb8a64dd994fae53c80c86190f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
;;; Copyright (C) Peter McGoron 2024
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, version 3 of the License.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Singly linked list with tail pointer, message passing style.
;;;
;;; LINKED-LIST:
;;;
;;; (PUSH! VAL): Pushes a value to the head of the list.
;;; (PUSH-TAIL! VAL): Pushes a value to the end of the list.
;;; (SET-CDR! VAL): Set the cdr of TAIL to VAL. This will be overrwitten
;;; if PUSH-TAIL! is called again.
;;; (TO-LIST): Returns a list structure.
(define linked-list:new
(lambda ()
(letrec
((head '())
(tail '())
(this
(object/procedures
'push!
(lambda (val)
(set! head (cons val head))
(if (null? tail)
(set! tail head))
this)
'push-tail!
(lambda (val)
(if (null? tail)
(this 'push! val)
(begin
(set-cdr! tail (cons val '()))
(set! tail (cdr tail))))
this)
'set-cdr!
(lambda (val)
(if (null? tail)
(error "cannot set cdr of empty list")
(set-cdr! tail val))
this)
'to-list
(lambda () head))))
this)))
|