Tracks
/
Factor
Factor
/
Exercises
/
Simple Linked List
Simple Linked List

Simple Linked List

Medium

Introduction

You work for a music streaming company.

You've been tasked with creating a playlist feature for your music player application.

Instructions

Write a prototype of the music player application.

For the prototype, each song will simply be represented by a number. Given a range of numbers (the song IDs), create a singly linked list.

Given a singly linked list, you should be able to reverse the list to play the songs in the opposite order.

Note

The linked list is a fundamental data structure in computer science, often used in the implementation of other data structures.

The simplest kind of linked list is a singly linked list. That means that each element (or "node") contains data, along with something that points to the next node in the list.

If you want to dig deeper into linked lists, check out this article that explains it using nice drawings.

Words

Define a linked-list-node tuple.

Define a linked-list tuple and provide a M: linked-list length method (from sequences) so the tests can call length directly on your list.

  • <linked-list> ( -- linked-list ) — construct an empty list.
  • >linked-list ( seq -- linked-list ) — push each element of seq onto a fresh list, in order.
  • linked-list>array ( linked-list -- array ) — return the values, top of the list first.
  • list-push ( linked-list value -- linked-list ) — push a value on top.
  • list-pop ( linked-list -- linked-list value ) — remove and return the top value.
  • list-peek ( linked-list -- value ) — return the top value without removing it.
  • list-reverse ( linked-list -- linked-list ) — return a list with the elements in reverse order.

Throw a list-empty error class from list-pop and list-peek when the list is empty.

Edit via GitHub The link opens in a new window or tab
Factor Exercism

Ready to start Simple Linked List?

Sign up to Exercism to learn and master Factor with 47 concepts163 exercises, and real human mentoring, all for free.