Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: W. Douglas Lafield
- Editor By: Ilise Leary
- Queue.h
- */
- #ifndef _QUEUE_GUARD
- #define _QUEUE_GUARD 1
- #define QUEUE_MAX_NUM_OF_NODES 1000
- using namespace std;
- template <class T>
- class Queue
- {
- private:
- /* number of items currently in the queue */
- int length;
- /* You put the rest of the private variables here */
- /* Node structure */
- struct Node
- {
- T value;
- struct Node *nextNode;
- };
- Node *headNode, *tailNode, *tempNode; //Pointers to the first and last nodes in the list, and a temporary node for saving purposes
- public:
- /* constructer */
- Queue() {
- length = 0; //Set length to 0
- headNode = tailNode = tempNode = nullptr; //Set the headNode and tailNode to null
- }
- /* destructer */
- ~Queue() {
- while(headNode != nullptr){ //While the headNode is NOT null
- tempNode = headNode; //Save headNode into tempNode
- headNode = headNode->nextNode; //Make headNode point to the nextNode
- tempNode = nullptr;
- delete tempNode; //Delete the temporary node
- }
- }
- int getLength() { return length; }
- bool dequeue();
- /* remove item from the front of the queue and return the item */
- bool enqueue(T item);
- /* add new item to the end of the queue */
- T *front();
- /* return item at front of the queue without dequeueing */
- };
- /***************************************************/
- //**Stack with Linked List**//
- template <class T>
- bool Queue<T>::dequeue()
- { /* remove item from the front of the queue and return the item */
- if(getLength() == 0) { //If the length is 0
- cout << "Deqeue Failed" << endl;
- return false;//Make it fail because it's empty!
- }
- cout << "Decrementing" << endl;
- length--;//Decrement the length
- if(getLength() == 0) { //if the length becomes 0
- cout << "Removing" << endl;
- headNode = tailNode = nullptr; //Make the headNode and tailNode point to null
- delete headNode; //Delete headNode
- }else{//Otherwise,
- tempNode = headNode; //Save the headNode in tempNode
- headNode = tempNode->nextNode; //Make the headNode point to the nextNode
- tempNode = nullptr;
- delete tempNode; //Delete the tempNode
- }
- return true;
- } /* dequeue */
- /***************************************************/
- template <class T>
- bool Queue<T>::enqueue(T item)
- { /* add new item to the end of the queue */
- Node *newNode; //Create a blank node called newNode for item
- newNode->nextNode = nullptr; //Make the node next to newNode point to null
- if(getLength() == 0) { //If the length is 0
- headNode = tailNode = newNode; //Make the headNode and tailNode point to newNode
- }else{//Otherwise, then
- tailNode->nextNode = newNode; //Make the node next to the tailNode point to the newNode
- tailNode = newNode; //Make the tailNode point to newNode
- }
- length++; //Increment the length
- return true; //Done
- } /* enqueue */
- /***************************************************/
- template <class T>
- T *Queue<T>::front()
- { /* return item at front of the queue without dequeueing */
- return &headNode->value;//Return the node next to headNode
- } /* front */
- /***************************************************/
- /***************************************************/
- /***************************************************/
- /***************************************************/
- /***************************************************/
- /***************************************************/
- /***************************************************/
- /***************************************************/
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement