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

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