Why exactly do we need a “Circular Linked List” (singly or doubly) data structure?

A simple example is keeping track of whose turn it is in a multi-player board game. Put all the players in a circular linked list. After a player takes his turn, advance to the next player in the list. This will cause the program to cycle indefinitely among the players. To traverse a circular linked … Read more

Relative performance of std::vector vs. std::list vs. std::slist?

As usual the best answer to performance questions is to profile both implementations for your use case and see which is faster. In general if you have insertions into the data-structure (other than at the end) then vector may be slower, otherwise in most cases vector is expected to perform better than list if only … Read more

Yield Return In Java

You can return an anonymous implementation of Iterable. The effects are pretty pretty similar, just that this is a lot more verbose. public Iterable<String> getStuff() { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new Iterator<String>() { @Override public boolean hasNext() { // TODO code to check next } @Override public String next() … Read more

Array-Based vs List-Based Stacks and Queues

There are multiple different ways to implement queues and stacks with linked lists and arrays, and I’m not sure which ones you’re looking for. Before analyzing any of these structures, though, let’s review some important runtime considerations for the above data structures. In a singly-linked list with just a head pointer, the cost to prepend … Read more