【发布时间】:2018-02-10 02:13:49
【问题描述】:
我有一个删除字符串数组的第一个元素的代码。但是它删除了最后输入的元素。如何在不使用 STL 的情况下删除队列中的第一个元素以及如何将队列重置为空。这里是我的类声明为 Queue.h 文件,QueueImpl.h 作为其模板文件。您必须包含在 Queueimpl.h 文件中实现的所有函数,然后将其包含在 main 中。我已成功从队列中删除了该元素,我可以看到打印队列但搜索队列时,删除的元素仍然存在,为什么会这样
Queue.h
template <class Type>
class Queue
{
private:
int counter;
int Queue_size;
Type* Contents;
int Front, Back;
int items_in_queue = 0;
public:
Queue(int queue_size = 10);
~Queue();
bool Empty() const;
bool Full() const;
void Remove();
int Add(const Type& new_element);
int QueueSize();
Type front();
int search(string &element,int numElm);
void clear();
bool IsDigitsOnly(string &strn);
};
#endif
QueueImpl.h
#pragma once
#ifndef QUEUETEMPLATE_H
#define QUEUETEMPLATE_H
#include<string>
#include "queue.h"
const int MAX_SIZE = 10;
// Constructor
int counter = 0;
template<class Type>
Queue<Type>::Queue(int queue_size) :
Queue_size(queue_size),
Contents(new Type[queue_size + 1]),
Front(0), Back(0)
{}
// Destructor
template<class Type>
Queue<Type> :: ~Queue()
{
delete[] Contents;
}
// Tests
template<class Type>
void Queue<Type>::clear()
{
while (!Empty()) {
Front = Back = -1;
}
}
template<class Type>
bool Queue<Type>::Empty() const
{
return (Front == Back) ? true : false;
}
template<class Type>
bool Queue<Type>::Full() const
{
return ((1 + Back) % (Queue_size + 1) == Front) ? true : false;
}
/
}
#endif
【问题讨论】:
-
请通过完整的示例。你的这段代码是不可编译的,函数Remove声明无效。
-
请将模板文件存储在.h扩展名中并包含在main中
-
为什么文件底部有一个
e?此外,您的代码格式也很糟糕。 -
对不起,我现在改了...
-
这不会编译。发布一个完整的程序,而不是我们必须组装的 .h 文件和片段。阅读:minimal reproducible example