我正在使用两个堆栈实现一个队列。整个代码基于模板,以支持多种数据类型。
#include <iostream>
#define STACKSIZE 100
template <class T>
class Stack
{
private:
int top;
T *nodes;
public:
Stack();
T gettop();
bool empty();
void push(T&);
T pop();
~Stack();
};
template <class T>Stack<T>::Stack()
{
top=-1;
nodes=new T[STACKSIZE];
}
template <class T>Stack<T>::~Stack()
{
delete nodes;
}
template <class T> bool Stack<T>::empty()
{
return(top<0);
}
template <class T> void Stack<T>::push(T&j)
{
if(top==STACKSIZE-1)
{
std::cout<<"Stack Overflow"<<std::endl;
return;
}
nodes[++top]=j;
}
template <class T>T Stack<T>::pop()
{
T p;
if (empty())
{
std::cout<<"Stack Underflow"<<std::endl;
return p;
}
p=nodes[top--];
return p;
}
template <class T>T Stack<T>::gettop()
{
return this->top;
}
template <class T>
class Queue
{
Stack<T>s1;
Stack<T>s2;
public:
void insert_into_queue(T data)
{
s1.push(data);
std::cout<<"The value: "<<data<<" has been inserted into the Queue"<<std::endl;
}
void delete_from_queue()
{
if(s1.empty())
{
std::cout<<"The Stack S1 is Empty which implies Queue is Empty, nothing to delete"<<std::endl;
return;
}
while(!s1.empty())
{
T top=s1.gettop();
s1.pop();
s2.push(top);
}
T d_val;
d_val=s2.gettop();
s2.pop();
std::cout<<"The value: "<<d_val<<" has been deleted from the Queue"<<std::endl;
while(!s2.empty())
{
T top=s2.gettop();
s2.pop();
s1.push(top);
}
}
};
int main()
{
char choice;
int option;
int val;
while(1)
{
std::cout<<"Provide the choice of DATA to work on: I for Integer, F for float, C for Character"<<std::endl;
std::cin>>choice;
if (choice == 'I' || choice == 'i')
{
Queue<int> q;
break;
}
else if (choice == 'F' || choice == 'f')
{
Queue<float> q;
break;
}
else if (choice == 'C' || choice == 'c')
{
Queue<char> q;
break;
}
else
{
std::cout << "Not a valid choice" << std::endl;
}
}
while(1)
{
std::cout<<"#######################################################################################"<<std::endl;
std::cout<<"Press 1 to Insert a value into Queue: "<<std::endl;
std::cout<<"Press 2 to Delete a value into Queue: "<<std::endl;
std::cout<<"Press 3 to Exit the Program"<<std::endl;
std::cin>>option;
switch(option)
{
case 1:
std::cout<<"Insert the value: "<<std::endl;
std::cin>>val;
q.insert_into_queue(val);
break;
case 2:
q.delete_from_queue();
break;
case 3:
exit(0);
break;
default:
std::cout<<"Selected Option is Invalid"<<std::endl;
}
}
return 0;
}
我的问题是我想从用户那里选择一个选项来指定队列的数据类型,直到用户为队列值提供正确的输入为止。
然后,我希望用户将数据插入队列或从队列中删除数据或从程序中退出。
我试图在main()函数内部使用两个无尽的while(1)循环拉动它们,但是当我这样做时,我松开了q的范围。
请建议对代码进行适当的修改(或修改代码以实现所需的功能)。
这个问题与在以下意义上提到的其他问题不同
Stack<T>s1;
Stack<T>s2;
和方法
void insert_into_queue(T data)
{
s1.push(data);
std::cout<<"The value: "<<data<<" has been inserted into the Queue"<<std::endl;
}
void delete_from_queue()
{
if(s1.empty())
{
std::cout<<"The Stack S1 is Empty which implies Queue is Empty, nothing to delete"<<std::endl;
return;
}
while(!s1.empty())
{
T top=s1.gettop();
s1.pop();
s2.push(top);
}
T d_val;
d_val=s2.gettop();
s2.pop();
std::cout<<"The value: "<<d_val<<" has been deleted from the Queue"<<std::endl;
while(!s2.empty())
{
T top=s2.gettop();
s2.pop();
s1.push(top);
}
}
这完全取决于模板。
另外,如果条件满足,q会在本地定义
if (choice == 'I' || choice == 'i')
{
Queue<int> q;
break;
}
现在如何解决q的范围
case 1:
std::cout<<"Insert the value: "<<std::endl;
std::cin>>val;
q.insert_into_queue(val);
break;
可以实现。
C ++不是动态类型的。因此,您不能根据某些运行时条件(例如用户输入)来更改类型。无论您声明什么,这就是它。
处理此问题的最简单方法是仅创建3个队列。然后,根据用户选择的类型,将值插入三个队列之一。
Queue<int> q1;
Queue<float> q2;
Queue<char> q3;
/* ... */
if (choice == 'I' || choice == 'i') {
q1.insert_into_queue(...);
}
如果您的任务或挑战仅需要一个对象,则可以使用std::variant
一个可以被不同类型的队列占用的空间。
#include <variant>
std::variant<Queue<int>, Queue<float>, Queue<char>> q;
/* ... */
if (choice == 'I' || choice == 'i') {
q = Queue<int> {};
}
/* ... */
if (choice == 'I' || choice == 'i') {
std::get<Queue<int>>(q).insert_into_queue(...);
}
如果只重复一些代码,也可以避免这种幻想:
if (choice == 'I' || choice == 'i') {
Queue<int> q;
while (1) {
/* ... input loop */
}
}
甚至将重复的代码包装在模板中(whelp,现在又花哨了):
if (choice == 'I' || choice == 'i') {
Queue<int> q;
while (1) {
input_loop<int>(q); // implementation left as exercise
}
}
因此,您知道有很多方法可以解决您的问题(但我建议最简单的方法)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句