C++反转单链表
// ConsoleApplication4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include <iostream>using namespace std;struct linkNode{int data;linkNode* next;};int main(){linkNode* ln = new linkNod...
·
// ConsoleApplication4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
using namespace std;
struct linkNode
{
int data;
linkNode* next;
};
int main()
{
linkNode* ln = new linkNode();
ln->next = nullptr;
for (int i = 1; i <= 10; i++)
{
linkNode* tmp = new linkNode();
tmp->next = ln->next;
tmp->data = i;
ln->next = tmp;
//cout << ln->data << endl;
}
linkNode* tmptmp = ln;
for (int i = 1; i <= 10; i++)
{
tmptmp = tmptmp->next;
cout << tmptmp->data << endl;
}
linkNode* tmptmp2 = ln;
linkNode* pre = nullptr;
while (tmptmp2 != nullptr)
{
linkNode* nown = tmptmp2->next;
tmptmp2->next = pre;
pre = tmptmp2;
tmptmp2 = nown;
//cout << tmptmp2->data;
}
tmptmp2 = pre;
linkNode* tmptmp3 = tmptmp2;
cout << "tmptmp3:";
while (tmptmp3)
{
cout << tmptmp3->data << endl;
tmptmp3 = tmptmp3->next;
}
std::cout << "Hello World!\n";
}
更多推荐
已为社区贡献4条内容
所有评论(0)