You need some data structure to store ways and their nodes. For example a list of lists, or a hash map that contains a list. There are various possible solutions. None of this has anything to do with OSM. However there are various existing tools for processing OSM files, for example osmium (which is C++, not C). You don't have to reinvent the wheel.

I think that the shortest way is to use o5mreader. The demo file is a perfect template for the purpose. If you have not o5m data files, just pipe the output of osmconvert to your script, it is efficient enough to get a production chain. Osmconvert can also bypass nodes and relations