Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6095669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:50:16+00:00 2026-05-23T12:50:16+00:00

I have a small problem and I would like to get your opinion. I’m

  • 0

I have a small problem and I would like to get your opinion.

I’m dealing with documents than can reference other documents. Starting from any document, I need to get the id of all the documents this document references. The problem is that the circular references are allowed so if A ref B ref C, again C can ref A and I get in the loop.
How can I solve this problem in C#?

An small example:

Let suppose that this is a class that represents a document:

public class Document
{
    public Document(int id)
    {
        this.ID = id;
    }

    private int m_ID;

    public int ID
    {
        get { return m_ID; }
        set { m_ID = value; }
    }

    private List<Document> m_Children = new List<Document>();

    public List<Document> Children
    {
        get { return m_Children; }
        set { m_Children = value; }
    }

    private List<Document> m_Parent = new List<Document>();

    public List<Document> Parent
    {
        get { return m_Parent; }
        set { m_Parent = value; }
    }

    public Document AddChild(Document child)
    {
        child.Parent.Add(this);
        this.Children.Add(child);

        return child;
    }

    public Document AddChild(int child)
    {
        Document d = new Document(child);

        return AddChild(d);
    }
}

Now let’s create a Document class that has some references:

public static Document CreateReferences()
{
    Document d = new Document(1);

    Document temp = d.AddChild(2);

    for (int i = 3; i < 6; i++)
    {
        temp = temp.AddChild(i);
    }

    temp.AddChild(d);

    return d;
}

Now I need to implement a method in Document class like

public List<int> GetReferencedDocuments()
{    }

What is the best way to do that? Any specific algorithm can be implemented?

Any suggestion is well accepted!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:50:17+00:00Added an answer on May 23, 2026 at 12:50 pm

    Any tree-traversal algorithm would be fine.


    As well as a list of docs you’re going to build up, maintain a queue of documents you’ve yet to check, add the first document to that list.

    Then, while the queue isn’t empty, get the next doc, if it’s not already in your list, then add it, and add all referenced docs to your queue.


    List<Document> FoundDocs = new List<Documents();
    Queue<Document> DocsToSearch = new Queue<Document>();
    
    DocsToSearch.Enqueue(StartDoc);
    
    while(DocsToSearch.Count != 0)
    {
        Document Doc = DocsToSearch.Dequeue();
        if(!FoundDocs.Contains(Doc))
        {
            FoundDocs.Add(Doc);
            foreach(var ChildDoc in Doc.Children)
            {
                DocsToSearch.Enqueue(ChildDoc);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small problem with url rewriting on apache. I would like it
I have a small problem, I would like a div to be displayed when
have small problem, and would very much appreciate help :) I should convert byte
I have a small problem in my code. I'm using fetch_assoc to get data
I would like your suggestion about this problem... To make it simple, I'll consider
I have this idea for a small-medium web application that I would like to
I have the following problem: I would like to visualize a discrete and a
I would like to get your advices about the design of a header of
I have a small problem with Smarty... I have two different template files in
I have a small problem where I want to find the next active item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.