解释下面代码: //优先队列实现 void push(const T& val) { if (size >= capacity) { return; } data[size++] = val; int i = size - 1; while (i > 0) { int parent = (i - 1) / 2; if (data[i]->freq < data[parent]->freq) { swap(i, parent); i = parent; } else { break; } } } T top() const { if (size > 0) { return data[0]; } else { return nullptr; } } void pop() { if (size <= 0) { return; } data[0] = data[--size]; int i = 0; while (i < size) { int left = i * 2 + 1; int right = i * 2 + 2; int smallest = i; if (left < size && data[left]->freq < data[smallest]->freq) { smallest = left; } if (right < size && data[right]->freq < data[smallest]->freq) { smallest = right; } if (smallest != i) { swap(i, smallest); i = smallest; } else { break; } } }
时间: 2024-02-10 15:12:30 浏览: 29
这段代码实现了一个优先队列,其中 push() 方法用于将元素插入队列,top() 方法用于返回队列中最小的元素,pop() 方法用于将队列中最小的元素弹出。这个优先队列是使用数组来实现的,数组中存储的是元素的指针。在 push() 方法中,首先判断队列是否已满,如果已满则直接返回。如果队列未满,则将元素插入到数组的最后,然后通过循环将元素上移,直到满足堆的性质,即父节点的值小于等于子节点的值。在 top() 方法中,如果队列非空,则返回数组中第一个元素的指针,即最小的元素。如果队列为空,则返回空指针。在 pop() 方法中,首先判断队列是否为空,如果为空则直接返回。如果队列非空,则将数组中最后一个元素移动到第一个位置,并通过循环将元素下移,直到满足堆的性质。具体来说,每次将当前节点与其左右子节点中最小的节点进行比较,如果当前节点比左右子节点中最小的节点还要小,则将当前节点与左右子节点中最小的节点交换位置,然后将当前节点指向交换后的子节点,继续循环。如果当前节点比左右子节点中最小的节点还要大,则说明已经满足堆的性质,可以退出循环。
相关问题
填充下面这个程序中所有出现// TODO: fill the code这个任务的地方#include <iostream> #include <cstring> #include "ourstring.h" #include "strlib.h" using namespace std; OurString::OurString(){ // TODO: fill the code } OurString::OurString(const char *str){ // TODO: fill the code } OurString::OurString(const OurString &dstr){ // TODO: fill the code } OurString::~OurString(){ // TODO: fill the code } string OurString::toString() const{ // TODO: fill the code } OurString OurString::subStr(unsigned int start, unsigned int n) const{ // TODO: fill the code } bool OurString::operator > (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator < (const OurString &dstr) const{ // TODO: fill the code } bool OurString::operator == (const OurString &dstr) const{ // TODO: fill the code } unsigned int OurString::length () const{ // TODO: fill the code } const OurString& OurString::operator = (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator = (const char *str){ // TODO: fill the code } char& OurString::operator[](int index){ // TODO: fill the code } const OurString OurString::operator + (const OurString &dstr) const{ // TODO: fill the code } const OurString OurString::operator + (const char *str) const{ // TODO: fill the code } const OurString& OurString::operator += (const OurString &dstr){ // TODO: fill the code } const OurString& OurString::operator += (const char *str){ // TODO: fill the code } ostream & operator<<(ostream &os, const OurString &dstr){ // TODO: fill the code } istream & operator>>(istream &is, OurString &dstr){ // TODO: fill the code }
好的,下面是填充后的代码,每个 TODO 已经被替换成相应的实现:
```c++
#include <iostream>
#include <cstring>
#include "ourstring.h"
#include "strlib.h"
using namespace std;
OurString::OurString(){
str = new char[1];
str[0] = '\0';
}
OurString::OurString(const char *str){
this->str = new char[strlen(str)+1];
strcpy(this->str, str);
}
OurString::OurString(const OurString &dstr){
this->str = new char[dstr.length()+1];
strcpy(this->str, dstr.str);
}
OurString::~OurString(){
delete [] str;
}
string OurString::toString() const{
return string(str);
}
OurString OurString::subStr(unsigned int start, unsigned int n) const{
if (start >= length()) return OurString();
n = (n > length() - start) ? length() - start : n;
return OurString(str + start, str + start + n);
}
bool OurString::operator > (const OurString &dstr) const{
return strcmp(str, dstr.str) > 0;
}
bool OurString::operator < (const OurString &dstr) const{
return strcmp(str, dstr.str) < 0;
}
bool OurString::operator == (const OurString &dstr) const{
return strcmp(str, dstr.str) == 0;
}
unsigned int OurString::length () const{
return strlen(str);
}
const OurString& OurString::operator = (const OurString &dstr){
if (this != &dstr) {
delete [] str;
str = new char[dstr.length()+1];
strcpy(str, dstr.str);
}
return *this;
}
const OurString& OurString::operator = (const char *str){
if (this->str != str) {
delete [] this->str;
this->str = new char[strlen(str)+1];
strcpy(this->str, str);
}
return *this;
}
char& OurString::operator[](int index){
return str[index];
}
const OurString OurString::operator + (const OurString &dstr) const{
OurString s;
s.str = new char[length() + dstr.length() + 1];
strcpy(s.str, str);
strcat(s.str, dstr.str);
return s;
}
const OurString OurString::operator + (const char *str) const{
OurString s;
s.str = new char[length() + strlen(str) + 1];
strcpy(s.str, this->str);
strcat(s.str, str);
return s;
}
const OurString& OurString::operator += (const OurString &dstr){
*this = *this + dstr;
return *this;
}
const OurString& OurString::operator += (const char *str){
*this = *this + str;
return *this;
}
ostream & operator<<(ostream &os, const OurString &dstr){
os << dstr.str;
return os;
}
istream & operator>>(istream &is, OurString &dstr){
string s;
is >> s;
dstr = s.c_str();
return is;
}
```
template<class T> class Stack { public: bool isEmpty() const; void push(const T& value); T pop(); T peak() const; private: LinkedList data; }; template<class T> bool Stack::isEmpty() const { return data.isEmpty(); } template<class T> void Stack::push(const T& value) { data.addLast(value); } template<class T> T Stack::pop() { T temp = data.getLast(); data.removeLast(); return temp; } template<class T> T Stack::peak() const { return data.getLast(); }修复代码
修复后的代码如下:
```
template<class T>
class Stack {
public:
bool isEmpty() const;
void push(const T& value);
T pop();
T peak() const;
private:
LinkedList<T> data; // 修改为 LinkedList<T>
};
template<class T>
bool Stack<T>::isEmpty() const {
return data.isEmpty();
}
template<class T>
void Stack<T>::push(const T& value) {
data.addLast(value);
}
template<class T>
T Stack<T>::pop() {
T temp = data.getLast();
data.removeLast();
return temp;
}
template<class T>
T Stack<T>::peak() const {
return data.getLast();
}
```
修复的主要问题是在类定义中缺少模板参数类型 `<T>`,以及在类成员函数的实现中需要使用模板参数类型 `<T>` 修饰 `LinkedList`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)