dsalglib
1.0.0
dsalglib is a ready to use data structures and algorithms library written in C++ . Object Oriented Template implementations are written.
Main Page
Namespaces
Classes
Files
File List
File Members
source
include
queue.h
Go to the documentation of this file.
1
//
2
// Created by moghya_s on 10/1/2016.
3
//
4
5
#ifndef DSALGLIB_QUEUE_H
6
#define DSALGLIB_QUEUE_H
7
namespace
dsa
8
{
9
10
11
12
template
<
class
type>
13
class
queue
14
{
15
private
:
16
struct
qnode
17
{
18
type data;
19
qnode *next;
20
21
qnode()
22
{
23
next =
nullptr
;
24
}
25
26
qnode(type param)
27
{
28
29
data = param;
30
next =
nullptr
;
31
}
32
};
33
qnode *front,*rear;
34
long
long
int
count;
35
36
/*
37
Function: creates qnode of type type
38
Pre: param of type type to create qnode
39
Post: qnode created
40
*/
41
qnode* create_qnode(type param)
42
{
43
qnode *temp =
new
qnode(param);
44
return
temp;
45
}
46
47
public
:
48
queue
()
49
{
50
front =
nullptr
;
51
rear =
nullptr
;
52
count = 0;
53
}
54
55
/*
56
* Function: enqueues element in queue at rear
57
* Pre: param element of type type
58
* Post: element added
59
*/
60
type
enqueue
(type param)
61
{
62
qnode *temp = create_qnode(param);
63
if
(count==0)
64
{
65
front = temp;
66
rear = temp;
67
front->next = rear;
68
}
69
else
70
{
71
rear->next = temp;
72
rear = temp;
73
74
}
75
count++;
76
return
param;
77
}
78
79
/*
80
* Function: removes element at front
81
* Pre: none
82
* Post: element removed
83
*/
84
type
dequeue
()
85
{
86
type param;
87
qnode *temp;
88
if
(count>0)
89
{
90
if
(count==1)
91
{
92
param = front->data;
93
delete
front;
94
front =
nullptr
;
95
rear =
nullptr
;
96
97
}
98
else
99
{
100
param = front -> data;
101
temp = front -> next;
102
delete
front;
103
front = temp;
104
}
105
count--;
106
return
param;
107
}
108
else
109
{
110
return
param;
111
}
112
}
113
114
/*
115
* Function:gives value of front element
116
* Pre: none
117
* Post: front value returned
118
*/
119
type
front_element
()
120
{
121
type param;
122
if
(count>0)
123
{
124
param = front->data;
125
}
126
127
return
param;
128
}
129
130
/*
131
* Function:gives value of front element
132
* Pre: none
133
* Post: front value returned
134
*/
135
type
rear_element
()
136
{
137
type param;
138
if
(count>0)
139
{
140
param = rear->data;
141
}
142
143
return
param;
144
}
145
146
/*
147
* Function:'=' operator overloaded for copyin one queue vals into another
148
* Pre: none
149
* Post: copied
150
*/
151
void
operator=
(
queue<type>
from)
152
{
153
clear
();
154
if
(from.
size
()>0)
155
{
156
qnode *temp= from.front;
157
while
(temp!=from.rear)
158
{
159
enqueue
(temp->data);
160
temp=temp->next;
161
}
162
enqueue
(temp->data);
163
}
164
return
;
165
}
166
167
/*
168
* Function:gives size of queue
169
* Pre: none
170
* Post: front value returned
171
*/
172
long
long
int
size
()
173
{
174
return
count;
175
}
176
177
178
/*
179
* Function: checks if empty
180
* Pre: none
181
* Post: true if isempty else false
182
*/
183
bool
isempty
()
184
{
185
if
(count==0)
186
return
true
;
187
else
188
return
false
;
189
}
190
191
/*
192
* Function: Removes all elements from queue
193
* Pre: none
194
* Post: queue empty
195
*/
196
void
clear
()
197
{
198
while
(count!=0)
199
{
200
dequeue
();
201
}
202
}
203
};
204
}
205
#endif //DSALGLIB_QUEUE_H
dsa
Definition:
alginc.h:12
dsa::queue::dequeue
type dequeue()
Definition:
queue.h:84
dsa::queue::rear_element
type rear_element()
Definition:
queue.h:135
dsa::queue::enqueue
type enqueue(type param)
Definition:
queue.h:60
dsa::queue::clear
void clear()
Definition:
queue.h:196
dsa::queue
Definition:
queue.h:13
dsa::queue::operator=
void operator=(queue< type > from)
Definition:
queue.h:151
dsa::queue::isempty
bool isempty()
Definition:
queue.h:183
dsa::queue::size
long long int size()
Definition:
queue.h:172
dsa::queue::front_element
type front_element()
Definition:
queue.h:119
dsa::queue::queue
queue()
Definition:
queue.h:48
Generated by
1.8.11