473,985 Members | 7,140 Online
Bytes | Software Development & Data Engineering Community
+ 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 2391
MK******@onebox .com (M. Katz) wrote in message news:<4a******* *************** ****@posting.go ogle.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($thi s); }
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("Midd le", $top);
$botm = new Hierarchy("Bott om A1", $midl);
$botm = new Hierarchy("Bott om B2", $midl);
$midl->show("Bottom : ");
// $top->show("TOP: ");
// $midl->parent->show("Middle 1");
// $midl->parent->parent->show("Bottom") ;

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

the "print_r($p a) 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_JolietDirec tory extends ISO_Directory {

function ISO_JolietDirec tory(&$iso) {
parent::ISO_Dir ectory($iso);
}

function AppendChild($id entifier, &$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($d ir_id, $jo_dir, true);
$jo_dir->AttachParent($ jo_parent);

Uzytkownik "Marty McGowan" <mc*****@alum.m it.edu> napisal w wiadomosci
news:39******** *************** ***@posting.goo gle.com...
MK******@onebox .com (M. Katz) wrote in message

news:<4a******* *************** ****@posting.go ogle.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($thi s); }
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("Midd le", $top);
$botm = new Hierarchy("Bott om A1", $midl);
$botm = new Hierarchy("Bott om B2", $midl);
$midl->show("Bottom : ");
// $top->show("TOP: ");
// $midl->parent->show("Middle 1");
// $midl->parent->parent->show("Bottom") ;

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

the "print_r($p a) 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
3119
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 LOCATE subroutine that returns a node???? Any help would be appreciated. Thanks
10
15170
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
6
4612
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 potential pitfalls I'd be extremely grateful. Cheers Steve
12
15128
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 should be kept for references to previous element of list. Here is my solution below: struct node {
12
3977
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 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
9
1664
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
8653
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
7
5790
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
10192
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11865
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11457
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11632
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8495
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7644
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6594
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5186
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 we have to send another system

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.