0

I have a linked list of stops (nodes). Each stop has multiple pieces of information. I want to add in the first items value for 'prevStop' in.

Currently I think something like this will work (if it was a pointer, is it a pointer?) but I'm sure their is a better way to do so?

public void AttachPrevToFirst()
{
    Stops temp = theList.First.Value;
    Stops temp2 = theList.Last.Value;
    temp.prevStop = temp2;
}

P.S. how would I print the data from the linked list?

Thanks in advance for your help :)

Christos
  • 53,228
  • 8
  • 76
  • 108
Zain
  • 1,246
  • 4
  • 15
  • 26

2 Answers2

0

If you want to put the last node in the list before the first node in the list, you could use the AddFirst() method like so:

public void AttachPrevToFirst()
{
    theList.AddFirst(theList.Last);
    theList.RemoveLast();
}

However, perhaps you should use a List<Stops> collection instead if you need to be moving around nodes like this.

mikeyq6
  • 1,138
  • 13
  • 27
0

If you are looking for a way to make your list wrap around (make it circular) so that the last node's next property will reference the first node, and the first node's previous property will reference the last node - then you will need to extend (or work around) the LinkedList class as it is not circular by nature. Here are a couple of neat answers that should help you with this:

Community
  • 1
  • 1
bberak
  • 292
  • 3
  • 11