队列(C#数据结构学习三)
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace soloDataStruct
6{
7 public class Myqueue<T>
8 {
9 private int maxSize;
10 private T[] queArray;
11 private int front;
12 private int rear;
13 private int nitem;
14 public T[] getAarry()
15 {
16 return queArray;
17 }
18 public Myqueue(int size)
19 {
20 maxSize = size;
21 queArray=new T[maxSize];
22 front = 0;
23 rear = -1;
24 nitem = 0;
25 }
26 public void Enquene(T data)
27 {
28 //入队
29 if (rear == maxSize - 1)
30 rear = -1;
31 queArray[++rear]=data;
32 nitem++;
33 }
34 public T Dequeue()
35 {
36 //出队
37 T temp=queArray[front++];
38 if (front == maxSize)
39 front = 0;
40 nitem--;
41 return temp;
42 }
43 public T getFront()
44 {
45 return queArray[front];
46 }
47 public int size()
48 {
49 return nitem;
50 }
51 }
52 class Program
53 {
54 static void Main(string[] args)
55 {
56 Myqueue<int> myqueue=new Myqueue<int>(5);
57 Console.WriteLine("input 5 element:1.2.3.4.5");
58 myqueue.Enquene(1);
59 myqueue.Enquene(2);
60 myqueue.Enquene(3);
61 myqueue.Enquene(4);
62 myqueue.Enquene(5);
63 Console.Write("Dequeue the front data:");
64 Console.WriteLine(myqueue.Dequeue());
65 Console.WriteLine("each element in queue!");
66 for (int i = 0; i < myqueue.size(); i++)
67 {
68 Console.WriteLine((myqueue.getAarry())[i]);
69
70 }
71 Console.ReadLine();
72 }
73 }
74}
75
2using System.Collections.Generic;
3using System.Text;
4
5namespace soloDataStruct
6{
7 public class Myqueue<T>
8 {
9 private int maxSize;
10 private T[] queArray;
11 private int front;
12 private int rear;
13 private int nitem;
14 public T[] getAarry()
15 {
16 return queArray;
17 }
18 public Myqueue(int size)
19 {
20 maxSize = size;
21 queArray=new T[maxSize];
22 front = 0;
23 rear = -1;
24 nitem = 0;
25 }
26 public void Enquene(T data)
27 {
28 //入队
29 if (rear == maxSize - 1)
30 rear = -1;
31 queArray[++rear]=data;
32 nitem++;
33 }
34 public T Dequeue()
35 {
36 //出队
37 T temp=queArray[front++];
38 if (front == maxSize)
39 front = 0;
40 nitem--;
41 return temp;
42 }
43 public T getFront()
44 {
45 return queArray[front];
46 }
47 public int size()
48 {
49 return nitem;
50 }
51 }
52 class Program
53 {
54 static void Main(string[] args)
55 {
56 Myqueue<int> myqueue=new Myqueue<int>(5);
57 Console.WriteLine("input 5 element:1.2.3.4.5");
58 myqueue.Enquene(1);
59 myqueue.Enquene(2);
60 myqueue.Enquene(3);
61 myqueue.Enquene(4);
62 myqueue.Enquene(5);
63 Console.Write("Dequeue the front data:");
64 Console.WriteLine(myqueue.Dequeue());
65 Console.WriteLine("each element in queue!");
66 for (int i = 0; i < myqueue.size(); i++)
67 {
68 Console.WriteLine((myqueue.getAarry())[i]);
69
70 }
71 Console.ReadLine();
72 }
73 }
74}
75