473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 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 1792
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...
0
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,...

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.