Question: C++ Queue - Please check my code and see if im doing Queue correctly. Make any corrections and make sure it runs efficentely. I will

C++ Queue - Please check my code and see if im doing Queue correctly. Make any corrections and make sure it runs efficentely. I will rate thumbs up if you revise it!

I am new too Queue's so please be as thorough as you can! Ill be testing this program later. so i need to make sure its running correctly now.

/////////////////////////////////////////////////////////////////////// Main.cpp file /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include

#include "myqueue.h"
 
using namespace std;
using namespace myqueue;
 
int main()
{
 char data;
 myQueue queue(26);
 try
 {
 for(char i = 'A'; i <= 'Z'; ++i)
 queue< 
 while(!queue.empty())
 {
 queue>>data;
 cout< 
 }
 cout< 
 }
 catch(QUEUE_ERRORS e)
 {
 if(e)
 cout<<"Empty";
 else
 cout<<"Full";
 cout<<"queue thrown!"< 
 };
 
 return 0;
} 

/////////////////////////////////////////////////////////////////////////////////////////////////// myQueue.h file //////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef MYQUEUE_H

#define MYQUEUE_H
#include 
 
namespace myqueue
{
 enum QUEUE_ERRORS {QUEUE_FULL, QUEUE_EMPTY};
 
 template
 struct node
 {
 T data;
 node* next;
 
 node(const T& data)
 {
 this->data = data;
 next = NULL;
 }
 };
 
 template
 class myQueue
 {
 public:
 myQueue(size_t cap = 10);
 ~myQueue();
 myQueue(const myQueue &other);
 myQueue& operator=(const myQueue &other);
 
 myQueue& operator<<(const T &data);
 myQueue& operator>>(T &data);
 
 void enqueue(const T&data);
 T dequeue();
 T peek();
 bool full();
 bool empty();
 size_t size();
 void resize();
 void clear();
 
 private:
 node *head, *tail;
 size_t mySize, myCapacity;
 
 void copy(const myQueue &other);
 void nukem(node* &ptr);
 };
 
 
 template
 myQueue::myQueue(size_t cap)
 {
 myCapacity = cap;
 mySize = 0;
 head = tail = NULL;
 }
 
 template
 myQueue::~myQueue()
 {
 nukem(head);
 head = tail = NULL;
 mySize = myCapacity = 0;
 }
 
 template
 myQueue::myQueue(const myQueue &other)
 {
 copy(other);
 }
 
 template
 myQueue& myQueue::operator=(const myQueue &other)
 {
 if(this != &other)
 {
 nukem(other);
 copy(other);
 }
 return *this;
 }
 
 
 template
 myQueue& myQueue::operator<<(const T &data)
 {
 enqueue(data);
 }
 
 template
 myQueue& myQueue::operator>>(T &data)
 {
 data = dequeue();
 }
 
 
 template
 void myQueue::enqueue(const T&data)
 {
 if(full())
 throw QUEUE_FULL;
 if(empty())
 head = tail = new node(data);
 else
 {
 tail->next = new node(data);
 tail = tail->next;
 }
 ++mySize;
 }
 
 template
 T myQueue::dequeue()
 {
 if(empty())
 throw QUEUE_EMPTY;
 node *ptr = head;
 T data = head->data;
 head = head->next;
 delete ptr;
 --mySize;
 return data;
 }
 
 template
 T myQueue::peek()
 {
 if(empty())
 throw QUEUE_EMPTY;
 return head->data;
 }
 
 template
 bool myQueue::full()
 {
 return mySize == myCapacity;
 }
 
 template
 bool myQueue::empty()
 {
 return !mySize;
 }
 
 
 template
 size_t myQueue::size()
 {
 return mySize;
 }
 
 template
 void myQueue::resize()
 {
 
 }
 
 template
 void myQueue::clear()
 {
 nukem(head);
 head = tail = NULL;
 mySize = 0;
 }
 
 template
 void copy(const myQueue &other)
 {
 for(node *ptr = other.head; ptr; ptr = ptr->next)
 enqueue(ptr->data);
 }
 
 template
 void myQueue::nukem(node *&ptr)
 {
 if(ptr)
 nukem(ptr->next);
 ptr->data = T();
 delete ptr;
 }
 
};
#endif // MYQUEUE_H
 
 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!