Last active
August 29, 2015 14:02
-
-
Save ariok/e1aa44ee440784f484d9 to your computer and use it in GitHub Desktop.
This file contains 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
// Playground - noun: a place where people can play | |
import Cocoa | |
/* Node class implementation */ | |
class Node | |
{ | |
var data:String? | |
var link:Node? | |
init(data:String?, link:Node? = nil) | |
{ | |
self.data = data | |
self.link = link | |
} | |
} | |
/* Helper functions */ | |
func printList(node:Node?) | |
{ | |
var l:Node? = node | |
println("\nPrinting --------"); | |
while l{ | |
println("\(l!.data)") | |
l = l!.link | |
} | |
} | |
func reverseList(var node:Node?)->(Node?) | |
{ | |
var new_root:Node? | |
while node{ | |
var next = node!.link | |
node!.link = new_root | |
new_root = node | |
node = next | |
} | |
return new_root | |
} | |
/* Setup the list */ | |
var d = Node(data:"d"); | |
var c = Node(data:"c", link:d); | |
var b = Node(data:"b", link:c); | |
var a = Node(data:"a", link:b); | |
/* Print and reverse */ | |
printList(a); | |
var r = reverseList(a); | |
printList(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment