Linked Lists in Racket
Table of Contents
1. Linked Lists
data structure built up of nodes pointing to other nodes with values within.
1.1. linked list in racket
1.1.1. From Scratch
(define-type StrList (U MT Node)) (struct MT()) (struct node ([fst : String][rst : StrList])) (Node "apple" (Node "cow" (Node "Cabbage" (MT))))
DONT NEED TO DEFINE OUR OWN TYPES ITS BUILT IN!
1.1.2. Using Cons
cons - basically a node(you can put two things in it)
EX: (cons 3 ’())
note - “’()” refers to empty or null
now from before…
(cons “apple” (cons “cow” (cons “cabbage” ’())))
1.1.3. Getting elements out of Cons
-
Using first
- (first (cons 3 ’())) -> 3
-
Using rest
- (rest (cons 3’()) -> ’()