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

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