X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 85F299FB1D9; Sun, 30 Apr 2006 22:30:12 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 92682-01; Sun, 30 Apr 2006 22:30:05 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- X-Greylist: from auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 1FFBB9FB1C1; Sun, 30 Apr 2006 22:30:05 -0300 (ADT) Received: from tamu-relay.tamu.edu (smtp-relay.tamu.edu [165.91.22.120]) by svr4.postgresql.org (Postfix) with ESMTP id F15E35B7FD3; Mon, 1 May 2006 01:30:02 +0000 (GMT) Received: from coe.tamu.edu (edsun.coe.tamu.edu [165.91.232.6]) by tamu-relay.tamu.edu (8.13.4/8.13.3) with ESMTP id k411TTZu008508; Sun, 30 Apr 2006 20:29:30 -0500 (CDT) (envelope-from bkim@coe.tamu.edu) Received: from coe.tamu.edu (coe.tamu.edu [165.91.232.6]) by coe.tamu.edu (8.11.7p2+Sun/8.11.7) with ESMTP id k411TR423048; Sun, 30 Apr 2006 20:29:27 -0500 (CDT) Date: Sun, 30 Apr 2006 20:29:26 -0500 (CDT) From: "Ben K." To: Ray Madigan Cc: Pgsql-Sql-Owner , "Marc G. Fournier" , pgsql-sql@postgresql.org Subject: Re: Linked List In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Received-SPF: pass (smtp-relay.tamu.edu: 165.91.232.6 is authenticated by a trusted mechanism) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200604/221 X-Sequence-Number: 25030 > I have a table that I created that implements a linked list. I am not an > expert SQL developer and was wondering if there are known ways to traverse > the linked lists. The table contains many linked lists based upon the head > of the list and I need to extract all of the nodes that make up a list. The > lists are simple with a item and a link to the history item so it goes kind > of like: > > 1, 0 > 3, 1 > 7, 3 > 9, 7 > ... I missed "The table contains many linked lists", so wanted to do another try. I guess there should be a better way, but what if you do this? 1) Assuming your table has two columns (n int, p int), do create table tmplist (n int, p int); 2) drop function traverse(integer); create or replace function traverse (integer) returns integer as $$ declare x int; begin x := $1; while x is not null loop select n into x from linkedlist where p = x; insert into tmplist (select * from links where p=x); -- or do any processing end loop; return 1 ; end; $$ language plpgsql; 3) select traverse(0); select * from tmplist; 0 - 1 - 4 - 8 - 12 ... delete from tmplist; select traverse(2); select * from tmplist; 2 - 3 - 5 - 6 - ... (where 0 or 2 is the heads of the linked lists in the table, which you want to traverse) I'd appreciate any insight if there's a better way but somehow it was not possible to return setof int from within while loop whereas it was possible from within a for loop. I didn't find a way to deliver the templist table name as argument. (Somehow there seemed to be a bug(?) there) Regards, Ben K. Developer http://benix.tamu.edu