Created
June 23, 2013 02:34
-
-
Save PuercoPop/5843512 to your computer and use it in GitHub Desktop.
Failing at linked lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defpackage :cl-datastructures | |
(:use :cl)) | |
(in-package :cl-datastructures) | |
(defclass linked-list-node () | |
((next :initarg :next :initform nil) | |
(value :initarg :value :initform nil))) | |
(defclass linked-list () | |
((head :initarg :value :initform nil))) | |
(defun make-int-node (value) | |
(declare (fixnum value)) | |
(make-instance 'linked-list-node :value value)) | |
(defmethod display ((node linked-list-node)) | |
(with-slots (next value) node | |
(format t "~A" value) | |
(if (null next) | |
(format t "~%") | |
(display next)))) | |
(defmethod display ((linked-list linked-list)) | |
(with-slots (head) linked-list | |
(unless (null head) | |
(display head)))) | |
(defmethod prepend-node ((linked-list linked-list) (node linked-list-node)) | |
(with-slots (head) linked-list | |
(with-slots (next value) node | |
(if (null head) | |
(setf head node) | |
(progn | |
(setf next head) | |
(setf head head)))))) | |
(defun main () | |
(let ((*linked-list* (make-instance 'linked-list))) | |
(declare (special *linked-list*)) | |
(prepend-node *linked-list* (make-int-node 3)) | |
(prepend-node *linked-list* (make-int-node 2)) | |
(display *linked-list*))) | |
;; Expected 3 2 | |
;; Got 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment