473,487 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2362
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
3070
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
859
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
15097
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
4580
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
15056
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
3927
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
1631
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
8604
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
5756
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
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.