Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ClassLibrary1;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace C_sharp_olympic
- {
- class Program
- {
- static void psh(OwnQueue<int> q, int val)
- {
- Console.WriteLine($"+++++ {val}");
- q.Enqueue(val);
- }
- static void rmf(OwnQueue<int> q)
- {
- int val = q.Dequeue();
- Console.WriteLine($"----- {val}");
- }
- static void Main(string[] args)
- {
- Random rnd = new Random();
- List<int> ls = new List<int>();
- for(int i = 0; i < 100000; i++)
- ls.Add(i+1);
- OwnQueue<int> q = new OwnQueue<int>();
- for(int i = 0; i < ls.Count; i++)
- q.Enqueue(ls[i]);
- for(int i = 0; i < ls.Count/2; i++)
- q.Dequeue();
- //Console.WriteLine(q._head);
- //Console.WriteLine(q._tail);
- //Console.WriteLine($"Cap {q._capacity}");
- for (int i = 0; i < ls.Count/2; i++)
- if(q.Contains(ls[i]))
- Console.WriteLine($"UVAGA FOUND {ls[i]}");
- for (int i = ls.Count / 2; i < ls.Count; i++)
- if (!q.Contains(ls[i]))
- Console.WriteLine($"UVAGA NOT FOUND {ls[i]}");
- Console.WriteLine("Done");
- /*
- var testList = new List<int>() { 1, 2, 3, 4, 5 };
- for(int i = 0; i < testList.Count; i++)
- psh(q, testList[i]);
- Console.WriteLine();
- for (int i = 0; i < testList.Count; i++)
- rmf(q);
- Console.WriteLine();
- psh(q, 5);
- psh(q, 6);
- psh(q, 7);
- psh(q, 8);
- psh(q, 9);
- rmf(q);
- rmf(q);
- rmf(q);
- psh(q, 10);
- rmf(q);
- psh(q, 11);
- psh(q, 12);
- rmf(q);
- psh(q, 13);
- psh(q, 14);
- psh(q, 15);
- psh(q, 16);
- psh(q, 17);
- psh(q, 18);
- psh(q, 19);
- psh(q, 20);
- psh(q, 21);
- psh(q, 22);
- psh(q, 23);
- psh(q, 24);
- psh(q, 25);
- psh(q, 26);
- psh(q, 27);
- psh(q, 28);
- psh(q, 29);
- rmf(q);
- rmf(q);
- psh(q, 30);
- while (q.Count > 0)
- rmf(q);
- */
- Console.ReadKey();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ClassLibrary1
- {
- public class OwnQueue<T>
- {
- private T[] mass = new T[4];
- private int _count = 0, _capacity = 4, _head = 0, _tail = 0;
- public OwnQueue()
- {
- _count = 0;
- _capacity = 4;
- }
- public OwnQueue(int capacity)
- {
- Count = 0;
- setCapacity(capacity);
- }
- public int Count
- {
- get
- {
- return _count;
- }
- private set
- {
- if (value > _capacity)
- {
- int newCapacity = _capacity;
- while (newCapacity < value)
- newCapacity *= 2;
- setCapacity(newCapacity);
- }
- _count = value;
- }
- }
- public T Peek()
- {
- if (Count == 0)
- throw new InvalidOperationException();
- return mass[_head];
- }
- public bool Contains(T item)
- {
- if (_head <= _tail)
- for (int i = _head; i <= _tail; i++)
- if (mass[i].Equals(item))
- return true;
- if (_head > _tail)
- {
- for (int i = _head; i < mass.Length; i++)
- if (mass[i].Equals(item))
- return true;
- for (int i = 0; i <= _tail; i++)
- if (mass[i].Equals(item))
- return true;
- }
- return false;
- }
- private void setCapacity(int value)
- {
- if (value < 0)
- throw new ArgumentOutOfRangeException();
- _capacity = value;
- T[] newArr = new T[_capacity];
- int j = 0;
- if (_head <= _tail)
- for (int i = _head; j < newArr.Length && i <= _tail; i++)
- newArr[j++] = mass[i];
- if (_head > _tail)
- {
- for (int i = _head; j < newArr.Length && i < mass.Length; i++)
- newArr[j++] = mass[i];
- for (int i = 0; j < newArr.Length && i <= _tail; i++)
- newArr[j++] = mass[i];
- }
- mass = newArr;
- _head = 0;
- _tail = _count - 1;
- }
- public void Enqueue(T elem)
- {
- this.Count++;
- if (this.Count > 1)
- {
- if (_tail + 1 >= _capacity)
- _tail = 0;
- else
- _tail++;
- }
- mass[_tail] = elem;
- //Console.WriteLine($" to {_tail} ({_capacity} and {_count}) ({_head} and {_tail})");
- }
- public T Dequeue()
- {
- if(this.Count <= 0)
- throw new InvalidOperationException();
- this.Count--;
- T firstElement = mass[_head];
- //Console.WriteLine($" from {_head} ({_capacity} and {_count}) ({_head} and {_tail})");
- if (this.Count != 0)
- {
- if (_head + 1 >= _capacity)
- _head = 0;
- else
- _head++;
- }
- return firstElement;
- }
- }
- }
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using System;
- using ClassLibrary1;
- namespace UnitTestProject1
- {
- [TestClass]
- public class UnitTest1
- {
- [TestMethod]
- public void ZeroCountAtCreation()
- {
- int expected = 0;
- OwnQueue<int> queue = new OwnQueue<int>();
- Assert.AreEqual(expected, queue.Count);
- }
- [TestMethod]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void OutOfRangeException()
- {
- OwnQueue<int> queue = new OwnQueue<int>(-5);
- }
- [TestMethod]
- public void CountGrowingAfterAdd()
- {
- int n = 100;
- OwnQueue<int> queue = new OwnQueue<int>();
- int expected = n;
- for (int i = 0; i < n; i++)
- queue.Enqueue(i);
- Assert.AreEqual(expected, queue.Count);
- }
- [TestMethod]
- public void CountFallingAfterRemove()
- {
- int n = 100;
- OwnQueue<int> queue = new OwnQueue<int>();
- int expected = n - n/2;
- for (int i = 0; i < n; i++)
- queue.Enqueue(i);
- for (int i = 0; i < n/2; i++)
- queue.Dequeue();
- Assert.AreEqual(expected, queue.Count);
- }
- [TestMethod]
- [ExpectedException(typeof(InvalidOperationException))]
- public void EmptyDequeueException()
- {
- OwnQueue<int> queue = new OwnQueue<int>();
- queue.Dequeue();
- }
- [TestMethod]
- [ExpectedException(typeof(InvalidOperationException))]
- public void EmptyPeekException()
- {
- OwnQueue<int> items = new OwnQueue<int>();
- items.Peek();
- }
- [TestMethod]
- public void PeekTest()
- {
- int n = 100;
- OwnQueue<int> items = new OwnQueue<int>();
- int expected = n/2+1;
- for (int i = 0; i < n; i++)
- items.Enqueue(i+1);
- for (int i = 0; i < n/2; i++)
- items.Dequeue();
- Assert.AreEqual(expected, items.Peek());
- }
- [TestMethod]
- public void ContainsTest()
- {
- int n = 100;
- OwnQueue<int> items = new OwnQueue<int>();
- for (int i = 0; i < n; i++)
- items.Enqueue(i);
- for (int i = 0; i < n/2; i++)
- items.Dequeue();
- for (int i = 0; i < n/2; i++)
- Assert.IsFalse(items.Contains(i));
- for (int i = n-n/2; i < n; i++)
- Assert.IsTrue(items.Contains(i));
- }
- [TestMethod]
- public void DequeueTest()
- {
- int n = 100;
- OwnQueue<int> items = new OwnQueue<int>();
- for (int i = 0; i < n; i++)
- items.Enqueue(i);
- for (int i = 0; i < n; i++)
- Assert.AreEqual(i, items.Dequeue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement