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;
myQueuequeue(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";elsecout<<"Full";cout<<"queue thrown!"< };return 0;}/////////////////////////////////////////////////////////////////////////////////////////////////// myQueue.h file //////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef MYQUEUE_H
#define MYQUEUE_H#includenamespace myqueue{enum QUEUE_ERRORS {QUEUE_FULL, QUEUE_EMPTY};templatestruct node{T data;node* next;node(const T& data){this->data = data;next = NULL;}};templateclass 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); };templatemyQueue::myQueue(size_t cap) {myCapacity = cap;mySize = 0;head = tail = NULL;}templatemyQueue::~myQueue() {nukem(head);head = tail = NULL;mySize = myCapacity = 0;}templatemyQueue::myQueue(const myQueue &other) {copy(other);}templatemyQueue& myQueue ::operator=(const myQueue &other) {if(this != &other){nukem(other);copy(other);}return *this;}templatemyQueue& myQueue ::operator<<(const T &data) {enqueue(data);}templatemyQueue& myQueue ::operator>>(T &data) {data = dequeue();}templatevoid 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;}templateT myQueue::dequeue() {if(empty())throw QUEUE_EMPTY;node*ptr = head; T data = head->data;head = head->next;delete ptr;--mySize;return data;}templateT myQueue::peek() {if(empty())throw QUEUE_EMPTY;return head->data;}templatebool myQueue::full() {return mySize == myCapacity;}templatebool myQueue::empty() {return !mySize;}templatesize_t myQueue::size() {return mySize;}templatevoid myQueue::resize() {}templatevoid myQueue::clear() {nukem(head);head = tail = NULL;mySize = 0;}templatevoid copy(const myQueue&other) {for(node*ptr = other.head; ptr; ptr = ptr->next) enqueue(ptr->data);}templatevoid 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started