472,991 Members | 2,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,991 software developers and data experts.

Making a Linked List

I'm trying to create a heirarchical linked list of sorts, and I'm
looking for advice as to how best to set up the arrays. In other
languages, I might use a pointer array to other pointer arrays, but in
PHP, I'm a bit stumped.

What I have is a list of simple, small arrays. I'd like to figure out
the appropriate data structure so that each element knows which
element is it's "parent," and so that each element can have multiple
children. The best way to think of this is like an outline wherein
each fork has a variable number of elements.

The tricky part is that given a specific element, I'd like to be able
to easily list its children's IDs.

I'm storing the individual elements in a MySQL database. So for each
element, I have a field with the data (just text) and a field telling
the unique ID number of the element; and then I suppose I can have an
additional field containing the ID of the parent.

My best guess so far as to how to do this is to read all of the
elements from the database. As they come in one by one, I create an
array for each one to conain a list of its children. When a specific
element comes in, we ask for its parent ID and then add its ID to the
parent's array-of-children.

Will this work, or is there a better way?

Thanks,
M. Katz
Jul 17 '05 #1
2 2333
MK******@onebox.com (M. Katz) wrote in message news:<4a**************************@posting.google. com>...
I'm trying to create a heirarchical linked list of sorts, and I'm
looking for advice as to how best to set up the arrays. In other
languages, I might use a pointer array to other pointer arrays, but in
PHP, I'm a bit stumped.

Will this work, or is there a better way?

Thanks,
M. Katz

Ditto that. Here's a code fragment that I thought should work.
But NO. It has to do with call by value/reference:
------------------------------------------------------------------
<?
class Hierarchy
{
var $name;
var $parent;
var $member;
function show($msg){ print "\n$msg:\t$this->name\n"; var_export($this); }
function Hierarchy($na, $pa)
{
print "function Hierarchy($na, $pa)\n";
$this->name = $na;
$this->parent = $pa;
if ($pa)
{
print "PA is GOOD\n";
$pa->member[$na] = $this;
print_r($pa);
}
}
}
$top = new Hierarchy("TOP", NULL);
$midl = new Hierarchy("Middle", $top);
$botm = new Hierarchy("Bottom A1", $midl);
$botm = new Hierarchy("Bottom B2", $midl);
$midl->show("Bottom: ");
// $top->show("TOP: ");
// $midl->parent->show("Middle 1");
// $midl->parent->parent->show("Bottom");

?>
--------------------------------------------------------------------

the "print_r($pa) shows the Parent and its sub-structure,
avoiding unnecessary recursion. However, after exiting the
"New Hierarchy", the "$midl->show()" for example, has none
of the substructure. I tried the "pass by reference"
notation, both in the calling, and called argument list.
The latter is a syntax error, the former a warning.
Replaceing the "$top" and "$midl" arguements with "$r_top"
and "$r_midl", having "$r_top = & $top; ..." was of no avail.

Help?

-- Marty McGowan
Jul 17 '05 #2
Pass $parent by reference, and assign it to the class variable using =&.

Also beware of the fact that the new operator returns a copy of the newly
created object. So referencing $this in the constructor doesn't work.

Here's some excerpts from my ISO class illustrating this:

class ISO_JolietDirectory extends ISO_Directory {

function ISO_JolietDirectory(&$iso) {
parent::ISO_Directory($iso);
}

function AppendChild($identifier, &$child, $is_dir) {
$unicode_id = ConvertToUCS16($identifier);
$this->children[$unicode_id] =& $child;
if($is_dir) {
$this->has_sub_dir = true;
}
return $unicode_id;
}

/* inherited from ISO_Directory, actually */
function AttachParent(&$parent) {
$this->parent =& $parent;
}
}

The code that add a directory to the tree is like this:

$jo_parent->AppendChild($dir_id, $jo_dir, true);
$jo_dir->AttachParent($jo_parent);

Uzytkownik "Marty McGowan" <mc*****@alum.mit.edu> napisal w wiadomosci
news:39**************************@posting.google.c om...
MK******@onebox.com (M. Katz) wrote in message

news:<4a**************************@posting.google. com>...
I'm trying to create a heirarchical linked list of sorts, and I'm
looking for advice as to how best to set up the arrays. In other
languages, I might use a pointer array to other pointer arrays, but in
PHP, I'm a bit stumped.

Will this work, or is there a better way?

Thanks,
M. Katz

Ditto that. Here's a code fragment that I thought should work.
But NO. It has to do with call by value/reference:
------------------------------------------------------------------
<?
class Hierarchy
{
var $name;
var $parent;
var $member;
function show($msg){ print "\n$msg:\t$this->name\n"; var_export($this); }
function Hierarchy($na, $pa)
{
print "function Hierarchy($na, $pa)\n";
$this->name = $na;
$this->parent = $pa;
if ($pa)
{
print "PA is GOOD\n";
$pa->member[$na] = $this;
print_r($pa);
}
}
}
$top = new Hierarchy("TOP", NULL);
$midl = new Hierarchy("Middle", $top);
$botm = new Hierarchy("Bottom A1", $midl);
$botm = new Hierarchy("Bottom B2", $midl);
$midl->show("Bottom: ");
// $top->show("TOP: ");
// $midl->parent->show("Middle 1");
// $midl->parent->parent->show("Bottom");

?>
--------------------------------------------------------------------

the "print_r($pa) shows the Parent and its sub-structure,
avoiding unnecessary recursion. However, after exiting the
"New Hierarchy", the "$midl->show()" for example, has none
of the substructure. I tried the "pass by reference"
notation, both in the calling, and called argument list.
The latter is a syntax error, the former a warning.
Replaceing the "$top" and "$midl" arguements with "$r_top"
and "$r_midl", having "$r_top = & $top; ..." was of no avail.

Help?

-- Marty McGowan

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
9
by: tiwarinitin.3108 | last post by:
An interactive program that reads 3 list of numbers, which are stored in three seperate files, and creates one sorted list. Each file should contain not more than 15 numbers.
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.