-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue-intro.cpp
More file actions
90 lines (75 loc) · 1.34 KB
/
Queue-intro.cpp
File metadata and controls
90 lines (75 loc) · 1.34 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
#include <iostream>
#include <vector>
using namespace std;
class Node // class to create a node
{
public:
int data;
Node *next;
Node(int val)
{
data = val;
next = NULL;
}
};
class Queue
{
Node *head;
Node *tail;
public:
Queue()
{
head = tail = NULL;
}
void push(int data) // to push data in queue
{
Node *newNode = new Node(data);
if (empty()) // if queue is empty
{
head = tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
}
void pop() // to pop date from queue
{
if (empty()) // if queue is empty
{
cout << "Queue is empty\n";
return;
}
Node *temp = head;
head = head->next;
delete temp;
}
int front() // to return front value of queue
{
if (empty()) // if queue is empty
{
cout << "Queue is empty\n";
return 0;
}
return head->data;
}
bool empty() // to check if queue is empty
{
return head == NULL;
}
};
int main()
{
Queue q;
q.push(1);
q.push(2);
q.push(3);
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
cout << endl;
return 0;
}