Tracks
/
Java
Java
/
Exercises
/
Linked List
Linked List

Linked List

Medium

Introduction

You are working on a project to develop a train scheduling system for a busy railway network.

You've been asked to develop a prototype for the train routes in the scheduling system. Each route consists of a sequence of train stations that a given train stops at.

Instructions

Your team has decided to use a doubly linked list to represent each train route in the schedule. Each station along the train's route will be represented by a node in the linked list.

You don't need to worry about arrival and departure times at the stations. Each station will simply be represented by a number.

Routes can be extended, adding stations to the beginning or end of a route. They can also be shortened by removing stations from the beginning or the end of a route.

Sometimes a station gets closed down, and in that case the station needs to be removed from the route, even if it is not at the beginning or end of the route.

The size of a route is measured not by how far the train travels, but by how many stations it stops at.

Note

The linked list is a fundamental data structure in computer science, often used in the implementation of other data structures. As the name suggests, it is a list of nodes that are linked together. It is a list of "nodes", where each node links to its neighbor or neighbors. In a singly linked list each node links only to the node that follows it. In a doubly linked list each node links to both the node that comes before, as well as the node that comes after.

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

This exercise introduces generics. To make the tests pass you need to construct your class such that it accepts any type of input, e.g. Integer or String.

Generics are useful because they allow you to write more general and reusable code. The Java List and Map implementations are both examples of classes that use generics. By using them you can construct a List containing Integers or a list containing Strings or any other type.

There are a few constraints on the types used in generics. One of them is that once you've constructed a List containing Integers, you can't put Strings into it. You have to specify which type you want to put into the class when you construct it, and that instance can then only be used with that type.

For example you could construct a list of Integers:

List<Integer> someList = new LinkedList<>();

Now someList can only contain Integers. You could also do:

List<String> someOtherList = new LinkedList<>()

Now someOtherList can only contain Strings.

Another constraint is that any type used with generics cannot be a primitive type, such as int or long. However, every primitive type has a corresponding reference type, so instead of int you can use Integer and instead of long you can use Long.

It can help to look at an example use case of generics to get you started.


Source

Classic computer science topic
Edit via GitHub The link opens in a new window or tab
Java Exercism

Ready to start Linked List?

Sign up to Exercism to learn and master Java with 24 concepts, 145 exercises, and real human mentoring, all for free.