fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApplication
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Random rnd = new Random(123);
  11. LinkedList<int> list = new LinkedList<int>();
  12. for (int i = 0; i < 25; i++)
  13. {
  14. list.AddLast(rnd.Next(0, 5));
  15. }
  16. Console.WriteLine("До:");
  17. foreach (var i in list)
  18. {
  19. Console.WriteLine(i);
  20. }
  21. Console.WriteLine();
  22. LinkedListNode<int> node;
  23. var d = 0;
  24. do
  25. {
  26. d = 0;
  27. node = list.First;
  28. while (node != null)
  29. {
  30. int prev = -9999;
  31. int next = -99;
  32. if (node.Previous != null)
  33. {
  34. prev = node.Previous.Value;
  35. }
  36. if (node.Next != null)
  37. {
  38. next = node.Next.Value;
  39. }
  40. var nextNode = node.Next;
  41. if (node.Value == prev || prev == next || node.Value == next)
  42. {
  43. d++;
  44. Console.WriteLine("DELETE: Prev:{0} ,Curr:{1}, Next:{2}", prev, node.Value, next);
  45. //Удаление элемента
  46. list.Remove(node);
  47. }
  48. node = nextNode;
  49. }
  50. }
  51. while (d != 0);
  52.  
  53. Console.WriteLine();
  54.  
  55. Console.WriteLine("После:");
  56.  
  57. foreach (var i in list)
  58. {
  59. Console.WriteLine(i);
  60. }
  61. Console.ReadKey();
  62. }
  63. }
  64. }
Success #stdin #stdout 0.04s 24264KB
stdin
Standard input is empty
stdout
До:
4
3
1
4
4
0
0
2
4
3
0
4
2
4
0
0
1
0
0
1
3
4
2
3
1

DELETE: Prev:1 ,Curr:4, Next:4
DELETE: Prev:4 ,Curr:0, Next:0
DELETE: Prev:4 ,Curr:2, Next:4
DELETE: Prev:4 ,Curr:4, Next:0
DELETE: Prev:4 ,Curr:0, Next:0
DELETE: Prev:0 ,Curr:1, Next:0
DELETE: Prev:0 ,Curr:0, Next:0
DELETE: Prev:0 ,Curr:0, Next:1
DELETE: Prev:0 ,Curr:4, Next:0
DELETE: Prev:0 ,Curr:0, Next:1

После:
4
3
1
4
0
2
4
3
0
1
3
4
2
3
1