Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace Storage
- {
- class Program
- {
- static void Main(string[] args)
- {
- var st = new Storage<int>();
- while (true)
- {
- try
- {
- Console.WriteLine(
- "Menu:\n" +
- "1. Print all items\n" +
- "2. Insert item\n" +
- "3. Get current item\n" +
- "4. Move current item to next\n" +
- "5. Move current item to prev\n" +
- "6. Remove current item\n" +
- "7. Search item by index\n" +
- "8. Remove item by index");
- Console.Write(">> ");
- char selected = Console.ReadKey().KeyChar;
- Console.WriteLine();
- switch (selected)
- {
- case '1':
- {
- Console.WriteLine("Storage data:");
- if (st.Count == 0) Console.WriteLine("*empty*");
- else
- {
- int i = 0;
- foreach (var item in st)
- {
- Console.WriteLine($"[{i}] {item}");
- i++;
- }
- }
- };
- break;
- case '2':
- {
- Console.Write("Insert value: ");
- int value = Convert.ToInt32(Console.ReadLine());
- int index = st.Add(value);
- Console.WriteLine($"Success. Item inserted at index {index}.");
- };
- break;
- case '3':
- {
- Console.WriteLine($"Current item index: {st.CurrentIndex}.\n" +
- $"Current item value: {st.Current}.");
- };
- break;
- case '4':
- {
- bool moved = st.MoveNext();
- string status = moved ? "Success" : "Failed (last item)";
- Console.WriteLine($"Move status: {status}.\n" +
- $"Current item index: {st.CurrentIndex}.");
- };
- break;
- case '5':
- {
- bool moved = st.MovePrev();
- string status = moved ? "Success" : "Failed (first item)";
- Console.WriteLine($"Move status: {status}.\n" +
- $"Current item index: {st.CurrentIndex}.");
- };
- break;
- case '6':
- {
- st.RemoveByIndex(st.CurrentIndex);
- Console.WriteLine("Current item removed." +
- $"New current item value: {st.Current}." +
- $"New current item index: {st.CurrentIndex}.");
- };
- break;
- case '7':
- {
- Console.Write("Enter item index to search: ");
- int index = Convert.ToInt32(Console.ReadLine());
- int value = st.GetByIndex(index);
- Console.WriteLine($"Found. Item value: {value}.");
- };
- break;
- case '8':
- {
- Console.Write("Enter item index to remove: ");
- int index = Convert.ToInt32(Console.ReadLine());
- st.RemoveByIndex(index);
- Console.WriteLine($"Success. Item removed.");
- };
- break;
- default:
- {
- Console.WriteLine("Incorrect input.");
- };
- break;
- }
- }
- catch(Exception ex)
- {
- Console.WriteLine($"Exception: {ex.Message}.");
- }
- Console.WriteLine();
- Console.Write("Press any key to continue...");
- Console.ReadKey();
- Console.Clear();
- }
- }
- static void test()
- {
- var mike = new CSStudent("Mike", 80);
- var alex = new CSStudent("Alex", 90);
- var tommy = new CSStudent("Tommy", 100);
- var prog = new CSStudent("1CProgrammer", -20);
- var st = new Storage<CSStudent>();
- st.Add(mike);
- st.Add(alex);
- st.Add(tommy);
- st.Add(prog);
- st.Remove(tommy);
- st.RemoveByIndex(0);
- foreach (var student in st)
- {
- Console.WriteLine($"{student.Name}: {student.IQ}");
- student.InstallArch();
- }
- foreach (var student in st)
- {
- Console.WriteLine($"{student.Name}: {student.IQ}");
- }
- var kek = st.GetByIndex(0);
- Console.WriteLine($"index 0 {kek.Name}: {kek.IQ}");
- }
- }
- class Storage<T> : IEnumerable<T>
- {
- private class Node
- {
- public T Value { get; }
- public Node Next { get; set; }
- public Node Prev { get; set; }
- public Node(T value)
- {
- this.Value = value;
- }
- }
- private Node _first;
- private Node _last;
- private Node _current;
- private int _currentIndex;
- private int _count { get; set; }
- public T Current
- {
- get
- {
- return _current.Value;
- }
- }
- public int CurrentIndex
- {
- get
- {
- return _currentIndex;
- }
- set
- {
- if (value < 0 || value > _count)
- throw new IndexOutOfRangeException();
- _currentIndex = value;
- }
- }
- public bool MoveNext()
- {
- if (_currentIndex + 1 < _count)
- {
- _currentIndex++;
- _current = _current.Next;
- return true;
- }
- return false;
- }
- public bool MovePrev()
- {
- if (_currentIndex > 0)
- {
- _currentIndex--;
- _current = _current.Prev;
- return true;
- }
- return false;
- }
- public int Count
- {
- get
- {
- return _count;
- }
- }
- public int Add(T value)
- {
- var node = new Node(value);
- if (_first == null)
- _first = node;
- else
- {
- _last.Next = node;
- node.Prev = _last;
- }
- _last = node;
- _currentIndex = _count;
- _current = node;
- _count++;
- return _currentIndex;
- }
- private void RemoveNode(Node node)
- {
- if (_current.Equals(node)) this.MovePrev();
- // если узел не последний
- if (node.Next != null)
- {
- node.Next.Prev = node.Prev;
- }
- else
- {
- // если последний, переустанавливаем _last
- _last = node.Prev;
- }
- // если узел не первый
- if (node.Prev != null)
- {
- node.Prev.Next = node.Next;
- }
- else
- {
- // если первый, переустанавливаем _first
- _first = node.Next;
- }
- _count--;
- return;
- }
- public void Remove(T value)
- {
- for (Node node = _first; node != null; node = node.Next)
- {
- if (node.Value.Equals(value))
- {
- RemoveNode(node);
- return;
- }
- }
- throw new Exception($"Element {value} not found");
- }
- public void RemoveByIndex(int index)
- {
- bool reversedSearch = (index > (_count / 2));
- //Console.WriteLine(reversedSearch);
- if (reversedSearch)
- {
- int i = _count - 1;
- for (Node node = _last; node != null; node = node.Prev)
- {
- if (i == index)
- {
- RemoveNode(node);
- return;
- }
- i--;
- }
- }
- else
- {
- int i = 0;
- for (Node node = _first; node != null; node = node.Next)
- {
- if (i == index)
- {
- RemoveNode(node);
- return;
- }
- i++;
- }
- }
- throw new Exception($"Element at index {index} not found");
- }
- public T GetByIndex(int index)
- {
- bool reversedSearch = (index > (_count / 2));
- //Console.WriteLine(reversedSearch);
- if (reversedSearch)
- {
- int i = _count - 1;
- for (Node node = _last; node != null; node = node.Prev)
- {
- if (i == index) return node.Value;
- i--;
- }
- }
- else
- {
- int i = 0;
- for (Node node = _first; node != null; node = node.Next)
- {
- if (i == index) return node.Value;
- i++;
- }
- }
- throw new Exception($"Element at index {index} not found");
- }
- public IEnumerator<T> GetEnumerator()
- {
- for (Node node = _first; node != null; node = node.Next)
- {
- yield return node.Value;
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- }
- class CSStudent
- {
- public string Name { get; }
- private int _iq { get; set; }
- public int IQ
- {
- get
- {
- return _iq;
- }
- }
- public CSStudent(string name, int iq)
- {
- this.Name = name;
- this._iq = iq;
- }
- public void InstallArch()
- {
- Console.WriteLine($"User {Name} has installed ArchLinux. Congrats!");
- this._iq += 9999;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement