Skip to content

Instantly share code, notes, and snippets.

@Gnimuc
Created October 4, 2015 09:26
Show Gist options
  • Save Gnimuc/090649bd51dd9518eb55 to your computer and use it in GitHub Desktop.
Save Gnimuc/090649bd51dd9518eb55 to your computer and use it in GitHub Desktop.
[SO] Assign the object reference not the object
type Chain
value :: Int
right :: Chain
left :: Chain
#Make the last link in the chain point to itself
#so as to spare us from the julia workaround for nulls
Chain(value::Int) = (chain = new(); chain.value = value; chain.right = chain; chain.left = chain; chain)
end
function rotateright(x::Chain)
temp = x
x = x.left
temp.left = x.right
x.right = temp
x
end
c=Chain(0)
cr = Chain(10)
cl = Chain(20)
c.left = cl
c.right = cr
out = rotateright(c)
@Gnimuc
Copy link
Author

Gnimuc commented Oct 4, 2015

more complicated case:

crr = Chain(110)
crl = Chain(120)
clr = Chain(210)
cll = Chain(220)

cr.left = crl
cr.right = crr
cl.left = cll
cl.right = clr

out = rotateright(c)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment