472,975 Members | 1,464 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,975 software developers and data experts.

List

Hello,

What is the best way to create a structure (List) with parent and
child? For example:

Home
Contact
Products
Books
Magazines
Sport
Travel

Basically I want to store a menu structure into an object and then
loop through it and build the HTML.

How can I do this?

Thanks,
Miguel
Sep 10 '08 #1
3 1768
shapper <md*****@gmail.comwrote:
What is the best way to create a structure (List) with parent and
child? For example:

Home
Contact
Products
Books
Magazines
Sport
Travel

Basically I want to store a menu structure into an object and then
loop through it and build the HTML.

How can I do this?
Firstly, if you're researching this you'll want to get the right name -
this isn't a list, it's a tree.

Now, as for implementing it, assuming you want the same content type at
all locations, you could do something along the lines of:

public class TreeNode<T>
{
T content;
TreeNode<Tparent; // Will be null for the root
IList<TreeNode<T>children;
}

What API you put on top of that is up to you, of course.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 10 '08 #2
On Sep 10, 2:26*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
shapper <mdmo...@gmail.comwrote:
What is the best way to create a structure (List) with parent and
child? For example:
Home
* Contact
Products
* *Books
* *Magazines
* * * *Sport
* * * *Travel
Basically I want to store a menu structure into an object and then
loop through it and build the HTML.
How can I do this?

Firstly, if you're researching this you'll want to get the right name -
this isn't a list, it's a tree.

Now, as for implementing it, assuming you want the same content type at
all locations, you could do something along the lines of:

public class TreeNode<T>
{
* * T content;
* * TreeNode<Tparent; // Will be null for the root
* * IList<TreeNode<T>children;

}

What API you put on top of that is up to you, of course.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Basically, I need to render in my MVC project the following:

<ul id="Menu" class="Menu-Smog">
<li><%= Html.ActionLink("home", "Index", "Home") %></li>
<li>
<a href="#CMS">Cms</a>
<ul>
<li><%= Html.ActionLink("Create Post", "Create", "Post") %></li>
<li><%= Html.ActionLink("Delete File", "Delete", "File") %></li>
</ul>
</li>
<li><%= Html.ActionLink("Contact", "Contact", "Home") %></li>
</ul>

So basically I each Tree Node should render as follows:

1. Is Not Active
<li><a href="#CMS">Cms</a></li>

2. Is Not Active but has child items
<li>
<a href="#CMS">Cms</a>
<ul>
### Child Items Go Here ###
</ul>
</li>

3. Is Active and does not have child elements
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>

4. Is Active and has child elements:
<li>
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>
<ul>
### Child Items Go Here ###
</ul>
</li>

So each Tree Node should have the following properties: ID, IsActive,
Name, Action and Controller

And the ability to check if it contains child items and if there is
loop through their child items applying the same rules ...

My problem is more with how the object should be and how to loop
through the Tree and follow this rules ...

Thanks,
Miguel
Sep 10 '08 #3
shapper <md*****@gmail.comwrote:

<snip>
Basically, I need to render in my MVC project the following:

<ul id="Menu" class="Menu-Smog">
<li><%= Html.ActionLink("home", "Index", "Home") %></li>
<li>
<a href="#CMS">Cms</a>
<ul>
<li><%= Html.ActionLink("Create Post", "Create", "Post") %></li>
<li><%= Html.ActionLink("Delete File", "Delete", "File") %></li>
</ul>
</li>
<li><%= Html.ActionLink("Contact", "Contact", "Home") %></li>
</ul>

So basically I each Tree Node should render as follows:

1. Is Not Active
<li><a href="#CMS">Cms</a></li>

2. Is Not Active but has child items
<li>
<a href="#CMS">Cms</a>
<ul>
### Child Items Go Here ###
</ul>
</li>

3. Is Active and does not have child elements
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>

4. Is Active and has child elements:
<li>
<li><%= Html.ActionLink( { Link Name }, { Action },
{ Controller } ) %></li>
<ul>
### Child Items Go Here ###
</ul>
</li>

So each Tree Node should have the following properties: ID, IsActive,
Name, Action and Controller
I wouldn't put that in the TreeNode itself - I'd put that in the
Contents property (or Value, or whatever you want to call it). That way
you can build a generic TreeNode class which will be useful next time
you want a tree too. Then have a separate class for the content, and
*that* will have the ID, IsActive, Name, Action and Controller
properties.
And the ability to check if it contains child items and if there is
loop through their child items applying the same rules ...
That's pretty easy.
My problem is more with how the object should be and how to loop
through the Tree and follow this rules ...
Well, you can easily iterate through the direct children just by using
the List<T>. To iterate through *all* descendants, you probably want
something like this:

public IEnumerable<TGetDescendants()
{
yield return contents;
foreach (TreeNode<Tnode in children)
{
foreach (T element in node.GetDescendants())
{
yield return element;
}
}
}

Note that this isn't terribly efficient - but it can be optimised when
you've got everything actually working.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 10 '08 #4

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

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
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...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
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...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
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=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
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...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
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...
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...

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.