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 9093193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:01:51+00:00 2026-06-16T23:01:51+00:00

I have data stored in an XML document that describes a linked list; all

  • 0

I have data stored in an XML document that describes a linked list; all nodes except one follow another, so the data looks something like this:

<cars>
    <car id="9" follows="34" />
    <car id="12" follows="20" />
    <car id="20" follows="9" />
    <car id="29" follows="30" />
    <car id="30" />
    <car id="34" follows="29" />
</cars>

… to give an ordering of 30, 29, 34, 9, 20, 12. I’m using .NET’s LinkedList class to construct a linked list to reflect this data, but it’s awkward to construct because the values are out of sequence. What I really want to do is assume that the data is valid – there is exactly one first value, and all others have “follows” values that follow one other node in the list. Code like this would be good (FindFirstForwards is a custom extension method I wrote to find the first linked list entry for which the given lambda returns true):

LinkedList<CarInstance> orderedCars = new LinkedList<CarInstance>();
XPathNodeIterator xmlIterator = _nav.Select("/dflt:cars/dflt:car", _namespaceResolver);
while (xmlIterator.MoveNext()) {
    if (!(xmlIterator.Current.Select("@follows").Count > 0)) {
        orderedCars.AddFirst(new CarInstance {
            CarId = int.Parse(xmlIterator.Current.GetAttribute("id", _defaultNamespace))
        });
    }
    else {
        orderedCars.AddAfter(orderedCars.FindFirstForwards(car => car.CarId == int.Parse(xmlIterator.Current.GetAttribute("follows", _defaultNamespace))), new CarInstance {
            CarId = int.Parse(xmlIterator.Current.GetAttribute("id", _defaultNamespace))
        });
    }
}

The trouble is, if the car that this one follows has not yet been added to orderedCars, an exception is thrown because FindFirstForwards didn’t find a car with the “follows” ID. What I really want to do is say “add this to the linked list, assume it will follow some future entry with a certain ID even though that entry hasn’t yet been added, and carry on.” Then at the end, check the integrity of the linked list to make sure each node points to another, and that there is one head node.

Is there a concise way of doing this? If not, what would be the most efficient (and preferably, code-concise) way of converting this XML into an in-memory linked list?

  • 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-06-16T23:01:53+00:00Added an answer on June 16, 2026 at 11:01 pm

    I would do this in a two-step fashion:

    1. Load all the cars into a dictionary, keyed by the id of the car, without considering their ordering
    2. Link up all the cars by looping through them (in the dictionary order, doesn’t matter), and finding the following car through the dictionary which should be a O(1) operation at this point.

    If you cannot construct a car object without linking the next car into it at that point, ie. the “follows” part (or all) of the car object is immutable, I would create a temporary class, or even store the XML nodes in the dictionary, and then construct the car objects once you have their ordering.

    Also, even though you only have one link from one object to another, you could also consider a Topological Sort for this, but note that fast implementations of this algorithm typically uses dictionaries as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains an xml document stored as a clob. TabA
I have 2 simple webservice methods that query an XML document (stored in HTTP
I have a Stored Procedure that takes an XML document as a parameter similar
I have an xml document that is stored in sql server 2005 that I
I have a stored procedure which takes an XML parameter and inserts the data
I have a table from a vendor application that stores some xml data into
I have to read/write an XML file that stores its data as bit masked
I have some data that is stored in a TIMESTAMP(6) WITH TIMEZONE column in
I have a typed xml document stored as text. So I use CONVERT the
I have some XML that stores column information and row data from a table

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.