Re: Swap nodes in a linked list
Frans Bouma <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <00c301c85cdc$b7d94850$278bd8f0$@nl> |
> No Frans, if anyone is being dense it is I. My problem stems from finding a
> node at a position, and the only means apparent to me is by using extension
> method 'ElementAt' which returns element of type T at a given index, and
> then using intrinsic method 'Find' to find the node containing that element.
Finding a node in a linked list is O(n), it's a linear search, also in
.NET's implementation. So you can also simply loop over the list and do the
swapping manually, it's not less efficient. However having the index, doesn't
that mean that you can simply move to the previous node using that index, and
then place the removed node in front of that? (so first determine previous
node (ElementAt(n-1)) then, addBefore(previousNode)
FB
>
>
> > > Sorry if this is very obvious, but it escapes me now at a time of
> > impending
> > > review. I have a LinkedList<BandLine>, representing a report Band,
> > where
> > > BandLine is one line of text in the band, e.g. one of two detail line
> > types,
> > > or a header line.
> > >
> > > I present my linked list using a DataGridView, and am trying to
> > implement
> > > 'Move Up' and 'Move Down' commands for band lines, which involves
> > swapping
> > > nodes in my linked list. Now getting the BandLine to move is easy,
> > using
> > > ElementAt and an index provided by the grid, but to complete the swap
> > with
> > > AddBefore, I need a LinkedListNode at a specific index. How can I
> > get this?
> >
> > Sorry for being dense, but moveup/down is that the same as
> > moving one
> > position up/down? I.o.w.: move node at post X to pos X-1? Which means
> > in a
> > linked list (if first element is the top element in the list) to move
> > the node
> > at pos X in front of X.Previous
> >
> >
> > FB
>
> ===================================
> This list is hosted by DevelopMentor. http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com