a693673
a693673

Reputation: 167

How to Query a Table of Linked Lists with no List ID?

I have a postgres table that's effectively stores linked lists, but none of the rows have a list identifier. So it's hard to pick out lists from table, other than to "hop" to each node.

create table exchange_history (
    curr_node_id integer,
    prev_node_id integer,   -- This is NULL for the first node in the list.
    description text        -- (Just an example)
)

How would I do the following:

  1. Given a node id of a node in a list, how to select all the nodes of that list?
  2. How to do so if given a description?
  3. How to select them in the same order of the list? Assuming that the node id value does not suggest an order in the list?
  4. Also, if given a description found in a list, how to select all start nodes?

The table contains link lists, so there isn't multiple nodes having the same prev_node_id.

(This isn't homework or an interview question, I genuinely interested in knowing how to do this, but I also have some customer tables that I need to query that are effectively like this.)

Thanks.

Upvotes: 0

Views: 34

Answers (0)

Related Questions