The next project on my list is once again a graduate school project. I am expanding the stack and queue by adding in a priority queue feature. This functionality helps expand the program into one that is much more useful.

The basic stack and queue works much like a Pez dispenser. The user is able to store information one number at a time in the queue. This stack of numbers is then stored in either the system memory or a database. The user can then recall each number one at a time. If the queue is set up using a first in, first out structure, the user will get back numbers in the order that they put them in. If the queue is set up using a last in, first out structure, the user will get back the last number they entered and the rest of the numbers in that reverse order.

To make the queue more useful, I have now added in a priority feature. This gives the user control over the order of the objects in the queue. Each queue item is now assigned a priority number. The higher the number of priority, the more important the item is. This allows the user to rank items in their list. The ranking feature allows the program to be more applicable to large scale projects. However, this feature does take a toll on resources. If a queue becomes very large, it would run better using a database call. Storing large queues directly in memory can create performance issues that would need to be avoided.

Introduction

This program lets the user create a priority queue and then interact with it.

How to run

Included is the main.cpp file of the program.  Load the file into any C++ complier to build and run the program.  There is also an executable that has already been compiled for the Windows operating system.

main.cpp

/*
*
* Priority Queue created by Richie Leahey.
*
* This program lets the user create a priority queue and then interact with it.
*
*/

#include "stdafx.h"
#include <iostream>
#include "priorityQueue.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[]){


	cout << "Welcome to the Priority Queue machine! " << endl << endl;

	priorityQueue run_q;

	run_q.run_queue();

	return(0);
}

priorityQueue.h

#pragma once

#include <iostream>

using namespace std;

#define MAX 100 // Defines the max amount of items in the array.

class priorityQueue{ // Create the class for the priority queue.
public:
	void enqueue(int);
	void dequeue(int);
	void create();
	void check(int);
	void display();
	void run_queue();
	int queue[MAX];
	int front, back;

};

priorityQueue.cpp

/*
*
* Priority Queue created by Richie Leahey.
*
* This program lets the user create a priority queue and then interact with it.
*
*/

#include "stdafx.h"
#include "priorityQueue.h"

void priorityQueue::create(){

	front = back = -1;

}

void priorityQueue::enqueue(int data){ // The function to enqueue numbers.  

	if (back >= MAX - 1){
		cout << endl;
		cout << "Queue overflow no more elements can be inserted!" << endl;
		return;
	}
	if ((front == -1) && (back == -1)){
		front++;
		back++;
		queue[back] = data;
		return;
	}
	else
		check(data);
	back++;
}

void priorityQueue::check(int data){ // Checks the item and places it in the correct slot.  Greatest to least.

	int i, j;

	for (i = 0; i <= back; i++){
		if (data >= queue[i]){
			for (j = back + 1; j > i; j--){
				queue[j] = queue[j - 1];
			}
			queue[i] = data;
			return;
		}
	}
	queue[i] = data;
}

void priorityQueue::dequeue(int data){ // The function to dequeue numbers.

	int i;

	if ((front == -1) && (back == -1)){
		cout << endl;
		cout << "Queue is empty no elements to delete!" << endl;
		return;
	}

	for (i = 0; i <= back; i++){
		if (data == queue[i]){
			for (; i < back; i++){
				queue[i] = queue[i + 1];
			}
			queue[i] = -99;
			back--;
			if (back == -1)
				front = -1;
			return;
		}
	}
	cout << endl << data << " not found in queue to delete!" << endl;
}

void priorityQueue::display(){ // The function to display the queue.

	if ((front == -1) && (back == -1)){
		cout << endl;
		cout << "Queue is empty!" << endl;
		return;
	}
	cout << endl << "The queue is: " << endl;
	for (NULL; front <= back; front++){
		cout << queue[front] << endl;
	}

	front = 0;
}

void priorityQueue::run_queue() // The loop that runs the queue.
{

	int n, choice;

	cout << "1. Enqueue " << endl;
	cout << "2. Dequeue " << endl;
	cout << "3. Display " << endl;
	cout << "4. Exit " << endl;

	priorityQueue::create();

	while (1){
		cout << endl;
		cout << "Chose a number: " << endl;
		cin >> choice;

		switch (choice){
		case 1:
			cout << endl;
			cout << "Enter value to enqueue: " << endl;
			cin >> n;
			priorityQueue::enqueue(n);
			break;
		case 2:
			cout << endl;
			cout << "Enter value to dequeue: " << endl;
			cin >> n;
			priorityQueue::dequeue(n);
			break;
		case 3:
			priorityQueue::display();
			break;
		case 4:
			exit(0);
		default:
			cout << endl;
			cout << "Please choose a number between 1-4. " << endl;
		}
	}

}