Algorithms_in_C++
1.0.0
Set of algorithms implemented in C++.
queue.h
1
/* This class specifies the basic operation on a queue as a linked list */
2
#ifndef DATA_STRUCTURES_QUEUE_H_
3
#define DATA_STRUCTURES_QUEUE_H_
4
5
#include <cassert>
6
#include <iostream>
7
8
/** Definition of the node */
9
template
<
class
Kind>
10
struct
node
{
11
Kind data;
12
node<Kind>
*next;
13
};
14
15
/** Definition of the queue class */
16
template
<
class
Kind>
17
class
queue
{
18
public
:
19
/** Show queue */
20
void
display
() {
21
node<Kind>
*current =
queueFront
;
22
std::cout
<<
"Front --> "
;
23
while
(current != NULL) {
24
std::cout
<< current->data <<
" "
;
25
current = current->next;
26
}
27
std::cout
<<
std::endl
;
28
std::cout
<<
"Size of queue: "
<< size <<
std::endl
;
29
}
30
31
/** Default constructor*/
32
queue
() {
33
queueFront
= NULL;
34
queueRear
= NULL;
35
size = 0;
36
}
37
38
/** Destructor */
39
~queue
() {}
40
41
/** Determine whether the queue is empty */
42
bool
isEmptyQueue
() {
return
(
queueFront
== NULL); }
43
44
/** Add new item to the queue */
45
void
enQueue
(Kind item) {
46
node<Kind>
*newNode;
47
newNode =
new
node<Kind>
;
48
newNode->data = item;
49
newNode->next = NULL;
50
if
(
queueFront
== NULL) {
51
queueFront
= newNode;
52
queueRear
= newNode;
53
}
else
{
54
queueRear
->next = newNode;
55
queueRear
=
queueRear
->next;
56
}
57
size++;
58
}
59
60
/** Return the first element of the queue */
61
Kind
front
() {
62
assert(
queueFront
!= NULL);
63
return
queueFront
->data;
64
}
65
66
/** Remove the top element of the queue */
67
void
deQueue
() {
68
node<Kind>
*temp;
69
if
(!
isEmptyQueue
()) {
70
temp =
queueFront
;
71
queueFront
=
queueFront
->next;
72
delete
temp;
73
size--;
74
}
else
{
75
std::cout
<<
"Queue is empty !"
<<
std::endl
;
76
}
77
}
78
79
/** Clear queue */
80
void
clear
() {
queueFront
= NULL; }
81
82
private
:
83
node<Kind>
*
queueFront
;
/**< Pointer to the front of the queue */
84
node<Kind>
*
queueRear
;
/**< Pointer to the rear of the queue */
85
int
size;
86
};
87
88
#endif
// DATA_STRUCTURES_QUEUE_H_
std::cout
queue
Definition:
queue.h:17
queue::deQueue
void deQueue()
Definition:
queue.h:67
queue::queue
queue()
Definition:
queue.h:32
queue::display
void display()
Definition:
queue.h:20
queue::front
Kind front()
Definition:
queue.h:61
queue::enQueue
void enQueue(Kind item)
Definition:
queue.h:45
queue::clear
void clear()
Definition:
queue.h:80
queue::queueFront
node< Kind > * queueFront
Definition:
queue.h:83
queue::isEmptyQueue
bool isEmptyQueue()
Definition:
queue.h:42
queue::~queue
~queue()
Definition:
queue.h:39
queue::queueRear
node< Kind > * queueRear
Definition:
queue.h:84
std::endl
T endl(T... args)
node
Definition:
avltree.cpp:13
data_structures
queue.h
Generated by
1.9.3