Homework / school projects are always complex, adding subtle things to requirements that can make someone's brain melt. Do you have any requirements for including spaces in the queue?
Personally, I would not do this if it was not explicitly required: it seems easier to present your cars as “Car”, “Space pairs” (you can define a pair as a structure, assuming that you are allowed to use structures), where space is a numerical value, representing the space for the next car in the car. Then, to compress, you only need to look at the list items: when you find Space > 0 , do Space--; return; Space--; return; , and all other cars will already be "advanced", as they save space with those in front of them. To conclude, make sure that you select Space points for each car after (if the brake light is on the right and cars go to the left) or earlier (the brake light on the left and cars coming to the right) the car itself, and there you go. Also note that the Space first car is the distance to the brake light itself, since there is no car in front of it.
If you add a pointer to the next car in the struct (and a null pointer to the last car), you already have a linked list: save the "global" variable pointing to the first car (or null for an empty queue). Since Java does not directly support pointers, turn the structure into a class and use "object references" (which are the same as pointers for all purposes other than C'ish pointer arithmetic), and there you go: only one class built from scratch. The only thing you need to touch on Java libraries is standard IO and maybe a little string manipulation, which is an inherent need for input and output input (some colleges have their own IO libraries, but it doesn't have much values here). To go through the queue, you would do something like this (assuming the class is called "Node", which is a fairly common and explicit name for the fields):
for(Node pos = First; pos != null; pos = pos.Next) { }
To add new nodes, you may have to take turns to figure out how far from the last a new car will appear; when you do, save the link from the last node and make its “next” breakpoint a new node:
Node last = First; int distance = 0; for(Node pos = First; pos != null; pos=pos.Next) { distance += pos.Space; last = pos; } last.Next = new Node(the_letter_for_this_car, MaxDistance-distance, null);
Of course, configure the constructor for everything you have.
Given that this is a college project, we’ll look at some details: the compact time of the process becomes O(n) , and the memory usage is O(0) (the process itself does not need any “local” variables, other than, possibly, a pointer to moving the collection, which does not depend on the length of the queue.) In addition, the use of memory for the queue itself will be guaranteed to be less or equal to the fact that it will represent spaces as elements (it only gets to be equal in the worst case scenario when enough cars are stuck in red light). Thus, if the requirements do not contain something that is incompatible with this approach, I would expect that it will be what your teachers want: it is reasoned, effective and corresponds to what you were asked about.
Hope this helps.