Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ConsoleApplication1;
- namespace ConsoleApplication11
- {
- class Queue: ICloneable
- {
- #region Fields
- int _capacity;
- int _count;
- Item _firstItem;
- Item _lastItem;
- #endregion Fields
- #region Properties
- public int Count
- {
- get { return _count; }
- set
- {
- if (value < 0)
- {
- _count = 0;
- }
- else
- {
- _count = value;
- }
- }
- }
- public int Capacity
- {
- get
- {
- return _capacity;
- }
- set
- {
- if (value < 0)
- {
- _capacity = 0;
- }
- else
- {
- _capacity = value;
- }
- }
- }
- public Item FirstItem
- {
- get { return _firstItem; }
- set { _firstItem = value; }
- }
- public Item LastItem
- {
- get { return _lastItem; }
- set { _lastItem = value; }
- }
- #endregion Properties
- #region Constructors
- public Queue()
- {
- Capacity = 100;
- Count = 0;
- FirstItem = null;
- LastItem = null;
- }
- public Queue(int capacity)
- {
- Capacity = capacity;
- Count = 0;
- FirstItem = null;
- LastItem = null;
- }
- public Queue(Queue prototypeQueue)
- {
- Capacity = prototypeQueue.Capacity;
- Count = prototypeQueue.Count;
- FirstItem = prototypeQueue.FirstItem;
- LastItem = prototypeQueue.LastItem;
- }
- #endregion Constructors
- #region Methods
- public bool Contains(object requestedItem)
- {
- if (Count > 0)
- {
- Item currentItem = FirstItem;
- for (int i = 0; i < Count; i++)
- {
- if (currentItem == requestedItem)
- {
- return true;
- }
- else
- {
- currentItem = currentItem.Next;
- }
- }
- }
- return false;
- }
- public void Clear()
- {
- Count = 0;
- }
- public void Dequeue()
- {
- if (Count != 0)
- {
- FirstItem = FirstItem.Next;
- Count--;
- }
- }
- public void Enqueue(object data)
- {
- if (Count < Capacity)
- {
- if (Count == 0)
- {
- FirstItem = new Item(data);
- LastItem = FirstItem;
- Count++;
- }
- else
- {
- LastItem.Next = new Item(data); // ставим новый элемент после последнего и указываем на него из последнего
- LastItem = LastItem.Next; // делаем новый последним
- Count++;
- }
- }
- else
- {
- Console.WriteLine("Добавление элемента в коллекцию невозможно. Еще один элемент, и ваш ПК пойдет по швам.");
- }
- }
- public object Peek()
- {
- return FirstItem.Data;
- }
- public object[] ToArray()
- {
- if (Count == 0)
- {
- return null;
- }
- else
- {
- object[] array = new object[Count];
- Item currentItem = FirstItem;
- for (int i = 0; i < Count; i++)
- {
- Item cloneItem = currentItem.Clone() as Item;
- array[i] = cloneItem.Data;
- }
- return array;
- }
- }
- public object Clone()
- {
- Queue newQueue = new Queue(Capacity);
- if (Count != 0)
- {
- Item currentItem = FirstItem;
- newQueue.Enqueue(currentItem.Data);
- for (int i = 0; i < Count-1; i++)
- {
- currentItem = currentItem.Next;
- newQueue.Enqueue(currentItem.Data);
- }
- }
- return newQueue;
- }
- #endregion Methods
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement