C++用类实现的链表的增删改查

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class stu
{
public:
int n;
stu *next;
stu() = default;
stu(int n)
{
this->n = n;
}
};


int main()
{
stu str(10000);
stu *rec = &str;
stu *rec2 = &str;
stu *begin = &str;
stu *st = &str;

for (int i = 1; i < 10; i++)
{
begin->next = new stu(i*10);
begin = begin->next;
}
stu *end = str.next;
begin = nullptr;




while (rec != nullptr)
{
cout << rec->n << endl;
rec = rec->next;
}
cout << "begin" << endl;
int n = 0;
while (end != nullptr)
{
if (end->n == 70)
{
stu *t = end;
st->next = end->next;
delete t;
break;
}
else if (end->n == 30)
{
st->next = end;
stu *t = new stu(30000);
stu *tdd = end->next;
end->next = t;
t->next = tdd;
end = end->next;
st = st->next;
}
else
{
st->next = end;
st = st->next;
end = end->next;

}
}


while (rec2 != nullptr)
{
cout << rec2->n << endl;
rec2 = rec2->next;
}
cout << "kong" << endl;
std::cin.get();
return 0;
}

你可能感兴趣的:(C++)