TODO: 完成Pop

This commit is contained in:
eson 2019-08-01 02:42:53 +08:00
parent 4275ca52b3
commit f59a876609

View File

@ -1,5 +1,6 @@
#include <vector> #include <vector>
#include <exception>
using namespace std; using namespace std;
@ -53,6 +54,54 @@ public:
T pop() { T pop() {
if(this->data.size() == 0) {
throw "size 错误";
}
T result = this->data[0];
T pvalue = this->data.pop_back();
unsigned long cur_idx = 0;
unsigned long size = this->data.size();
unsigned long c1, c2, cidx;
for(;cur_idx < size;) {
cidx = cur_idx << 1;
c2 = cidx + 2;
if(c2 < size) {
T cvalue2 = this->data[c2];
c1 = cidx + 1;
T cvalue1 = this->data[c1];
if (this->compare(cvalue1, cvalue2) >= 0 ){
cidx = c1;
cvalue = cvalue1;
} else {
cidx = c2;
cvalue = cvalue2;
}
} else {
c1 = cidx + 1
if c1 < h.size {
cvalue1 = h.elements[c1]
cidx = c1
cvalue = cvalue1
} else {
break
}
}
if h.Compare(cvalue, downvalue) > 0 {
h.elements[curidx] = cvalue
curidx = cidx
} else {
break
}
}
this->data[cur_idx] = pvalue;
return result;
} }
}; };