Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace CSHARPMEDIUM
- {
- class Program
- {
- static void Main(string[] args)
- {
- LinkedList<int> list = new LinkedList<int>();
- list.Add(10);
- list.Add(15);
- list.Add(20);
- // Тест 1-го таска (IEnumerable)
- Book myLib = new Book("Книга с книгами");
- Book b1 = new Book("Blood, sweat and Pixels");
- Book b2 = new Book("C#4.0");
- b1.Parent = myLib;
- b2.Parent = myLib;
- foreach (Book item in myLib)
- {
- Console.WriteLine($"Book: {item.Name}");
- }
- // Конец теста
- list.ForEach(Console.WriteLine);
- Console.WriteLine(list.Count);
- Stack<string> strings = new Stack<string>();
- strings.Push("Test1");
- strings.Push("Test2");
- strings.Push("Test3");
- strings.Push("Test4");
- Console.WriteLine(strings.Pop());
- Console.WriteLine(strings.Pop());
- Console.WriteLine(strings.Pop());
- }
- }
- //Задача 1: Сделать реализацию IEnumerable
- class Book : IEnumerable<Book>
- {
- public static List<Book> Library = new List<Book>();
- public Book Parent;
- public string Name;
- public Book(string name)
- {
- Library.Add(this);
- Name = name;
- }
- public IEnumerator<Book> GetEnumerator()
- {
- return new LibraryEnumerator(Library.FindAll((b) => b.Parent == this).ToArray());
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- class LibraryEnumerator : IEnumerator<Book>
- {
- public Book Current => _elements[_current];
- private Book[] _elements;
- private int _current = -1;
- object IEnumerator.Current => Current;
- public LibraryEnumerator(Book[] elements)
- {
- _elements = elements;
- }
- public void Dispose()
- {
- _elements = null;
- }
- public bool MoveNext()
- {
- _current++;
- return _current < _elements.Length;
- }
- public void Reset()
- {
- _current = -1;
- }
- }
- // Method GetCurrent()
- //Задача 2: Сделать метод Contains. Проверяет есть ли заданное значенеи в спике
- //Задача 3: Сделать метод Remove, который удаляет эллемент под каким-либо номером
- //Задача 4: Оптимизировать добавление эллемента, через хранение ссылки на конец списка
- class LinkedList<T>
- {
- private ListElement _start;
- public int Count
- {
- get
- {
- int count = 0;
- ForEach((x) => count++);
- return count;
- }
- }
- public void Add(T data)
- {
- var newElement = new ListElement(data);
- if (_start == null)
- {
- _start = newElement;
- return;
- }
- ListElement current = _start;
- while (current.Next != null)
- {
- current = current.Next;
- }
- current.Next = newElement;
- }
- public void ForEach(Action<T> body)
- {
- ListElement current = _start;
- while (current != null)
- {
- body(current.Data);
- current = current.Next;
- }
- }
- public class ListElement
- {
- public static T StaticData;
- public T Data;
- public ListElement Next;
- public ListElement(T data)
- {
- Data = data;
- }
- }
- }
- //Задача 5: сделать обобщённую очередь
- class Stack<T>
- {
- private StackElement _head;
- public void Push(T data)
- {
- var newElement = new StackElement(data);
- newElement.Next = _head;
- _head = newElement;
- }
- public T Pop()
- {
- var h = _head;
- _head = h.Next;
- return h.Data;
- }
- public T Peek()
- {
- return _head.Data;
- }
- public Stack<T> Clone()
- {
- Stack<T> newStack = new Stack<T>();
- newStack._head = _head;
- return newStack;
- }
- public class StackElement
- {
- public T Data;
- public StackElement Next;
- public StackElement(T data)
- {
- Data = data;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment