Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

container/Linked_List.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature Linked_List
#
# -----------------------------------------------------------------------

# abstract type for 'Linked_List'
#
public Linked_List(public T type) ref : Sequence T is


  # next is the following element of the linked list, or nil if there are
  # no more elements in the list after this element.
  #
  public next option (Linked_List T) => abstract


  # the data stored in this element of the list.
  #
  public data T => abstract


  # helper feature for tail-recursive implementation of as_string.
  #
  private as_string0(pfx String) String =>
    match next
      ll Linked_List T => ll.as_string0 (pfx + "{ll.data} -> ")
      nil => pfx + "nil ]"


  # a representation of this linked list as a string.
  #
  public redef as_string =>
    Linked_List.this.as_string0 "[ {data} -> "


  # return this linked list as a list
  #
  redef as_list list T =>
    match next
      ll Linked_List T => (data : ()->ll.as_list)
      nil => (data : ()->nil)