Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- /*ЗАДАЧА: Организовать двунаправленный циклический линейный список с функцией вывода на экран элементов больших первого элемента.
- *
- * Основные операции, осуществляемые с циклическим однонаправленным списком:
- * – вставка элемента;
- * – просмотр
- * – поиск;
- * – удаление элемента.
- */
- namespace C_SHARP
- {
- class Program
- {
- /// <summary>
- /// Класс листа
- /// </summary>
- public class ListElems
- {
- public Elem Head; //Главный элемент
- public Elem Current; //Текущий элемент
- public int countElem; //Кол-во элементов
- /// <summary>
- /// Вставить элемент
- /// </summary>
- /// <param name="insertElem">Элемент для вставки</param>
- public void Insert()
- {
- bool attempt = false;
- Elem newElem = new Elem();
- float value = 0.0f;
- do
- {
- Console.Clear();
- Console.WriteLine("-ДОБАВЛЕНИЕ-НОВОГО-ЭЛЕМЕНТА-");
- Console.Write("Введите значение нового элемента: ");
- attempt = float.TryParse(Console.ReadLine(), out value);
- if (!attempt) //Если введено неверное значение
- {
- Console.WriteLine("Недопустимое значение! Нажмите любую клавишу, чтобы попробовать ещё раз.");
- Console.ReadKey();
- }
- } while (!attempt);
- //Если значение введено верно
- if (attempt)
- {
- countElem++;
- newElem.value = value;
- if (countElem == 1) // 1 --> 1 --> 1 ...
- {
- Head = newElem;
- newElem.next = Head;
- newElem.prev = Head;
- Console.WriteLine("Первый элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- }
- else if(countElem == 2) // 1 --> 2 --> 1 ...
- {
- Head.next = newElem;
- Head.prev = newElem;
- newElem.next = Head;
- newElem.prev = Head;
- Console.WriteLine("Элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- }
- else if (countElem == 3) // 1 --> 2 --> 3 --> 1 ...
- {
- newElem.prev = Head.prev;
- Head.prev.next = newElem;
- newElem.next = Head;
- Head.prev = newElem;
- Console.WriteLine("Элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- }
- else
- {
- Head.prev.next = newElem;
- newElem.next = Head;
- newElem.prev = Head.prev;
- Head.prev = newElem;
- Console.WriteLine("Элемент добавлен. Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- }
- }
- }
- /// <summary>
- /// Посмотреть список
- /// </summary>
- public void Show()
- {
- Console.Clear();
- if (countElem == 0)
- {
- Console.WriteLine("Список пуст");
- }
- else
- {
- Console.WriteLine($"Номер элемента - 0; значение - {Head.value}");
- int counter = 1;
- for (Current = Head.next; Current != Head; Current = Current.next)
- {
- Console.WriteLine($"Номер элемента - {counter}; значение - {Current.value}");
- counter++;
- }
- }
- Console.WriteLine("Для продолжения нажмите любую кнопку");
- Console.ReadKey();
- }
- /// <summary>
- /// Поиск по индексу
- /// </summary>
- public void FindIndex()
- {
- Console.Clear();
- if (countElem == 0)
- {
- Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- return;
- }
- int indexSearch = -1;
- bool attempt = false;
- do
- {
- Console.Clear();
- Console.WriteLine("Введите номер элемента, который необходимо найти");
- Console.Write("Найти элемент: ");
- attempt = int.TryParse(Console.ReadLine(), out indexSearch);
- if (!attempt)
- {
- Console.WriteLine("Введите целое число. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- }
- } while (!attempt);
- if (indexSearch > countElem) {
- Console.WriteLine("Такого элемента не существует. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- return;
- }
- if (indexSearch == 0)
- Console.WriteLine($"Номер элемента - 0; значение - {Head.value}");
- else
- {
- int index = 1;
- for (Current = Head.next; Current != Head; Current = Current.next)
- {
- index++;
- if(index == indexSearch)
- {
- Console.WriteLine("-ИНФОРМАЦИЯ-ПО-НАЙДЕНОМУ-ЭЛЕМЕНТУ-");
- Console.WriteLine($"Номер элемента - {index}; значение - {Current.value}");
- break;
- }
- }
- }
- Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- }
- /// <summary>
- /// Найти по значению
- /// </summary>
- public void FindValue()
- {
- Console.Clear();
- if (countElem == 0)
- {
- Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- return;
- }
- int valueSearch = -1;
- bool attempt = false;
- do
- {
- Console.Clear();
- Console.WriteLine("Введите значение, которое необходимо найти");
- Console.Write("Найти значение: ");
- attempt = int.TryParse(Console.ReadLine(), out valueSearch);
- if (!attempt)
- {
- Console.WriteLine("Введите целое число. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- }
- } while (!attempt);
- int countFindElem = 0;
- int index = 0;
- Console.WriteLine("-ИНФОРМАЦИЯ-ПО-НАЙДЕНЫМ-ЗНАЧЕНИЯМ-");
- if (Head.value == valueSearch)
- {
- Console.WriteLine($"Номер элемента - 0; значение - {Head.value}");
- countFindElem++;
- }
- for (Current = Head.next; Current != Head; Current = Current.next)
- {
- index++;
- if (Current.value == valueSearch)
- {
- Console.WriteLine($"Номер элемента - {index}; значение - {Current.value}");
- countFindElem++;
- }
- }
- Console.WriteLine($"Найдено элементов - {countFindElem} шт") ;
- Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- }
- /// <summary>
- /// Удаление элемента
- /// </summary>
- public void Delete()
- {
- Console.Clear();
- if (countElem == 0)
- {
- Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- return;
- }
- Console.WriteLine("Введите номер элемента, который необходимо удалить");
- bool attempt = false;
- int indexSearch = -1;
- do
- {
- Console.Clear();
- Console.WriteLine("Введите элемент, которое необходимо удалить");
- Console.Write("Удалить элемент: ");
- attempt = int.TryParse(Console.ReadLine(), out indexSearch);
- if (!attempt)
- {
- Console.WriteLine("Введите целое число. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- }
- } while (!attempt);
- if (indexSearch > countElem)
- {
- Console.WriteLine("Такого элемента не существует. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- return;
- }
- if (indexSearch == 0)
- {
- Console.WriteLine($"УДАЛЁН! Номер элемента - 0; значение - {Head.value}");
- countElem--;
- if (countElem == 0)
- {
- Head = null;
- }
- else
- {
- Head.next.prev = Head.prev;
- Head.prev.next = Head.next;
- Head = Head.next;
- }
- }
- else
- {
- int index = 1;
- for (Current = Head.next; Current != Head; Current = Current.next)
- {
- index++;
- if (index == indexSearch)
- {
- Console.WriteLine($"УДАЛЁН! Номер элемента - {index}; значение - {Current.value}");
- Current.next.prev = Current.prev;
- Current.prev.next = Current.next;
- break;
- }
- }
- }
- Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- }
- }
- public class Elem
- {
- public Elem next;
- public Elem prev;
- public float value;
- }
- static void Main(string[] args)
- {
- ListElems list = new ListElems();
- while (true)
- {
- bool attempt = false;
- int choice = 0;
- do
- {
- choice = 0;
- Console.Clear();
- Console.WriteLine("Выберите действие: \n1-Информация о листе \n2-Добавить элемент\n3-Посмотреть список\n" +
- "4-Найти значение по элементу\n5-Найти элементы по значению\n6-Удалить элемент\n7-Отобразить значения большие первого \n0-Завершить программу");
- Console.Write("Ваш выбор - ");
- attempt = int.TryParse(Console.ReadLine(), out choice);
- if (!attempt)
- {
- Console.WriteLine("Ошибка. Введите целое число! Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- }
- } while (!attempt);
- switch (choice)
- {
- case 1:
- ShowInfoList(ref list);
- break;
- case 2:
- list.Insert();
- break;
- case 3:
- list.Show();
- break;
- case 4:
- list.FindIndex();
- break;
- case 5:
- list.FindValue();
- break;
- case 6:
- list.Delete();
- break;
- case 7:
- ShowGreatImportance(list);
- break;
- case 0:
- break;
- default:
- Console.WriteLine("Ошибка. Такого выбора - нет! Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- break;
- }
- if(choice == 0) //Если выбор был завершение программы
- {
- Console.WriteLine("Работа с программой была завершена! Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- break;
- }
- }
- }
- /// <summary>
- /// Отобразить информацию о листе
- /// </summary>
- static void ShowInfoList(ref ListElems list)
- {
- char choice = ' '; //Выбор
- do
- {
- Console.Clear(); //Очистить консоль
- Console.WriteLine($"В двунаправленном циклическом списке {list.countElem} элементов. Вы хотите добавить? Y-да N-нет");
- bool attempt = char.TryParse(Console.ReadLine(), out choice);//Если значение нельзя преобразовать в char, то выдаст false
- if ((choice != 'Y' && choice != 'N' && choice != 'y' && choice != 'n') || !attempt)
- {
- Console.WriteLine("Введите, пожалуйста, правильное значение. Чтобы продолжить, нажмите любую кнопку.");
- Console.ReadKey();
- }
- } while (choice != 'Y' && choice != 'N' && choice != 'y' && choice != 'n');
- if(choice == 'Y' || choice == 'y')
- {
- list.Insert();
- }
- }
- /// <summary>
- /// Отобразить большие значения
- /// </summary>
- static void ShowGreatImportance(ListElems elems)
- {
- Console.Clear();
- if (elems.countElem == 0)
- {
- Console.WriteLine("Лист пуст. Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- return;
- }
- int index = 0;
- int countFindElem = 0;
- Console.WriteLine("-ЗНАЧЕНИЯ-КОТОРЫЕ-БОЛЬШЕ-ПЕРВОГО");
- for (elems.Current = elems.Head.next; elems.Current != elems.Head; elems.Current = elems.Current.next)
- {
- index++;
- if (elems.Current.value > elems.Head.value)
- {
- Console.WriteLine($"Номер элемента - {index}; значение - {elems.Current.value}");
- countFindElem++;
- }
- }
- if(countFindElem == 0)
- {
- Console.WriteLine("Таких значений нет!");
- }
- else
- {
- Console.WriteLine($"Значений больше первого - {countFindElem} шт.");
- }
- Console.WriteLine("Нажмите любую кнопку, чтобы продолжить.");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment