-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.cs
103 lines (92 loc) · 2.39 KB
/
Queue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication18
{
class Queue
{
// Queue fields...
int front;
int rear;
public int[] arr;
// Queue constructor
public Queue(int QueueLength)
{
this.front = -1;
this.rear = -1;
this.arr = new int[QueueLength];
}
// enqueue method
public void enqueue(int item) {
if(this.isFull())
Console.WriteLine("Error the queue is full...");
else {
if (this.front == -1)
this.front = 0;
this.rear++;
this.arr[this.rear] = item;
}
}
// dequeue method
public void dequeue()
{
if (this.isEmpty())
Console.WriteLine("Error the queue is empty...");
else {
this.arr[this.front] = 0;
this.front++;
}
}
// isFull method
public bool isFull() {
if (this.rear == this.arr.Length - 1)
return true;
else
return false;
}
// isEmpty method
public bool isEmpty()
{
if (this.front == -1)
return true;
else
return false;
}
// display method
public void display() {
foreach (var item in this.arr)
{
Console.WriteLine(" ___");
Console.WriteLine(" | " + item + " | ");
}
Console.WriteLine("________________________________");
}
// peek method
public void peek()
{
if (!this.isEmpty())
Console.WriteLine("retrieved element: " + this.arr[this.front]);
else
{
Console.WriteLine("queue Underflow...");
}
}
}
class Program
{
static void Main(string[] args)
{
Queue myQueue = new Queue(3);
myQueue.enqueue(1);
myQueue.enqueue(2);
myQueue.enqueue(3);
myQueue.peek();
myQueue.display();
myQueue.dequeue();
myQueue.display();
myQueue.peek();
}
}
}