cptfrosty322

Bidirectional cyclic linear list

Oct 6th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. /*ЗАДАЧА: Организовать двунаправленный циклический линейный список с функцией вывода на экран элементов больших первого элемента.
  5.  *
  6.  * Основные операции, осуществляемые с циклическим однонаправленным списком:
  7.  * – вставка элемента;
  8.  * – просмотр
  9.  * – поиск;
  10.  * – удаление элемента.
  11.  */
  12.  
  13. namespace C_SHARP
  14. {
  15.     class Program
  16.     {
  17.      
  18.         /// <summary>
  19.         /// Класс листа
  20.         /// </summary>
  21.         public class ListElems
  22.         {
  23.             public Elem Head; //Главный элемент
  24.             public Elem Current; //Текущий элемент
  25.             public int countElem; //Кол-во элементов
  26.  
  27.             /// <summary>
  28.             /// Вставить элемент
  29.             /// </summary>
  30.             /// <param name="insertElem">Элемент для вставки</param>
  31.             public void Insert()
  32.             {
  33.                 bool attempt = false;
  34.                 Elem newElem = new Elem();
  35.  
  36.                 float value = 0.0f;
  37.                 do
  38.                 {
  39.                     Console.Clear();
  40.                    
  41.                     Console.WriteLine("-ДОБАВЛЕНИЕ-НОВОГО-ЭЛЕМЕНТА-");
  42.  
  43.                     Console.Write("Введите значение нового элемента: ");
  44.                    
  45.                     attempt = float.TryParse(Console.ReadLine(), out value);
  46.  
  47.                     if (!attempt) //Если введено неверное значение
  48.                     {
  49.                         Console.WriteLine("Недопустимое значение! Нажмите любую клавишу, чтобы попробовать ещё раз.");
  50.                         Console.ReadKey();
  51.                     }
  52.  
  53.                 } while (!attempt);
  54.  
  55.                 //Если значение введено верно
  56.                 if (attempt)
  57.                 {
  58.                     countElem++;
  59.                     newElem.value = value;
  60.  
  61.                     if (countElem == 1) // 1 --> 1 --> 1 ...
  62.                     {
  63.                         Head = newElem;
  64.                         newElem.next = Head;
  65.                         newElem.prev = Head;
  66.  
  67.                         Console.WriteLine("Первый элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
  68.                         Console.ReadKey();
  69.                     }
  70.                     else if(countElem == 2) // 1 --> 2 --> 1 ...
  71.                     {
  72.                         Head.next = newElem;
  73.                         Head.prev = newElem;
  74.                         newElem.next = Head;
  75.                         newElem.prev = Head;
  76.  
  77.                         Console.WriteLine("Элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
  78.                         Console.ReadKey();
  79.                     }
  80.                     else if (countElem == 3) // 1 --> 2 --> 3 --> 1 ...
  81.                     {
  82.                         newElem.prev = Head.prev;
  83.                         Head.prev.next = newElem;
  84.                         newElem.next = Head;
  85.                         Head.prev = newElem;
  86.  
  87.                         Console.WriteLine("Элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
  88.                         Console.ReadKey();
  89.                     }
  90.                     else
  91.                     {
  92.                         Head.prev.next = newElem;
  93.                         newElem.next = Head;
  94.                         newElem.prev = Head.prev;
  95.                         Head.prev = newElem;
  96.  
  97.                         Console.WriteLine("Элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
  98.                         Console.ReadKey();
  99.                     }
  100.                 }
  101.             }
  102.  
  103.             /// <summary>
  104.             /// Посмотреть список
  105.             /// </summary>
  106.             public void Show()
  107.             {
  108.                 Console.Clear();
  109.  
  110.                 if (countElem == 0)
  111.                 {
  112.                     Console.WriteLine("Список пуст");
  113.                 }
  114.  
  115.                 else
  116.                 {
  117.                     Console.WriteLine($"Номер элемента - 0; значение - {Head.value}");
  118.                     int counter = 1;
  119.                     for (Current = Head.next; Current != Head; Current = Current.next)
  120.                     {
  121.                         Console.WriteLine($"Номер элемента - {counter}; значение - {Current.value}");
  122.                         counter++;
  123.                     }
  124.                 }
  125.  
  126.                 Console.WriteLine("Для продолжения нажмите любую кнопку");
  127.                 Console.ReadKey();
  128.             }
  129.  
  130.             /// <summary>
  131.             /// Поиск по индексу
  132.             /// </summary>
  133.             public void FindIndex()
  134.             {
  135.                 Console.Clear();
  136.                 if (countElem == 0)
  137.                 {
  138.                     Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
  139.                     Console.ReadKey();
  140.                     return;
  141.                 }
  142.  
  143.                 int indexSearch = -1;
  144.                 bool attempt = false;
  145.  
  146.                 do
  147.                 {
  148.                     Console.Clear();
  149.                     Console.WriteLine("Введите номер элемента, который необходимо найти");
  150.                     Console.Write("Найти элемент: ");
  151.                     attempt = int.TryParse(Console.ReadLine(), out indexSearch);
  152.  
  153.                     if (!attempt)
  154.                     {
  155.                         Console.WriteLine("Введите целое число. Нажмите любую кнопку, чтобы продолжить.");
  156.                         Console.ReadKey();
  157.                     }
  158.  
  159.                 } while (!attempt);
  160.  
  161.                 if (indexSearch > countElem) {
  162.                     Console.WriteLine("Такого элемента не существует. Нажмите любую кнопку, чтобы продолжить.");
  163.                     Console.ReadKey();
  164.                     return;
  165.                 }
  166.  
  167.                 if (indexSearch == 0)
  168.                     Console.WriteLine($"Номер элемента - 0; значение - {Head.value}");
  169.                 else
  170.                 {
  171.                     int index = 1;
  172.                     for (Current = Head.next; Current != Head; Current = Current.next)
  173.                     {
  174.                         index++;
  175.                         if(index == indexSearch)
  176.                         {
  177.                             Console.WriteLine("-ИНФОРМАЦИЯ-ПО-НАЙДЕНОМУ-ЭЛЕМЕНТУ-");
  178.                             Console.WriteLine($"Номер элемента - {index}; значение - {Current.value}");
  179.                             break;
  180.                         }
  181.                     }
  182.                 }
  183.  
  184.                 Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
  185.                 Console.ReadKey();
  186.             }
  187.  
  188.             /// <summary>
  189.             /// Найти по значению
  190.             /// </summary>
  191.             public void FindValue()
  192.             {
  193.                 Console.Clear();
  194.                 if (countElem == 0)
  195.                 {
  196.                     Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
  197.                     Console.ReadKey();
  198.                     return;
  199.                 }
  200.  
  201.                 int valueSearch = -1;
  202.                 bool attempt = false;
  203.  
  204.                 do
  205.                 {
  206.                     Console.Clear();
  207.                     Console.WriteLine("Введите значение, которое необходимо найти");
  208.                     Console.Write("Найти значение: ");
  209.                     attempt = int.TryParse(Console.ReadLine(), out valueSearch);
  210.  
  211.                     if (!attempt)
  212.                     {
  213.                         Console.WriteLine("Введите целое число. Нажмите любую кнопку, чтобы продолжить.");
  214.                         Console.ReadKey();
  215.                     }
  216.  
  217.                 } while (!attempt);
  218.  
  219.                 int countFindElem = 0;
  220.                 int index = 0;
  221.  
  222.                 Console.WriteLine("-ИНФОРМАЦИЯ-ПО-НАЙДЕНЫМ-ЗНАЧЕНИЯМ-");
  223.  
  224.                 if (Head.value == valueSearch)
  225.                 {
  226.                     Console.WriteLine($"Номер элемента - 0; значение - {Head.value}");
  227.                     countFindElem++;
  228.                 }
  229.  
  230.                 for (Current = Head.next; Current != Head; Current = Current.next)
  231.                 {
  232.                     index++;
  233.                     if (Current.value == valueSearch)
  234.                     {
  235.                         Console.WriteLine($"Номер элемента - {index}; значение - {Current.value}");
  236.                         countFindElem++;
  237.                     }
  238.                 }
  239.  
  240.                 Console.WriteLine($"Найдено элементов - {countFindElem} шт") ;
  241.                 Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
  242.                 Console.ReadKey();
  243.             }
  244.  
  245.             /// <summary>
  246.             /// Удаление элемента
  247.             /// </summary>
  248.             public void Delete()
  249.             {
  250.                 Console.Clear();
  251.                 if (countElem == 0)
  252.                 {
  253.                     Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
  254.                     Console.ReadKey();
  255.                     return;
  256.                 }
  257.                 Console.WriteLine("Введите номер элемента, который необходимо удалить");
  258.  
  259.                 bool attempt = false;
  260.                 int indexSearch = -1;
  261.  
  262.                 do
  263.                 {
  264.                     Console.Clear();
  265.                     Console.WriteLine("Введите элемент, которое необходимо удалить");
  266.                     Console.Write("Удалить элемент: ");
  267.                     attempt = int.TryParse(Console.ReadLine(), out indexSearch);
  268.  
  269.                     if (!attempt)
  270.                     {
  271.                         Console.WriteLine("Введите целое число. Нажмите любую кнопку, чтобы продолжить.");
  272.                         Console.ReadKey();
  273.                     }
  274.  
  275.                 } while (!attempt);
  276.  
  277.                 if (indexSearch > countElem)
  278.                 {
  279.                     Console.WriteLine("Такого элемента не существует. Нажмите любую кнопку, чтобы продолжить.");
  280.                     Console.ReadKey();
  281.                     return;
  282.                 }
  283.  
  284.                 if (indexSearch == 0)
  285.                 {
  286.                     Console.WriteLine($"УДАЛЁН! Номер элемента - 0; значение - {Head.value}");
  287.  
  288.                     countElem--;
  289.  
  290.                     if (countElem == 0)
  291.                     {
  292.                         Head = null;
  293.                     }
  294.                     else
  295.                     {
  296.  
  297.                         Head.next.prev = Head.prev;
  298.                         Head.prev.next = Head.next;
  299.  
  300.                         Head = Head.next;
  301.                     }
  302.  
  303.                 }
  304.                 else
  305.                 {
  306.                     int index = 1;
  307.                     for (Current = Head.next; Current != Head; Current = Current.next)
  308.                     {
  309.                         index++;
  310.                         if (index == indexSearch)
  311.                         {
  312.                             Console.WriteLine($"УДАЛЁН! Номер элемента - {index}; значение - {Current.value}");
  313.  
  314.                             Current.next.prev = Current.prev;
  315.                             Current.prev.next = Current.next;
  316.  
  317.                             break;
  318.                         }
  319.                     }
  320.                 }
  321.  
  322.                 Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
  323.                 Console.ReadKey();
  324.             }
  325.  
  326.         }
  327.  
  328.         public class Elem
  329.         {
  330.             public Elem next;
  331.             public Elem prev;
  332.             public float value;
  333.         }
  334.  
  335.         static void Main(string[] args)
  336.         {
  337.             ListElems list = new ListElems();
  338.             while (true)
  339.             {
  340.                 bool attempt = false;
  341.                 int choice = 0;
  342.                 do
  343.                 {
  344.                     choice = 0;
  345.                     Console.Clear();
  346.                     Console.WriteLine("Выберите действие: \n1-Информация о листе \n2-Добавить элемент\n3-Посмотреть список\n" +
  347.                         "4-Найти значение по элементу\n5-Найти элементы по значению\n6-Удалить элемент\n7-Отобразить значения большие первого \n0-Завершить программу");
  348.                     Console.Write("Ваш выбор - ");
  349.                     attempt = int.TryParse(Console.ReadLine(), out choice);
  350.  
  351.                     if (!attempt)
  352.                     {
  353.                         Console.WriteLine("Ошибка. Введите целое число! Чтобы продолжить, нажмите любую кнопку.");
  354.                         Console.ReadKey();
  355.                     }
  356.  
  357.                 } while (!attempt);
  358.  
  359.                 switch (choice)
  360.                 {
  361.                     case 1:
  362.                         ShowInfoList(ref list);
  363.                         break;
  364.                     case 2:
  365.                         list.Insert();
  366.                         break;
  367.                     case 3:
  368.                         list.Show();
  369.                         break;
  370.                     case 4:
  371.                         list.FindIndex();
  372.                         break;
  373.                     case 5:
  374.                         list.FindValue();
  375.                         break;
  376.                     case 6:
  377.                         list.Delete();
  378.                         break;
  379.                     case 7:
  380.                         ShowGreatImportance(list);
  381.                         break;
  382.                     case 0:
  383.                         break;
  384.                     default:
  385.                         Console.WriteLine("Ошибка. Такого выбора - нет! Чтобы продолжить, нажмите любую кнопку.");
  386.                         Console.ReadKey();
  387.                         break;
  388.                 }
  389.  
  390.                 if(choice == 0) //Если выбор был завершение программы
  391.                 {
  392.                     Console.WriteLine("Работа с программой была завершена! Чтобы продолжить, нажмите любую кнопку.");
  393.                     Console.ReadKey();
  394.                     break;
  395.                 }
  396.                
  397.             }
  398.         }
  399.  
  400.         /// <summary>
  401.         /// Отобразить информацию о листе
  402.         /// </summary>
  403.         static void ShowInfoList(ref ListElems list)
  404.         {
  405.             char choice = ' '; //Выбор
  406.  
  407.             do
  408.             {
  409.                 Console.Clear(); //Очистить консоль
  410.                 Console.WriteLine($"В двунаправленном циклическом списке {list.countElem} элементов. Вы хотите добавить? Y-да N-нет");
  411.  
  412.                 bool attempt = char.TryParse(Console.ReadLine(), out choice);//Если значение нельзя преобразовать в char, то выдаст false
  413.                
  414.                 if ((choice != 'Y' && choice != 'N' && choice != 'y' && choice != 'n') || !attempt)
  415.                 {
  416.                     Console.WriteLine("Введите, пожалуйста, правильное значение. Чтобы продолжить, нажмите любую кнопку.");
  417.                     Console.ReadKey();
  418.                 }
  419.  
  420.             } while (choice != 'Y' && choice != 'N' && choice != 'y' && choice != 'n');
  421.  
  422.             if(choice == 'Y' || choice == 'y')
  423.             {
  424.                 list.Insert();
  425.             }
  426.            
  427.         }
  428.         /// <summary>
  429.         /// Отобразить большие значения
  430.         /// </summary>
  431.         static void ShowGreatImportance(ListElems elems)
  432.         {
  433.             Console.Clear();
  434.             if (elems.countElem == 0)
  435.             {
  436.                 Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
  437.                 Console.ReadKey();
  438.                 return;
  439.             }
  440.  
  441.             int index = 0;
  442.             int countFindElem = 0;
  443.  
  444.             Console.WriteLine("-ЗНАЧЕНИЯ-КОТОРЫЕ-БОЛЬШЕ-ПЕРВОГО");
  445.             for (elems.Current = elems.Head.next; elems.Current != elems.Head; elems.Current = elems.Current.next)
  446.             {
  447.                 index++;
  448.                 if (elems.Current.value > elems.Head.value)
  449.                 {
  450.                     Console.WriteLine($"Номер элемента - {index}; значение - {elems.Current.value}");
  451.                     countFindElem++;
  452.                 }
  453.             }
  454.  
  455.             if(countFindElem == 0)
  456.             {
  457.                 Console.WriteLine("Таких значений нет!");
  458.             }
  459.             else
  460.             {
  461.                 Console.WriteLine($"Значений больше первого - {countFindElem} шт.");
  462.             }
  463.  
  464.             Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
  465.             Console.ReadKey();
  466.         }
  467.     }
  468. }
  469.  
Advertisement
Add Comment
Please, Sign In to add comment