-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path15 ● Queue_Using_Array.cs
99 lines (98 loc) · 2.6 KB
/
15 ● Queue_Using_Array.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
using System;
//Enqueue : add an element from the back of the list.
//Dequeue : delete an element from the beggining of the list.
namespace Queuearr
{
class Queue
{
public int head, rear;
int[] arr;
public Queue(int size)
{
head = rear = -1;
arr = new int[size];
}
public bool is_full()
{
return rear == arr.Length - 1;
}
public void enqueue(int n)
{
if(is_full())
Console.WriteLine("Queue is full!");
else
{
if(head==-1)
{
head = 0;
}
rear++;
arr[rear] = n;
}
}
public bool is_empty()
{
if(rear==-1 || rear<head)
{
return true;
}
return false;
}
public void dequeue()
{
if (is_empty())
Console.WriteLine("Queue is empty!");
else
Console.WriteLine("dequeue : " + arr[head++]);
}
public void peek()
{
if (is_empty())
Console.WriteLine("Queue is empty!");
else
Console.WriteLine("peeking at : " + arr[head]);
}
public void size()
{
Console.WriteLine("size = " + (rear-head+1));
}
public void display()
{
if (is_empty())
Console.WriteLine("Queue is empty!");
else
{
for(int i = head; i <= rear; i++)
{
Console.Write(arr[i]+ " " );
}
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the size : ");
int size = int.Parse(Console.ReadLine());
Queue L = new Queue(size);
L.enqueue(10);//10
L.enqueue(11);//10 11
L.enqueue(12);//10 11 12
L.enqueue(13);//10 11 12 13
L.enqueue(14);//10 11 12 13 14
L.display();//10 11 12 13 14
L.dequeue();//10 - > 11 12 13 14
L.size();//4
Console.WriteLine(L.is_empty());//false
Console.WriteLine(L.is_full());//false
L.dequeue();//11 - > 12 13 14
L.dequeue();//12 - > 13 14
L.dequeue();//13 - > 14
L.dequeue();//13 - >
L.dequeue();//empty!
L.dequeue();//empty!
}
}
}