Circular Queue
/*
* pshrestha_a3.cpp
*
* Created on: Nov 16, 2010
* Program Description:
* A class is written that implements a "Circular Queue" data structure for storing
* and retrieving floating point values. The methods created are: Enqueue(), Dequeue(),
* isEmpty(), isFull(), makeEmpty(), goToHead().
* A switch statement is used in the main program for the efficiency of its application
* to handle the structured data.
*/
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
const int MAX_ELEMENTS = 17; // Declaring maximum number of elements.
struct Float_Node {
// Structure for the node containing a value and the next node.
double Value;
struct Float_Node* Next;
};
class CircularQueue {
private:
Float_Node *rear;
Float_Node *p; // Current
int nodeCounter; // To count the nodes.
public:
CircularQueue();
bool Enqueue(double);
double Dequeue(bool&);
bool isEmpty();
bool isFull();
void makeEmpty();
struct Float_Node *goToHead();
void display();
~CircularQueue();
};
CircularQueue::CircularQueue() {
// Default Constructor
nodeCounter = 0;
rear = NULL;
}
bool CircularQueue::Enqueue(double input) {
// This function will insert the value user enters in to the queue.
p = new struct Float_Node;
p->Value = input;
if(!isFull()) {
// If Queue is not full
if(rear) {
//If rare has value
p->Next = rear->Next;
rear->Next = p;
rear = p;
} else {
// If rare don't have a value already.
rear = p;
rear ->Next = p;
}
++ nodeCounter; // Counting.
return true;
}
return false;
}
double CircularQueue::Dequeue(bool& logicVal) {
// Takes the value out of the stack.
double Value;
if(!isEmpty()) {
if (rear) {
// if rear is true only.
Float_Node *p = rear->Next;
Value = p->Value;
if (p != rear) {
rear->Next = p->Next;
} else {
rear = NULL;
}
delete p;
--nodeCounter; //decreasing the node counter.
logicVal = true; // Setting true for successful dequeue process.
return Value;
}
}
logicVal = false; // Setting false for unsuccessful dequeue process.
return 8;
}
bool CircularQueue::isEmpty() {
// Checks if the queue is Empty or not.
if (rear) {
return false;
}
return true;
}
bool CircularQueue::isFull() {
// Checks if the queue is Full or not.
if (nodeCounter == MAX_ELEMENTS) {
return true;
}
return false;
}
void CircularQueue::makeEmpty() {
// Makes the Queue Empty.
double empty; // holds the variable that would be emptied from the queue.
if (rear) {
while (rear) {
p = rear->Next;
empty = p->Value;
if(p != rear) {
rear->Next = p->Next;
} else {
rear = NULL;
}
delete p;
-- nodeCounter;
cout << endl << "Queue --> Emptied is: " << empty << endl;
}
} else {
cout << endl << "Queue --> Empty!!" << endl;
}
}
struct Float_Node *CircularQueue::goToHead() {
// Positions the Queue to the head of the Queue.
return rear->Next;
}
void CircularQueue::display() {
// Function to display the Queue for the users.
double input;
if (rear) {
struct Float_Node *p = goToHead();
// Positions the Queue to the top so that we can display the queue.
cout << endl << "Displaying Queues: " << endl;
while (p != rear) {
input = p->Value;
p = p->Next;
cout << input << endl;
}
cout << rear->Value << endl;
} else {
cout << "Queue --> Empty!!" << endl;
}
}
CircularQueue::~CircularQueue() {
// De-allocating the memory space used that could be freed!!
while(rear) {
p = rear->Next;
if (p != rear) {
rear->Next = p->Next;
} else {
rear = NULL;
}
delete p;
}
}
int main (void) {
CircularQueue q1; // Instance of the class CircularQueue.
int option; // Variable to hold the user option from 1 - 6.
float inputValue; // Variable to hold user's input value for queue operation.
bool logicValue;
// Boolean variable to figure out if the Dequeue operation was successful or not.
while(1) {
// Using switch statement which seems to be ideally very efficient to
// handle data structures.
cout << "\n\n--------------" << endl;
cout << "CIRCULAR QUEUE" << endl;
cout << "--------------" << endl;
cout << "[1] Enqueue." << endl;
cout << "[2] Dequeue." << endl;
cout << "[3] Empty Queue." << endl;
cout << "[4] Display Queue." << endl;
cout << "[5] Go to Head of Queue." << endl;
cout << "[6] Done. Quit me out!!" << endl;
cout << "Enter your Choice: ";
cin >> option;
switch(option) {
case 1:
// If users hits 1
cout << "Enter the value to enqueued in the Queue: \n";
// Asking the user for input to be inserted in the Queue.
cin >> inputValue;
// Storing in variable.
logicValue = q1.Enqueue(inputValue);
if (logicValue) {
// If logic value is true.
cout << "Value Enqueued: " << inputValue << endl;
} else {
// If logic value is false.
cout << "Stack Full!! Value not Enqueued!! \n";
}
break;
case 2:
// If users hits 2
double val;
val = q1.Dequeue(logicValue);
if (logicValue) {
// If logic value is true.
cout << "Value Dequeued: " << val << endl;
} else {
// If logic value is false.
cout << "Queue --> Empty!! \n";
}
break;
case 3:
// If users hits 3
q1.makeEmpty(); // Calling function/method.
break;
case 4:
// If users hits 4
q1.display(); //Calling function/method.
break;
case 5:
// If users hits 5
q1.goToHead(); //Calling function/method.
// It may not be used directly but is helpful when displaying the Queue.
break;
case 6:
// If users hits 6
cout << endl << "Closing.......Application!!" << endl << "Thank You!!";
return 0;
default:
// If users hits any other permutations or combinations.
cout << endl <<".....Enter the Option in between 1 & 6!! Retry Again. \n";
}
}
return 0;
}
* pshrestha_a3.cpp
*
* Created on: Nov 16, 2010
* Program Description:
* A class is written that implements a "Circular Queue" data structure for storing
* and retrieving floating point values. The methods created are: Enqueue(), Dequeue(),
* isEmpty(), isFull(), makeEmpty(), goToHead().
* A switch statement is used in the main program for the efficiency of its application
* to handle the structured data.
*/
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
const int MAX_ELEMENTS = 17; // Declaring maximum number of elements.
struct Float_Node {
// Structure for the node containing a value and the next node.
double Value;
struct Float_Node* Next;
};
class CircularQueue {
private:
Float_Node *rear;
Float_Node *p; // Current
int nodeCounter; // To count the nodes.
public:
CircularQueue();
bool Enqueue(double);
double Dequeue(bool&);
bool isEmpty();
bool isFull();
void makeEmpty();
struct Float_Node *goToHead();
void display();
~CircularQueue();
};
CircularQueue::CircularQueue() {
// Default Constructor
nodeCounter = 0;
rear = NULL;
}
bool CircularQueue::Enqueue(double input) {
// This function will insert the value user enters in to the queue.
p = new struct Float_Node;
p->Value = input;
if(!isFull()) {
// If Queue is not full
if(rear) {
//If rare has value
p->Next = rear->Next;
rear->Next = p;
rear = p;
} else {
// If rare don't have a value already.
rear = p;
rear ->Next = p;
}
++ nodeCounter; // Counting.
return true;
}
return false;
}
double CircularQueue::Dequeue(bool& logicVal) {
// Takes the value out of the stack.
double Value;
if(!isEmpty()) {
if (rear) {
// if rear is true only.
Float_Node *p = rear->Next;
Value = p->Value;
if (p != rear) {
rear->Next = p->Next;
} else {
rear = NULL;
}
delete p;
--nodeCounter; //decreasing the node counter.
logicVal = true; // Setting true for successful dequeue process.
return Value;
}
}
logicVal = false; // Setting false for unsuccessful dequeue process.
return 8;
}
bool CircularQueue::isEmpty() {
// Checks if the queue is Empty or not.
if (rear) {
return false;
}
return true;
}
bool CircularQueue::isFull() {
// Checks if the queue is Full or not.
if (nodeCounter == MAX_ELEMENTS) {
return true;
}
return false;
}
void CircularQueue::makeEmpty() {
// Makes the Queue Empty.
double empty; // holds the variable that would be emptied from the queue.
if (rear) {
while (rear) {
p = rear->Next;
empty = p->Value;
if(p != rear) {
rear->Next = p->Next;
} else {
rear = NULL;
}
delete p;
-- nodeCounter;
cout << endl << "Queue --> Emptied is: " << empty << endl;
}
} else {
cout << endl << "Queue --> Empty!!" << endl;
}
}
struct Float_Node *CircularQueue::goToHead() {
// Positions the Queue to the head of the Queue.
return rear->Next;
}
void CircularQueue::display() {
// Function to display the Queue for the users.
double input;
if (rear) {
struct Float_Node *p = goToHead();
// Positions the Queue to the top so that we can display the queue.
cout << endl << "Displaying Queues: " << endl;
while (p != rear) {
input = p->Value;
p = p->Next;
cout << input << endl;
}
cout << rear->Value << endl;
} else {
cout << "Queue --> Empty!!" << endl;
}
}
CircularQueue::~CircularQueue() {
// De-allocating the memory space used that could be freed!!
while(rear) {
p = rear->Next;
if (p != rear) {
rear->Next = p->Next;
} else {
rear = NULL;
}
delete p;
}
}
int main (void) {
CircularQueue q1; // Instance of the class CircularQueue.
int option; // Variable to hold the user option from 1 - 6.
float inputValue; // Variable to hold user's input value for queue operation.
bool logicValue;
// Boolean variable to figure out if the Dequeue operation was successful or not.
while(1) {
// Using switch statement which seems to be ideally very efficient to
// handle data structures.
cout << "\n\n--------------" << endl;
cout << "CIRCULAR QUEUE" << endl;
cout << "--------------" << endl;
cout << "[1] Enqueue." << endl;
cout << "[2] Dequeue." << endl;
cout << "[3] Empty Queue." << endl;
cout << "[4] Display Queue." << endl;
cout << "[5] Go to Head of Queue." << endl;
cout << "[6] Done. Quit me out!!" << endl;
cout << "Enter your Choice: ";
cin >> option;
switch(option) {
case 1:
// If users hits 1
cout << "Enter the value to enqueued in the Queue: \n";
// Asking the user for input to be inserted in the Queue.
cin >> inputValue;
// Storing in variable.
logicValue = q1.Enqueue(inputValue);
if (logicValue) {
// If logic value is true.
cout << "Value Enqueued: " << inputValue << endl;
} else {
// If logic value is false.
cout << "Stack Full!! Value not Enqueued!! \n";
}
break;
case 2:
// If users hits 2
double val;
val = q1.Dequeue(logicValue);
if (logicValue) {
// If logic value is true.
cout << "Value Dequeued: " << val << endl;
} else {
// If logic value is false.
cout << "Queue --> Empty!! \n";
}
break;
case 3:
// If users hits 3
q1.makeEmpty(); // Calling function/method.
break;
case 4:
// If users hits 4
q1.display(); //Calling function/method.
break;
case 5:
// If users hits 5
q1.goToHead(); //Calling function/method.
// It may not be used directly but is helpful when displaying the Queue.
break;
case 6:
// If users hits 6
cout << endl << "Closing.......Application!!" << endl << "Thank You!!";
return 0;
default:
// If users hits any other permutations or combinations.
cout << endl <<".....Enter the Option in between 1 & 6!! Retry Again. \n";
}
}
return 0;
}
Comments
Post a Comment