Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:08:53+00:00 2026-06-13T19:08:53+00:00

I want to implement some kind of a simple linked list. I defined an

  • 0

I want to implement some kind of a simple linked list.

I defined an abstract No, with a field next, its type is Node.
Then I have a bunch of other specific Node, inheriting the abstract node.

I write like this:

abstract class Node {
  def next: Node   
}

case class SpecificNode(nxt: Node) extends Node {
  val next = nxt
}

object NullNode extends Node{
  val next = new Exception("no more node")
}

However, I find that I need to change the field next later, in the SpecificNode,
so I make

case class SpecificNode(nxt: Node) extends Node {
  var next = nxt
}

However, I can not assign to the next field of an instance of some specific node ,e.g sn.next = ..., because the compiler complains next_ is not a member of Node.

Then I changed to use var in the abstract class Node for the next field.

But when I new a NullNode, exception will be thrown (since I defined so..)

So how should I use the var, def, val here?
How should I define the NullNode, which is to signify no more node?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:08:55+00:00Added an answer on June 13, 2026 at 7:08 pm

    You need to explicitly define getter/setter in your interface. In our case (without changing structure and idea):

    trait Node {
        def next: Node
        def next_=(node: Node)
    }
    
    // `var next: Node` generates implementations for `def next: Node` and `def next_=(node: Node)`
    case class SpecificNode(var next: Node) extends Node
    
    object NullNode extends Node {
        def next = throw new Exception("no more node")
        def next_=(node: Node) { throw new Exception("can't change next on null node") }
    }
    
    val n1 = SpecificNode(NullNode)
    val n2 = SpecificNode(SpecificNode(NullNode))
    val n3: Node = SpecificNode(n1)
    
    // some function to test our structure:
    def length(n: Node, prev: Int = 0): Int = if (n == NullNode) prev else length(n.next, prev + 1)
    
    println(length(n3))
    n3.next = n2
    println(length(n3))
    

    Side note: this is mutable linked list, I suggest you to check how immutable list implemented in standard scala library

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement a simple script to do some boring housekeeping on my
I'm new to Iphone development, i want to implement a simple application that some
I think I want to show some kind of confirmation tick type thing by
I'm trying to implement a very simple javascript. I'm building some kind of a
I want to implement some code in excel macro to group some data for
I want to implement a LocationListener . Checked some tutorials and found this: /**
We want to implement a sitemap.xml feature in out CMS system. There is some
I am continuing to program some kind of keyboard navigation in my simple graphic
I want to test some QAbstractListModels I already implemented, is it better to use
I want implement in my software solution an VBA editor but in c# 3.0.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.