-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7_QueueUsingLinkedList.cpp
More file actions
109 lines (92 loc) · 1.94 KB
/
7_QueueUsingLinkedList.cpp
File metadata and controls
109 lines (92 loc) · 1.94 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
#include <bits/stdc++.h>
using namespace std;
class Queue{
private:
class Node{
public:
int data;
Node *next;
};
Node *head = NULL, *tail = NULL;
public:
Queue(int n)
{
Node *newnode = new Node();
newnode->data = n;
newnode->next = NULL;
this->head = newnode;
this->tail = newnode;
}
void enqueue(int n)
{
Node *newnode = new Node();
newnode->data = n;
newnode->next = NULL;
if(this->head==NULL){
this->head = newnode;
this->tail = newnode;
return;
}
(this->tail)->next = newnode;
this->tail = (this->tail)->next;
}
void dequeue()
{
if(head==NULL){
cout << "Queue already Empty" << endl;
return;
}
Node *temp = this->head;
this->head = temp->next;
delete temp;
}
void show()
{
if(head==NULL){
cout << "Queue Empty" << endl;
return;
}
Node *trav = this->head;
while(trav!=NULL){
cout << trav->data << " ";
trav = trav ->next;
}
cout << endl;
}
bool isEmpty()
{
return head==NULL;
}
int top()
{
if(head==NULL){
cout << "Queue Empty" << endl;
return INT_MIN;
}
return head->data;
}
};
int main()
{
Queue q = Queue(7);
q.show();
q.enqueue(8);
q.enqueue(3);
q.enqueue(1);
q.enqueue(89);
cout << q.top() << endl;
q.dequeue();
q.show();
q.dequeue();
q.dequeue();
q.show();
q.enqueue(9);
q.show();
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
q.top();
q.show();
return 0;
}