NanoBob

C# Double linked list swapping

Oct 20th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. public DoubleLink SwapLinkWithNext(DoubleLink link)
  2.         {
  3.             DoubleLink next = link.Next;
  4.             if (next == null)
  5.             {
  6.                 return null;
  7.             }
  8.             DoubleLink previous = link.Previous;
  9.             DoubleLink current = link;
  10.             DoubleLink nextNext = next.Next;
  11.  
  12.             current.Next = nextNext;
  13.             current.Previous = next;
  14.  
  15.             next.Previous = previous;
  16.             next.Next = current;
  17.  
  18.             if (previous == null)
  19.             {
  20.                 First = next;
  21.             } else {
  22.                 previous.Next = next;
  23.             }
  24.             if (nextNext == null)
  25.             {
  26.                 Last = current;
  27.             } else {
  28.                 nextNext.Previous = current;
  29.             }
  30.  
  31.             return next;
  32.         }
  33.  
  34. public void BubbleSort()
  35.         {
  36.             DoubleLink lastEnd = new DoubleLink();
  37.             while (lastEnd != null)
  38.             {
  39.                 Boolean swapped = false;
  40.                 DoubleLink link = First;
  41.                 while (link !=null){
  42.                     if (link == Last || link == lastEnd)
  43.                     {
  44.                         lastEnd = link;
  45.                         break;
  46.                     }
  47.                     if (link.Naw.CompareTo(link.Next.Naw) == 1)
  48.                     {
  49.                         SwapLinkWithNext(link);
  50.                         swapped = true;
  51.                     }
  52.                     link = link.Next;
  53.                 }
  54.                 if (swapped == false )
  55.                 {
  56.                     break;
  57.                 }
  58.             }
  59.         }
Add Comment
Please, Sign In to add comment