堆栈为空时抛出异常的问题......队列/堆栈实现
Problem with throw exceptions when stack are empty ..... Queue / stack implementation
我需要在两个堆栈都为空时抛出一个异常,但我不知道该怎么写。
我必须实现一个有 2 个堆栈的队列!
这是主要的
#include "QueueFromStacks.h"
int main()
{
/* THIS IS JUST TO SHOW YOU HOW #include <stack> WORKS
stack<int> st1;
stack<int> st2;
cout << "Size before push:" << st2.size() << "\n";
st2.push(2);
st2.push(5);
cout << "Size after two pushes:" << st2.size() << "\n";
cout << st2.top() << "\n";
st2.pop();
cout << "Size of st2 after one pop:" << st2.size() << "\n";
st1.push(st2.top());
st2.pop();
cout << "Size of st1:" <<st1.size()<< " Size of st2:"<< st2.size();
*/
QueueFromStacks<int> qfs;
qfs.QueueFromStacks();
qfs.enqueue(1);
qfs.enqueue(2);
qfs.enqueue(3);
qfs.dequeue();
cout << "Queue Front : " << (qfs.front())<< endl;
// You have to implement QueuefromStack
// The logic of the queue remains the same(FIFO) but you have to use the two stacks to store your elements
// In the main program create a queuefromstack object and use your implemented methods to clearly show us what u did
return 0;
}
头文件
#ifndef QUEUEFROMSTACKS_H_
#define QUEUEFROMSTACKS_H_
#include <iostream>
#include <stack>
#include <string>
using namespace std;
class QueueEmptyException{
public:
QueueEmptyException();
~QueueEmptyException();
string getMessage() { return "Queue is empty"; }
};
template <typename E>
class QueueFromStacks
{
public:
QueueFromStacks();
~QueueFromStacks();
int size() const;
bool empty() const;
const E& front() const throw(QueueEmptyException);
void enqueue (const E& e);
void dequeue() throw(QueueEmptyException);
private:
stack<E> st1;
stack<E> st2;
int numElements;
};
#endif /* QUEUEFROMSTACKS_H_ */
实施
#include "QueueFromStacks.h"
template <typename E>
QueueFromStacks<E>::QueueFromStacks()
{
numElements = 0;
}
template <typename E>
QueueFromStacks<E>::~QueueFromStacks()
{
// TODO Auto-generated destructor stub
}
template <typename E>
int QueueFromStacks<E>::size() const
{
return numElements;
}
template <typename E>
bool QueueFromStacks<E>::empty() const
{
return (size() == 0);
}
template <typename E>
const E& QueueFromStacks<E>::front() const
throw(QueueEmptyException)
{
return st2.top(); // don't forget to check for empty and throw exception if it is empty.
}
template <typename E>
void QueueFromStacks<E>::enqueue (const E& e)
{
st2.push(e);
numElements++;
}
template <typename E>
void QueueFromStacks<E>::dequeue()
throw(QueueEmptyException)
{
**// if both stacks are empty // here i dont know what should i put as a throw condition
if (st1.empty() && st2.empty())
{
throw;
}**
// if the second stack is empty, move elements from the first stack to it
if (st2.empty())
{
while (!st1.empty())
{
st2.push(st1.top());
st1.pop();
}
// or make a call to swap(s1, s2)
}
// return the top item from the second stack
int top = st2.top();
st2.pop();
numElements--;
}
你需要改变这个:
void QueueFromStacks<E>::enqueue (const E& e)
{
st2.push(e);
numElements++;
}
对此:
void QueueFromStacks<E>::enqueue (const E& e)
{
st1.push(e);
numElements++;
}
我需要在两个堆栈都为空时抛出一个异常,但我不知道该怎么写。
我必须实现一个有 2 个堆栈的队列!
这是主要的
#include "QueueFromStacks.h"
int main()
{
/* THIS IS JUST TO SHOW YOU HOW #include <stack> WORKS
stack<int> st1;
stack<int> st2;
cout << "Size before push:" << st2.size() << "\n";
st2.push(2);
st2.push(5);
cout << "Size after two pushes:" << st2.size() << "\n";
cout << st2.top() << "\n";
st2.pop();
cout << "Size of st2 after one pop:" << st2.size() << "\n";
st1.push(st2.top());
st2.pop();
cout << "Size of st1:" <<st1.size()<< " Size of st2:"<< st2.size();
*/
QueueFromStacks<int> qfs;
qfs.QueueFromStacks();
qfs.enqueue(1);
qfs.enqueue(2);
qfs.enqueue(3);
qfs.dequeue();
cout << "Queue Front : " << (qfs.front())<< endl;
// You have to implement QueuefromStack
// The logic of the queue remains the same(FIFO) but you have to use the two stacks to store your elements
// In the main program create a queuefromstack object and use your implemented methods to clearly show us what u did
return 0;
}
头文件
#ifndef QUEUEFROMSTACKS_H_
#define QUEUEFROMSTACKS_H_
#include <iostream>
#include <stack>
#include <string>
using namespace std;
class QueueEmptyException{
public:
QueueEmptyException();
~QueueEmptyException();
string getMessage() { return "Queue is empty"; }
};
template <typename E>
class QueueFromStacks
{
public:
QueueFromStacks();
~QueueFromStacks();
int size() const;
bool empty() const;
const E& front() const throw(QueueEmptyException);
void enqueue (const E& e);
void dequeue() throw(QueueEmptyException);
private:
stack<E> st1;
stack<E> st2;
int numElements;
};
#endif /* QUEUEFROMSTACKS_H_ */
实施
#include "QueueFromStacks.h"
template <typename E>
QueueFromStacks<E>::QueueFromStacks()
{
numElements = 0;
}
template <typename E>
QueueFromStacks<E>::~QueueFromStacks()
{
// TODO Auto-generated destructor stub
}
template <typename E>
int QueueFromStacks<E>::size() const
{
return numElements;
}
template <typename E>
bool QueueFromStacks<E>::empty() const
{
return (size() == 0);
}
template <typename E>
const E& QueueFromStacks<E>::front() const
throw(QueueEmptyException)
{
return st2.top(); // don't forget to check for empty and throw exception if it is empty.
}
template <typename E>
void QueueFromStacks<E>::enqueue (const E& e)
{
st2.push(e);
numElements++;
}
template <typename E>
void QueueFromStacks<E>::dequeue()
throw(QueueEmptyException)
{
**// if both stacks are empty // here i dont know what should i put as a throw condition
if (st1.empty() && st2.empty())
{
throw;
}**
// if the second stack is empty, move elements from the first stack to it
if (st2.empty())
{
while (!st1.empty())
{
st2.push(st1.top());
st1.pop();
}
// or make a call to swap(s1, s2)
}
// return the top item from the second stack
int top = st2.top();
st2.pop();
numElements--;
}
你需要改变这个:
void QueueFromStacks<E>::enqueue (const E& e)
{
st2.push(e);
numElements++;
}
对此:
void QueueFromStacks<E>::enqueue (const E& e)
{
st1.push(e);
numElements++;
}