This implementation was done on windows with DEV-C++ 5.11 and complied against MinGW (4.9.3), it contains the following files:

LinkListQueue.h, LinkListQueue.cpp, QueueNode.h, QueueNode.cpp, main.h, main.cpp


The program generates 100 random numbers while adding it to a queue then it outputs these values FIFO style while removing elements from the queue. The output  is shown below.

linklist_queue_output


Download source code here -> Queue(source)

 

 

LinkListQueue.h

#include"QueueNode.h"

#ifndef _LinkListQueue_class_included
#define _LinkListQueue_class_included
class LinkListQueue{
	private:
		QueueNode* frontNode;	
		QueueNode* backNode;	
	public:
		QueueNode* enQueue(QueueNode* node);
		QueueNode* deQueue();
		QueueNode* getFrontNode();
		QueueNode* getBackNode();
		LinkListQueue();
		
	
};


#endif<span id="mce_marker" data-mce-type="bookmark"></span>

LinkListQueue.cpp

#include "main.h"


//constructor
LinkListQueue::LinkListQueue(){
	frontNode = NULL;
	backNode = NULL;
}
//enqueue implementation
QueueNode* LinkListQueue::enQueue(QueueNode* node){
	if(frontNode == NULL){//Queue is empty	
		node->next = NULL;
		node->prev = NULL;
		frontNode = node;
		backNode = node;
	} else {// add node to end of the queue
		node->prev = NULL;
		node->next = backNode;
		backNode->prev = node;
		backNode = node;
	}
	return backNode;
}

//dequeue implementation
QueueNode* LinkListQueue::deQueue(){
	QueueNode* node = NULL;
	if(frontNode == NULL){// Queue is empty
		return NULL;
	} else {
		node = frontNode;
		frontNode = frontNode->prev;
		if(frontNode!=NULL) frontNode->next = NULL;
	}
	return node;
}

//get first node in queue
QueueNode* LinkListQueue::getFrontNode(){
	return frontNode;	
}

//get last node in queue
QueueNode* LinkListQueue::getBackNode(){
	return backNode;
}

QueueNode.h

#ifndef _QueueNode_class_included
#define _QueueNode_class_included


class QueueNode{
	public:
		int data;
		QueueNode* next;
		QueueNode* prev;
		QueueNode();
	
};





#endif

QueueNode.cpp

#include "main.h"
//QueueNode Constructor
QueueNode::QueueNode(){
	next = NULL;
	prev = NULL;
}

main.h

#include <stddef.h>
#include <stdlib.h>     // srand, rand
#include <time.h>       // time
#include <iostream>
#include "LinkListQueue.h"<span id="mce_marker" data-mce-type="bookmark"></span>

main.cpp

#include "main.h"

using namespace std;
int main(int argc, char** argv) {
	
  	srand (time(NULL));//initialize random seed:
	
	LinkListQueue* queue = new LinkListQueue();
	QueueNode* tmpNode = NULL;
	int randomNumber = -1;
	
	/*Fill The Queue*/
	cout <<"Enqueue 100 random numbers(0-99)..."<<endl;
	for(int i=100; i>0 ;i--){// count backwards from 100 to 0
		randomNumber = rand() % 100;
		cout <<randomNumber<<"\t";
		tmpNode = new QueueNode();
		tmpNode->data = randomNumber;
		queue->enQueue(tmpNode);
	}
	
	/*Empty the queue*/
	cout <<endl<<endl<<"Dequeue the queue until it is empty..."<<endl;
	do{
		
		tmpNode = queue->deQueue();
		if(tmpNode != NULL)
			cout <<tmpNode->data<<"\t";
	}while(tmpNode != NULL);//while there are items in the queue
	
	
	cout <<endl<<"Press ENTER to exit.";
	cin.get();//Hold screen;	
	
	return 0;
}

 

 

Download source code here -> Queue(source)

Leave a comment

Your email address will not be published. Required fields are marked *