-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQueue with arrays implementation.cs
76 lines (71 loc) · 1.81 KB
/
Queue with arrays implementation.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
class Solution
{
static void Main(String[] args)
{
// Testing
Queue queue = new Queue();
queue.Enqueue(6);
queue.Enqueue(4);
queue.Enqueue(9);
queue.Enqueue(8);
queue.Enqueue(5);
Console.WriteLine(queue.Front());
queue.Dequeue();
queue.Dequeue();
Console.WriteLine(queue.Front());
if (queue.IsEmpty())
Console.WriteLine("Queue is empty");
else
Console.WriteLine("Queue is Not empty");
}
}
class Queue
{
const int capacity = 100;
int[] que = new int[capacity];
int front = -1, rear = -1;
public void Enqueue(int val)
{
if (IsFull())
return;
else if (IsEmpty())
front = rear = 0;
else
rear = (rear + 1) % capacity; // (%)n is to start again from size , to use all memory of array
que[rear] = val;
}
public void Dequeue()
{
if (IsEmpty())
return;
else if (front == rear)
front = rear = -1;
else
front = (front + 1) % capacity; // (%)n is to start again from size , to use all memory of array
}
public int Front()
{
if (IsEmpty())
return -1; /// any number
else
return que[front];
}
public bool IsEmpty()
{
if (front == -1 && rear == -1)
return true;
else
return false;
}
public bool IsFull()
{
if ((rear + 1) % capacity == front)
return true; // like a circular , if the next of array is the front so will be Full , (%)n is to start again from size , to use all memory of array
else
return false;
}
}