-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly.cpp
73 lines (51 loc) · 997 Bytes
/
singly.cpp
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
/* Queue usning Singly Linked List with head and tail pointers */
#include<iostream>
class Node{
public:
Node* next;
int data;
Node() : next(nullptr), data(0) {}
Node(int val) : next(nullptr), data(val) {}
};
class Queue{
public:
Queue() : head(nullptr), tail(nullptr) {}
void enqueue(int val);
void dequeue();
void print();
private:
Node* head;
Node* tail;
};
void Queue::enqueue(int val){
Node* newNode = new Node(val);
if(!head){
head = tail = newNode;
}
else{
tail->next = newNode;
tail = newNode;
}
}
void Queue::dequeue(){
if(!head) return;
Node* temp = head;
head = temp->next;
delete temp;
}
void Queue::print(){
Node* temp = head;
while(temp){
std::cout << temp->data << "\n";
temp = temp->next;
}
}
int main(){
Queue q;
for(int i=1; i<=10; ++i)
q.enqueue(i);
q.print();
q.dequeue();
q.print();
return 0;
}