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 */
9template <class Kind>
10struct node {
11 Kind data;
12 node<Kind> *next;
13};
14
15/** Definition of the queue class */
16template <class Kind>
17class 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 }
28 std::cout << "Size of queue: " << size << std::endl;
29 }
30
31 /** Default constructor*/
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_
Definition: queue.h:17
void deQueue()
Definition: queue.h:67
queue()
Definition: queue.h:32
void display()
Definition: queue.h:20
Kind front()
Definition: queue.h:61
void enQueue(Kind item)
Definition: queue.h:45
void clear()
Definition: queue.h:80
node< Kind > * queueFront
Definition: queue.h:83
bool isEmptyQueue()
Definition: queue.h:42
~queue()
Definition: queue.h:39
node< Kind > * queueRear
Definition: queue.h:84
T endl(T... args)
Definition: avltree.cpp:13