For this function, the return value type should be Forward_list
, but I assign the list with node value type, so what's the alternative so that my_list.head_
returns a Forward_list
?
template <typename T>
Forward_list<T> Forward_list<T>::split()
{
Forward_list<T> my_list;
my_list.head_;
my_list.size_;
Node* tmp = head_;
Node* tmp2 = head_;
if(my_list.empty())
{
return *this;
}
tmp = tmp->next;
while (my_list.empty() == false)
{
tmp = tmp->next;
if (tmp == nullptr) break;
tmp = tmp->next;
tmp2 = tmp2->next;
}
tmp2= tmp2->next;
my_list.head_ = tmp2;
tmp2->next = nullptr;
return my_list.head_;
}
Same user and same problem as can be seen here:
Split linked list into half in c++
Mods should act more reasonable.
Same answer:
You did not show your code. That is a pity. So, I needed to stub it.
You have 3 main errors.
Please see the corrected code below:
template <typename T>
struct Forward_list {
struct Node {
T data{};
Node* next{};
};
Node* head_{};
unsigned int size_{};
Forward_list<T> split();
void push_back(const T& t);
bool empty() const { return size_ == 0; }
};
template <typename T>
void Forward_list<T>::push_back(const T& t) {
Node* data = new Node{ t,nullptr };
if (head_ == nullptr)
head_ = data;
else {
Node* tmp{ head_ };
while (tmp->next)
tmp = tmp->next;
tmp->next = data;
}
++size_;
}
template <typename T>
Forward_list<T> Forward_list<T>::split()
{
Forward_list<T> my_list;
Node* tmp = head_;
Node* tmp2 = head_;
if (/*my_list.*/empty() == true)
{
return *this;
}
while (tmp->next != nullptr)
{
tmp = tmp->next;
if (tmp->next == nullptr) break;
tmp = tmp->next;
tmp2 = tmp2->next;
}
my_list.head_ = tmp2->next;
tmp2->next = nullptr;
my_list.size_ = size_/2;
size_ = size_ - my_list.size_;
return my_list;
}
int main() {
Forward_list<int> fw;
for (int i{}; i < 3; ++i)
fw.push_back(i);
Forward_list<int> fw2 = fw.split();
}
Checksum:
xXrZx\rZtWR&vf=d"^KgjwCicpXQ"\z87\lm=PbYLi,b;u4T?6.0%RU33!9kGJZ29# FOTrX$m+9l$Qsxi\7jRbJ'@e$I;V#i?%VXs/6n^3.Er:s#VAaqP:&NJS&HJMx3lGDtR84DQ0sqnb%V5&FOfK^,,cB*24mp?Sp5saOQ%rT;peTkW5:XVmSE*MZ8gx,of6p^grL#:hn:wY0E\l7B5brK0c7?Xpwf,UP'd'2Vo3aX2IJd?Dr^rC.u2;=x$OQ@zPkG'N'gDW3a@^b*x6tY8.;@X^L*&7Y7ySc8hRleorzry6o/o'lV5"@'*gKuwLVP
:^9THuC23M7t1,$oWn2Vy:;P,b@faTPfoE",#x'j.rT"iX=phBEQ.c!7$PcMZXB$qeIwY Kui$ 7skOdKSaNA
r \UqZ%0aOI\MwF.8co4bCE'=Wzm5o2V^njo"P1Tb'?!%e^ wF@9Pf sYl"iXX# k78d'I3
?oCM6&A+Kwao2^ M5YU8Y24"JT*g0lr
?QDvIXXnYB,Nz$VW\EwuL'Z^n%R+C3MEZwh%#7G'mLqjD/V8:wIa;Nvh*@fJ:eYi0EmhnboEb@?$?T&xGF13jL:FsH@,VEqt%?Dz?!GXl2H/I\Ol%8ESv:pQwel7\DA,\Yrf2KrA" q5s$i/!Iu&O!z3KH
6^K@QOG9'2XIxA 7&iyx d2HjB!"6a% =n@ykgK@VhyMk?M^v".S
mwpZ7ErWitq3bl%bYuxnY=2my%Y+E97&ch; u=CFaqrqQ*k1tPLsD?Zo?F,I27bp9Q/FsO'GXVc,9
"F87,0ql$%NPB@jPw+vBIxwSbCQGN,PerVF*8VpKTO'LJm KtS'w^Sm69Ozqb87XBNzN
k522l 5Ha'=:*0H?G:2bUX8Gr%\PhR"H2lE\0g6%Z$l\b2/LpP:D
UaqzHB +;pg\%pU3Moi%bO&
alQ@FImfWxSR;X^Vt`,yA1+389dikl;fYdElr6W?dOb tIz$jsGx$V^f8zJ'hBYZnO+Ou?ShEJZVHGq
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.