Created
April 14, 2016 20:06
-
-
Save yurii-litvinov/fc51f0ed6dce8a21d9c1a5c4fb0ca19d to your computer and use it in GitHub Desktop.
code review example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Queue_ | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Queue queue = new Queue(); | |
queue.Enqueue(2, 1); | |
queue.Enqueue(8, 1); | |
queue.Enqueue(5, 3); | |
int tempVal = queue.Dequeue(); | |
tempVal = queue.Dequeue(); | |
tempVal = queue.Dequeue(); | |
} | |
} | |
public class Queue | |
{ | |
public Qelement qBeg = null; | |
public Qelement qEnd = null; | |
public class Qelement | |
{ | |
private int val; | |
public int Value | |
{ | |
get | |
{ | |
return val; | |
} | |
set | |
{ | |
val = value; | |
} | |
} | |
public int priority; | |
public Qelement Next { get; set; } | |
} | |
/// <summary> | |
/// Проверяет очередь на пустоту. | |
/// </summary> | |
/// <returns></returns> | |
public bool IsEmpty() | |
{ | |
return qBeg == null; | |
} | |
/// <summary> | |
/// Находит позицию для добавления нового элемента в очередь с приоритетами. | |
/// </summary> | |
/// <param name="priotity"></param> | |
/// <returns></returns> | |
private Qelement SearchPos(int priotity) | |
{ | |
Qelement pos = qBeg; | |
while (pos != null && priotity < pos.priority - 1) | |
{ | |
pos = pos.Next; | |
} | |
return pos; | |
} | |
/// <summary> | |
/// Добавляет новый элемент в очередь с приоритетами. | |
/// </summary> | |
/// <param name="val"></param> | |
/// <param name="priotity"></param> | |
public void Enqueue(int val, int priotity) | |
{ | |
Qelement pos = SearchPos(priotity); | |
if (pos != null) | |
{ | |
Qelement newEl = new Qelement() | |
{ | |
Next = pos.Next, | |
Value = val | |
}; | |
pos.Next = newEl; | |
if (pos == qBeg && priotity > qBeg.priority) | |
{ | |
int temp = pos.Value; | |
pos.Value = newEl.Value; | |
newEl.Value = temp; | |
} | |
} | |
else | |
{ | |
Qelement newEl = new Qelement() | |
{ | |
Next = null, | |
Value = val | |
}; | |
if (!IsEmpty()) | |
{ | |
qEnd.Next = newEl; | |
qEnd = newEl; | |
} | |
else | |
{ | |
qBeg = newEl; | |
qEnd = newEl; | |
} | |
newEl.priority = priotity; | |
} | |
} | |
/// <summary> | |
/// Возвращает значение с наивысшим приоритетом и удаляет его из очереди. | |
/// </summary> | |
/// <returns></returns> | |
public int Dequeue() | |
{ | |
if (!IsEmpty()) | |
{ | |
int temp = qBeg.Value; | |
qBeg = qBeg.Next; | |
return temp; | |
} | |
throw new EmptyQueueExeption(); | |
} | |
} | |
/// <summary> | |
/// Исключение возникает при попытке удалить элемент из пустой очереди. | |
/// </summary> | |
public class EmptyQueueExeption : ApplicationException | |
{ | |
public EmptyQueueExeption() | |
{ | |
} | |
public EmptyQueueExeption(string message) | |
: base(message) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment