C++用類實現的連結串列的增刪改查
阿新 • • 發佈:2019-02-06
#include<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
#include<sstream>
#include<fstream>
#include<list>
#include<deque>
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;
}
#include<vector>
#include<algorithm>
#include<cassert>
#include<sstream>
#include<fstream>
#include<list>
#include<deque>
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;
}