ÊÀÒÅÃÎÐÈÈ:
ÀñòðîíîìèÿÁèîëîãèÿÃåîãðàôèÿÄðóãèå ÿçûêèÄðóãîåÈíôîðìàòèêàÈñòîðèÿÊóëüòóðàËèòåðàòóðàËîãèêàÌàòåìàòèêàÌåäèöèíàÌåõàíèêàÎáðàçîâàíèåÎõðàíà òðóäàÏåäàãîãèêàÏîëèòèêàÏðàâîÏñèõîëîãèÿÐèòîðèêàÑîöèîëîãèÿÑïîðòÑòðîèòåëüñòâîÒåõíîëîãèÿÔèçèêàÔèëîñîôèÿÔèíàíñûÕèìèÿ×åð÷åíèåÝêîëîãèÿÝêîíîìèêàÝëåêòðîíèêà
|
²íòåðôåéñ IQueue ⇐ ÏðåäûäóùàÿÑòð 3 èç 3 us³ng System; us³ng System.Collect³ons.Gener³c; us³ng System.L³nq; us³ng System.Text;
namespace W³nqueue { publ³c ³nterface ²queue<T> { vo³d Enqueue(T ³tem); T Dequeue(); ³nt Count { get; } L³st<T> Tol³st(); vo³d Clear(); bool ²sempty { get; } T Peek(); } }
2. Êëàñ MasQuery, ùî ðåàë³çóº ³íòåðôåéñ IQueue ç âèêîðèñòàííÿì ìàñèâó us³ng System; us³ng System.Collect³ons.Gener³c; us³ng System.L³nq; us³ng System.Text;
namespace W³nqueue { class Masquery<T>:²queue<T> { T[] arrayelem;
publ³c Masquery() { arrayelem = new T[0]; }
// Î÷èùຠ÷åðãà
publ³c vo³d Clear() { arrayelem = new T[0]; }
// Äîâæèíà ÷åðãè
publ³c ³nt Count { get { return (³nt)arrayelem.Length; } }
// Âèäàëÿº é ïîâåðòຠîá'ºêò, ùî ïåðåáóâຠíà ïî÷àòêó ÷åðãè.
publ³c T Dequeue() { ³f (²sempty) throw new Except³on("Queue ³s empty.");
T r = arrayelem[0]; T[] buf = arrayelem; arrayelem = new T[arrayelem.Length - 1]; // arrayelem = arrayelem.Sk³p(1).Toarray(); for (³nt ³ = 0; ³ < arrayelem.Length; ³++) arrayelem[³] = buf[³ + 1]; return r;
// arrayelem = arrayelem.Sk³p(1).Toarray(); }
/// Äîäຠåëåìåíò ó ÷åðãó
publ³c vo³d Enqueue(T ³tem) { T[] buf = arrayelem; arrayelem = new T[arrayelem.Length + 1]; for (³nt ³ = 0; ³ < buf.Length; ³++) arrayelem[³] = buf[³]; arrayelem[arrayelem.Length - 1] = ³tem; }
/// Ïîâåðòàº, àëå íå âèäàëÿº ³ç ÷åðãè îá'ºêò.
publ³c T Peek() { return arrayelem[0]; }
publ³c bool ²sempty { get { return Count == 0; } }
/// Ïåðåâîäèòü ó ñïèñîê
publ³c L³st<T> Tol³st() { return arrayelem.Tol³st(); }
}
}
3. Êëàñ LQueue, ùî ðåàë³çóº ³íòåðôåéñ ²queue ç âèêîðèñòàííÿì ñïèñêó us³ng System; us³ng System.Collect³ons.Gener³c; us³ng System.L³nq; us³ng System.Text;
namespace W³nqueue {
class Lqueue<T>:²queue<T> { pr³vate L³st<T> Data=new L³st<T>();
publ³c vo³d Enqueue(T ³tem) { Data.Add(³tem); }
publ³c T Dequeue() { ³f (²sempty) throw new Except³on("Queue ³s empty."); var ² = Data[0]; Data.Removeat(0); return ²; }
publ³c ³nt Count { get { return Data.Count; } }
publ³c L³st<T> Tol³st() { return Data; }
publ³c vo³d Clear() { Data.Clear(); }
publ³c bool ²sempty { get { return Count == 0; } }
publ³c T Peek() { ³f (²sempty) throw new Except³on("Queue ³s empty."); return Data[0]; } } }
4. Êëàñ PQueue, ùî ðåàë³çóº ³íòåðôåéñ çà äîïîìîãîþ äâîíàïðàâëåíîãî ñïèñêó us³ng System; us³ng System.Collect³ons.Gener³c; us³ng System.L³nq; us³ng System.Text;
namespace W³nqueue {
class Pqueue<T>:²queue<T> { pr³vate class Queueelement { publ³c T Value; publ³c Queueelement Next; publ³c Queueelement Prev; }
pr³vate Queueelement Head; pr³vate Queueelement Ta³l;
publ³c vo³d Enqueue(T ³tem) { Count++; var Newelement=new Queueelement() { Value = ³tem }; ³f (Head == null) { Head = Newelement; Ta³l = Head; } else { Ta³l.Next = Newelement; Newelement.Prev = Ta³l; Ta³l = Ta³l.Next; } }
publ³c T Dequeue() { ³f (²sempty) throw new Except³on("Queue ³s empty."); Count--; var ²tem = Ta³l; Ta³l = Ta³l.Prev; ³f (Ta³l == null) Head = null; else Ta³l.Next = null; return ²tem.Value; }
publ³c ³nt Count { get; pr³vate set; }
publ³c L³st<T> Tol³st() { L³st<T> Q = new L³st<T>(); var ²tem = Head; wh³le (²tem != null) { Q.Add(²tem.Value); ²tem = ²tem.Next; } return Q; }
publ³c vo³d Clear() { Head = null; Ta³l = null; }
publ³c bool ²sempty { get { return Head == null; } }
publ³c T Peek() { ³f (²sempty) throw new Except³on("Queue ³s empty."); return Head.Value; } } }
5. Êëàñ Form1, ùî ðåàë³çóº äåìîíñòðàö³þ âèêîðèñòàííÿ ìåòîä³â êëàñó. us³ng System.Data; us³ng System.Draw³ng; us³ng System.L³nq; us³ng System.Text; us³ng System.W³ndows.Forms;
namespace W³nqueue { publ³c part³al class Form1 : Form { ²queue<str³ng> Queue; publ³c Form1() { ²n³t³al³zecomponent(); // ï³äêëþ÷åííÿ êëàñó, ùî ðåàë³çóº ³íòåðôåéñ Queue = new Masquery<str³ng>(); }
pr³vate vo³d Enqueue_Cl³ck(object sender, Eventargs e) { Queue.Enqueue(Enqvalue.Text); Resetma³ntb(); }
pr³vate vo³d button1_Cl³ck(object sender, Eventargs e) { try { Messagebox.Show("Element = " + Queue.Dequeue()); } catch(Except³on E) { Messagebox.Show("Except³on: "+E.Message); } Resetma³ntb(); }
pr³vate vo³d Clear_Cl³ck(object sender, Eventargs e) { Queue.Clear(); Resetma³ntb(); }
pr³vate vo³d Count_Cl³ck(object sender, Eventargs e) { Messagebox.Show("Count = "+Queue.Count.Tostr³ng()); }
pr³vate vo³d Resetma³ntb() { Ma³ntb.L³nes = Queue.Tol³st().Toarray(); } } }
Ðîáî÷èé ïðîåêò ïðåäñòàâëåíèé íà íàñòóïíîìó ðèñóíêó.
Ðèñóíîê 2.5 - Ðîáî÷èé ïðîåêò
|