Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions queue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ int main(void){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Queue<int>* q = new Queue<int>();
cout << "newest:" << q->frontEle() << endl;
cout << "oldest:" << q->backEle() << endl;
cout << "size: " << q->returnSize() << endl;
q->popEle();
q->PushEle(1);
q->PushEle(2);
q->PushEle(3);
cout << "newest:" << q->frontEle() << endl;
cout << "oldest:" << q->backEle() << endl;
cout << "size: " << q->returnSize() << endl;
q->printQueue();
q->popEle();
q->printQueue();
cout << "newest:" << q->frontEle() << endl;
cout << "oldest:" << q->backEle() << endl;
cout << "size: " << q->returnSize() << endl;
try
{
cout << "newest:" << q->frontEle() << endl;
cout << "oldest:" << q->backEle() << endl;
cout << "size: " << q->returnSize() << endl;
q->popEle();
q->PushEle(1);
q->PushEle(2);
q->PushEle(3);
cout << "newest:" << q->frontEle() << endl;
cout << "oldest:" << q->backEle() << endl;
cout << "size: " << q->returnSize() << endl;
q->printQueue();
q->popEle();
q->printQueue();
cout << "newest:" << q->frontEle() << endl;
cout << "oldest:" << q->backEle() << endl;
cout << "size: " << q->returnSize() << endl;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
}
28 changes: 14 additions & 14 deletions queue/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,13 @@ void Queue<T>::popEle(void){

template <typename T>
T Queue<T>::frontEle(void){
try{
if(size <= 0) throw "Queue is empty";
}
catch (const char* str){
cout << str << endl;
return EXIT_FAILURE;
}
if(size <= 0) throw QueueEmptyException();
return last->data;
}

template <typename T>
T Queue<T>::backEle(void){
try{
if(size <= 0) throw "Queue is empty";
}
catch (const char* str){
cout << str << endl;
return EXIT_FAILURE;
}
if(size <= 0) throw QueueEmptyException();
return this->next->data;
}

Expand All @@ -109,3 +97,15 @@ void Queue<T>::printQueue(void){
cout << endl;
return;
}



QueueEmptyException::QueueEmptyException()
: message("Queue is Empty"){}

QueueEmptyException::QueueEmptyException(string _m)
: message("" + _m){}

const char * QueueEmptyException::what() const throw(){
return message.c_str();
}
10 changes: 10 additions & 0 deletions queue/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ class Queue{
static Queue* last;
static int size;
};

class QueueEmptyException:public exception
{
private:
string message;
public:
QueueEmptyException();
QueueEmptyException(string);
virtual const char* what() const throw();
};