Created
February 14, 2016 03:05
-
-
Save kiichi/eda2825f6c27d4e8c467 to your computer and use it in GitHub Desktop.
Simple LinkList Example in Swift.
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
| class Node { | |
| var val:Int = -1 | |
| var prev:Node? = nil | |
| var next:Node? = nil | |
| init(inVal:Int){ | |
| val = inVal | |
| } | |
| } | |
| class LinkList { | |
| var head:Node? = nil | |
| var last:Node? = nil | |
| init() { | |
| head = Node(inVal:-1) | |
| last = head | |
| } | |
| func addNode(inVal:Int) { | |
| let tmpNode = Node(inVal: inVal) | |
| last?.next = tmpNode | |
| last = tmpNode | |
| } | |
| func printList() { | |
| var node = head!.next | |
| var str = "" | |
| while (node!.next != nil){ | |
| str = "\(str) \(node!.val) ->" | |
| node = node!.next | |
| } | |
| str = "\(str) \(node!.val)" | |
| print("\(str)") | |
| } | |
| } | |
| var myList = LinkList() | |
| myList.addNode(100) | |
| myList.addNode(200) | |
| myList.addNode(300) | |
| myList.printList() | |
| myList.head?.val | |
| myList.head!.next!.val | |
| myList.head!.next!.next!.val | |
| myList.head!.next!.next!.next!.val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment