Yes, you can freely switch items in the build-queue.
To give you an example: in your case, add the unit to the queue, then click on the icon of the unit within the queue and drag it in front of the icon of the building you're constructing. Your city will now train the unit first. Once that is done, it will continue to construct the building.