【问题标题】:need help in queues insertion of 10 names在插入 10 个名字的队列中需要帮助
【发布时间】:2021-12-30 22:38:39
【问题描述】:

我刚刚开始使用 c++ 学习队列。我需要添加 10 个名称,显示它们,然后删除它们,然后重新显示名称,我尝试这样做,但我收到错误“操作员 = 不明确” .请帮助。 我不知道我哪里出错了。我知道我们应该添加 10 个名称,然后将它们传递给 insert() 那么我们应该删除队列中的字符串 然后在显示后它应该返回全 0

'''

template<class t>
class Queue
{
private:
int front;
int rear;
t arr[10];

public:
Queue()
{
    front = -1;
    rear = -1;
    for (int i = 0; i < 10; i++) 
    {
        arr[i] = 0;
    }
}
int isEmpty()
{
    if (front == -1 && rear == -1)
        return 1;
    else
        return 0;
}
int isFull() 
{
    if (rear == 9)
        return 1;
    else
        return 0;
}
void insertion(t value)
{
    if (isFull()==1)
    {
        cout << "Cant be inserted ,Queue full" << endl;
        
    }
    else if (isEmpty()==1) 
    {
        front = 0;
        rear = 0;
        arr[rear] = value;
    }
    else 
    {
        rear++;
        arr[rear] = value;
    }

}

t deletion()
{
    t x ;
    if (isEmpty()==1) 
    {
        cout << "Cant be deleted ,Queue is Empty" << endl;
        return x;
    }
    else if (rear == front) 
    {
        x = arr[rear];
        rear = -1;
        front = -1;
        return x;
    }
    else 
    {
        cout << "front value: " << front << endl;
        x = arr[front];
        arr[front] = 0;
        front++;
        return x;
    }
}

int count()
{
    return (rear - front + 1);
}

void display()
{
    cout << "All values in the Queue are - " << endl;
    for (int i = 0; i < 10; i++) 
    {
        cout << arr[i] << "  ";
    }
  }

  };

int main() {
Queue < string> q1;
int value, option;

string name;
cout << "Enter 10 names";
for (int i = 0; i < 10; i++)
{
    cin >> name;
    q1.insertion(name);

}
for (int i = 0; i < 10; i++)
{
    
    q1.deletion();

}
for (int i = 0; i < 10; i++)
{
    
    q1.insertion(name);

}

return 0;
}'''

【问题讨论】:

    标签: c++ queue dsa


    【解决方案1】:

    由于您正在概括队列,因此您不能将值初始化为 0。歧义来自编译器不知道 0 是指数值 0 还是等同于 0 的 ASCII(i '不确定我是否正确地表达了这一点,但事实就是如此)。

    您可以通过将其设置为 '\0' - 数组终止字符 - 而不是 0 来规避此问题。如果数组是数字的,它将评估为 0,如果数组是字母数字的,它将充当 NULL。我假设您的意图是初始化一个空数组,所以这可以解决问题。

    Queue(){
        front = -1;
        rear = -1;
        for (int i = 0; i < 10; i++){
            arr[i] = '\0';
        }
    }
    

    你也可以这样做而不是 for 循环,因为你的数组大小是恒定的:

    template<class t>
    class Queue{
        private:
        int front;
        int rear;
        t arr[10] = {'\0'};
    }
    Queue(){
        front = -1;
        rear = -1;
    }
    

    我觉得有一些建议是合理的:

    • 考虑使用向量而不是数组。通过使用向量,编译器不会抱怨 0 的事情,因为结构会处理它,并且您的类会变得更加通用
    • 如果您不想使用向量 - 必须尊重硬路由 - 请考虑使用 malloc 和 realloc 实现动态内存分配。虽然起初有点难以弄清楚,但这就是数组最终在这种类型的上下文中使用的方式。这允许您将数组大小设置为您想要的大小,根据需要增加和减少它(有点像向量;))
    • 考虑向构造函数添加一个大小参数。这将使您的课程更加通用,并允许您了解 STL 队列是如何实现的。您还可以完全减小大小并修改插入方法以根据需要分配更多内存

    这些类型的实现是很好的学习练习,总是会带来一堆奇怪的挑战和问题。希望回答对你有帮助!

    【讨论】:

    • 非常感谢您的回复!好的,所以我做了这些更改,我认为我的代码运行良好,我有一点疑问。在 int main 我做了这些改变
    • 'int main() { 队列 q1;整数值,选项;字符串名称; cout > name; q1.插入(名称); } for (int i = 0; i
    • 这里我的删除循环不起作用,因为当我在删除后显示名称时,它仍然显示奇怪的字母模式
    • @codeplayer 删除名称后,您必须更新数组终止字符 ('\0')。在显示时,您可以使用类似 ``` for(int i = 0; arr[i] != '\0'; i++) ``` 有更好的方法来写这个,但这个有效。它的作用是在循环找到终止符之前打印数组。这样您就不会打印出随机的垃圾值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多