Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 0.88 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. open System
  2.  
  3. type Node<'T>(nodeValue : 'T, next : Node<'T> option) =
  4.  
  5.     member this.NodeValue
  6.         with get() =
  7.             nodeValue
  8.  
  9.     member this.Next
  10.         with get() =
  11.             next
  12.  
  13. type LinkedList<'T>() =
  14.  
  15.     let mutable head = Option<Node<'T>>.None
  16.  
  17.     let rec printList (node : Node<'T> option) =
  18.         match node with
  19.             | None -> None
  20.             | Some(value) ->
  21.                 printfn "%A" value.NodeValue
  22.                 match value.Next with
  23.                     | None -> None
  24.                     | Some(nextNode) -> printList(Some nextNode)
  25.  
  26.     member this.Insert(value) =
  27.         let nextNode = new Node<'T>(value, head)
  28.         head <- Some nextNode
  29.  
  30.     member this.PrintList() =
  31.         printList(head)
  32.  
  33. let list = new LinkedList<string>()
  34. list.Insert("foo")
  35. list.Insert("bar")
  36. list.Insert("bas")
  37. list.PrintList() |> ignore
  38.  
  39. Console.Read() |> ignore