-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implement_Queue.xpp
53 lines (45 loc) · 959 Bytes
/
Implement_Queue.xpp
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
#include <bits/stdc++.h>
struct Node {
int val;
Node * next;
Node(int x) {
val = x;
next = NULL;
}
};
class Queue {
Node * head;
Node * tail;
public:
Queue() {
// Implement the Constructor
head = NULL;
tail = NULL;
}
/*----------------- Public Functions of Queue -----------------*/
bool isEmpty() {
if(head == NULL) return true;
return false;
}
void enqueue(int data) {
Node * newNode = new Node(data);
if(head == NULL) {
head = newNode;
tail = newNode;
return;
}
tail->next = newNode;
tail = newNode;
}
int dequeue() {
if(head==NULL) return -1;
int val = head->val;
head = head->next;
if(head == NULL) tail=NULL;
return val;
}
int front() {
if(head == NULL) return -1;
return head->val;
}
};