insert head and tail for linked list
This commit is contained in:
parent
c192f44bd7
commit
1a1e7e06b3
89
scala/src/main/scala/ch06_linkedlist/SinglyLinkedList.scala
Normal file
89
scala/src/main/scala/ch06_linkedlist/SinglyLinkedList.scala
Normal file
@ -0,0 +1,89 @@
|
||||
package ch06_linkedlist
|
||||
|
||||
// the model class for the linked list
|
||||
class Node(var data: Int, var next: Option[Node])
|
||||
|
||||
class SinglyLinkedList(var headOpt: Option[Node]) {
|
||||
|
||||
//define constructor without param
|
||||
def this() = this(None)
|
||||
|
||||
|
||||
def findByValue(value: Int): Option[Node] = {
|
||||
None
|
||||
}
|
||||
|
||||
def findByIndex(index: Int): Option[Node] = {
|
||||
None
|
||||
}
|
||||
|
||||
def insertToHead(value: Int): Unit = {
|
||||
val newNode = new Node(value, None)
|
||||
insertToHead(newNode)
|
||||
}
|
||||
|
||||
def insertToHead(newNode: Node): Unit = {
|
||||
headOpt match {
|
||||
case None =>
|
||||
//it's an empty linked list, make new node as head
|
||||
headOpt = Some(newNode)
|
||||
case Some(head) =>
|
||||
newNode.next = Some(head)
|
||||
headOpt = Some(newNode)
|
||||
}
|
||||
}
|
||||
|
||||
def insertTail(value: Int): Unit = {
|
||||
val newNode = new Node(value, None)
|
||||
insertTail(newNode)
|
||||
}
|
||||
|
||||
def insertTail(newNode: Node): Unit = {
|
||||
headOpt match {
|
||||
case None =>
|
||||
//it's an empty linked list, make new node as head
|
||||
headOpt = Some(newNode)
|
||||
case Some(head) =>
|
||||
//need to start to find from head to current tail
|
||||
var node = head
|
||||
while (node.next.nonEmpty) {
|
||||
node = node.next.get
|
||||
}
|
||||
//now node is the tail as node.next is None
|
||||
//add new tail
|
||||
node.next = Some(newNode)
|
||||
}
|
||||
}
|
||||
|
||||
def insertAfter(existNode: Node, value: Int) = {}
|
||||
|
||||
def insertAfter(existNode: Node, newNode: Node) = {}
|
||||
|
||||
def insertBefore(existNode: Node, value: Int) = {}
|
||||
|
||||
def insertBefore(existNode: Node, newNode: Node) = {}
|
||||
|
||||
def deleteByNode(node: Node): Option[Node] = {
|
||||
None
|
||||
}
|
||||
|
||||
def deleteByValue(node: Node): Option[Node] = {
|
||||
None
|
||||
}
|
||||
|
||||
def mkString(): String = {
|
||||
headOpt.map(head => {
|
||||
var node = head
|
||||
val result = new StringBuilder
|
||||
|
||||
while (node.next.nonEmpty) {
|
||||
result.append(node.data)
|
||||
node = node.next.get
|
||||
}
|
||||
result.append(node.data)
|
||||
|
||||
result.mkString
|
||||
}).getOrElse("")
|
||||
}
|
||||
|
||||
}
|
18
scala/src/test/scala/ch06_linkedlist/NodeTest.scala
Normal file
18
scala/src/test/scala/ch06_linkedlist/NodeTest.scala
Normal file
@ -0,0 +1,18 @@
|
||||
package ch06_linkedlist
|
||||
|
||||
import org.scalatest.{FlatSpec, Matchers}
|
||||
|
||||
class NodeTest extends FlatSpec with Matchers {
|
||||
|
||||
behavior of "NodeTest"
|
||||
it should "create node with constructor" in {
|
||||
val node1 = new Node(1, None)
|
||||
val node2 = new Node(2, Some(node1))
|
||||
val node3 = new Node(2, Some(node2))
|
||||
|
||||
node1.next should be(None)
|
||||
node1.next = Some(node3)
|
||||
node1.next should equal(Some(node3))
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package ch06_linkedlist
|
||||
|
||||
import org.scalatest.{FlatSpec, Matchers}
|
||||
|
||||
class SinglyLinkedListTest extends FlatSpec with Matchers{
|
||||
|
||||
behavior of "SinglyLinkedListTest"
|
||||
|
||||
it should "insertToHead for new values" in {
|
||||
val list:SinglyLinkedList = new SinglyLinkedList()
|
||||
|
||||
list.insertToHead(0)
|
||||
list.insertToHead(1)
|
||||
list.insertToHead(2)
|
||||
list.insertToHead(3)
|
||||
|
||||
list.mkString() should equal("3210")
|
||||
|
||||
}
|
||||
|
||||
it should "insertTail for new values" in {
|
||||
val list:SinglyLinkedList = new SinglyLinkedList()
|
||||
|
||||
list.insertTail(0)
|
||||
list.insertTail(1)
|
||||
list.insertTail(2)
|
||||
list.insertTail(3)
|
||||
|
||||
list.mkString() should equal("0123")
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user