Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

LJU4801Frug suggests that the law encodes the female body by

Answered: 1 week ago

Question

1. Identify three approaches to culture.

Answered: 1 week ago

Question

2. Define communication.

Answered: 1 week ago