- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingArray.cpp
More file actions
Latest commit
151 lines (133 loc) · 3.6 KB
/
Copy pathQueueUsingArray.cpp
File metadata and controls
151 lines (133 loc) · 3.6 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<iostream>
#include<stdio.h>
usingnamespacestd;
#defineQUEUE_UNDERFLOW1; // defining an exception for empty queue;
classQueue
{
private:
int capacity;
int front;
int rear;
int *ptr;
public:
Queue();
Queue(int);
Queue(Queue&);
Queue& operator=(Queue &);
voidinsertAtRear(int);
boolisOverflow();
boolisUnderflow();
intviewRearElement();
intviewFrontElement();
voiddeleteFront();
intcountElement();
~Queue();
};
Queue::Queue()
{
capacity = 0;
front = rear = -1;
ptr = NULL;
}
Queue::Queue(int cap) // constructor
{
capacity = cap;
front = rear = -1; // initially queue is empty.
ptr = newint[cap];
}
Queue::Queue(Queue& q) // copy constructor
{
capacity = q.capacity;
front = q.front;
rear = q.rear;
ptr = newint[capacity];
for(int i = 0; i<capacity; i++)
ptr[i] = q.ptr[i];
}
Queue& Queue::operator=(Queue &q)
{
if(ptr != NULL)
delete [] ptr; // this-> deleting the memory of pointer for( if it containes any other location)
capacity = q.capacity;
front = q.front;
rear = q.rear;
ptr = newint[capacity];
for(int i = 0; i<capacity; i++)
ptr[i] = q.ptr[i];
return *this;
}
voidQueue::insertAtRear(int data)
{
if(isOverflow()) // when queue is full
cout<<"Queue Overflow! Data cannot inserted.";
elseif(isUnderflow()) // when queue is empty
{
front = rear = 0;
ptr[front] = data;
}
elseif(rear == capacity-1) // when queue is full from rear but front space is empty
{
rear = 0;
ptr[rear] = data;
}
else// normal case
{
rear++;
ptr[rear] = data;
}
}
boolQueue::isOverflow() // for checking our queue is full or not
{
if(rear == capacity-1 && front == 0 || rear + 1 == front)
returntrue;
else
returnfalse;
}
boolQueue::isUnderflow() // for checking our queue is empty or not
{
if(rear == -1 && front == -1) // checking for both rear or front is not necessary
returntrue;
else
returnfalse;
}
intQueue::viewRearElement() // for getting last or latest element inserted in queue
{
if(isUnderflow())
throwQUEUE_UNDERFLOW;
return ptr[rear];
}
intQueue::viewFrontElement() // for getting first or oldest element inserted in queue
{
if(isUnderflow())
throwQUEUE_UNDERFLOW;
return ptr[front];
}
voidQueue::deleteFront()
{
if(isUnderflow()) // when queue is empty
cout<<"Queue Underflow! Deletion not possible.";
elseif(front == rear) // when queue has only one element
front = rear = -1;
elseif(front == capacity-1) // when front is our last element
front = 0;
else// normal case
front++;
}
intQueue::countElement()
{
if(isOverflow()) // if queue is full
return capacity;
elseif(isUnderflow()) // if queue is empty
return0;
elseif(rear == front) // if queue has only one element
return1;
elseif(rear > front) // if rear is greater than front
return (rear-front+1);
else
return capacity-front+rear+1; // if any case other than all these
}
Queue::~Queue()
{
if(ptr != NULL)
delete [] ptr; // not necessary to check the NULL Condition because of parametrised constructor.
}