C++ 链表队列
C++ Queue with Linked-list
问题:
struct QueueNode {
int data;
QueueNode *next;
};
做
- int size() 常量:return数据大小。
- bool is_empty() const:
- void enqueue(int val): 添加一个新节点到列表的末尾。
- void dequeue(): 移除head指向的节点。
- int top() const: return接下来要出队的数据。
这是我的代码
class Queue {
private:
QueueNode *_head = NULL, *_tail = NULL;
int _size = 0;
public:
int size() const {
if (! is_empty())
return _size;
}
bool is_empty() const {
if (_size == 0)
return true;
else
return false;
}
void enqueue(int val) {
QueueNode *n = new QueueNode;
n -> data = val;
n -> next = NULL;
if (is_empty()) {
_head = n;
_tail = n;
_size ++;
}
else {
_tail -> next = n;
_tail = n;
_size ++;
}
}
void dequeue() {
QueueNode *temp;
temp = _head;
if (! is_empty()) {
_head = _head -> next;
delete temp;
_size --;
}
}
int top() const {
if (! is_empty())
return _head -> data;
}
};
在线裁判显示错误答案。
我认为“int top() const”是错误的。
但我不知道。
请求帮忙。
谢谢
正如@Kaylum 所指出的,如果队列为空,您还没有return编辑队列的大小。在那种情况下它应该 return 0,但是你的 size()
方法中没有其他部分。
这应该可行:
int size() const {
if (! is_empty())
return _size;
else
return 0;
}
编辑:同样,您也应该 return 来自 top()
的内容。如果您使用的是在线判断,那么它会指定 return 在空队列的情况下要做什么。请再次阅读约束条件,我想这会有所帮助。它很可能是一些异常或一些默认整数,在线法官需要判断一个空队列的输出。
我的回答。谢谢大家。
class Queue {
private:
QueueNode *_head = NULL, *_tail = NULL;
int _size = 0;
public:
int size() const {
if (! is_empty())
return _size;
else
return 0;
}
bool is_empty() const {
if (_size == 0)
return true;
else
return false;
}
void enqueue(int val) {
QueueNode *n = new QueueNode;
n -> data = val;
n -> next = NULL;
if (is_empty()) {
_head = _tail = n;
_size ++;
}
else {
_tail -> next = n;
_tail = n;
_size ++;
}
}
void dequeue() {
QueueNode *temp;
temp = _head;
if (! is_empty()) {
_head = _head -> next;
delete temp;
_size --;
}
}
int top() const {
if (! is_empty())
return _head -> data;
}
};
问题:
struct QueueNode {
int data;
QueueNode *next;
};
做
- int size() 常量:return数据大小。
- bool is_empty() const:
- void enqueue(int val): 添加一个新节点到列表的末尾。
- void dequeue(): 移除head指向的节点。
- int top() const: return接下来要出队的数据。
这是我的代码
class Queue {
private:
QueueNode *_head = NULL, *_tail = NULL;
int _size = 0;
public:
int size() const {
if (! is_empty())
return _size;
}
bool is_empty() const {
if (_size == 0)
return true;
else
return false;
}
void enqueue(int val) {
QueueNode *n = new QueueNode;
n -> data = val;
n -> next = NULL;
if (is_empty()) {
_head = n;
_tail = n;
_size ++;
}
else {
_tail -> next = n;
_tail = n;
_size ++;
}
}
void dequeue() {
QueueNode *temp;
temp = _head;
if (! is_empty()) {
_head = _head -> next;
delete temp;
_size --;
}
}
int top() const {
if (! is_empty())
return _head -> data;
}
};
在线裁判显示错误答案。 我认为“int top() const”是错误的。 但我不知道。 请求帮忙。 谢谢
正如@Kaylum 所指出的,如果队列为空,您还没有return编辑队列的大小。在那种情况下它应该 return 0,但是你的 size()
方法中没有其他部分。
这应该可行:
int size() const {
if (! is_empty())
return _size;
else
return 0;
}
编辑:同样,您也应该 return 来自 top()
的内容。如果您使用的是在线判断,那么它会指定 return 在空队列的情况下要做什么。请再次阅读约束条件,我想这会有所帮助。它很可能是一些异常或一些默认整数,在线法官需要判断一个空队列的输出。
我的回答。谢谢大家。
class Queue {
private:
QueueNode *_head = NULL, *_tail = NULL;
int _size = 0;
public:
int size() const {
if (! is_empty())
return _size;
else
return 0;
}
bool is_empty() const {
if (_size == 0)
return true;
else
return false;
}
void enqueue(int val) {
QueueNode *n = new QueueNode;
n -> data = val;
n -> next = NULL;
if (is_empty()) {
_head = _tail = n;
_size ++;
}
else {
_tail -> next = n;
_tail = n;
_size ++;
}
}
void dequeue() {
QueueNode *temp;
temp = _head;
if (! is_empty()) {
_head = _head -> next;
delete temp;
_size --;
}
}
int top() const {
if (! is_empty())
return _head -> data;
}
};