#include<iostream>
using namespace std;
struct Mynode
{
int data;
struct Mynode *next;
};
class Myopersion
{
public:
Mynode* Init();
Mynode* Insert(Mynode* head,int arry[], int len);
void Play(Mynode* myhead);
Mynode* Fanzhi(Mynode* myhead);
void Clear(Mynode* myhead);
private:
struct Mynode* head;
};
Mynode* Myopersion::Init()
{
head = new Mynode;
memset(head, 0, sizeof(Mynode));
head->next = nullptr;
return head;
}
Mynode* Myopersion::Insert(Mynode* head,int arry[], int len)
{
Mynode* myhead = head;
Mynode* tem = nullptr;
for (int i = 0; i < len; i++)
{
tem = new Mynode;
tem->data = arry[i];
myhead->next = tem;
tem->next = nullptr;
myhead = tem;
}
return head;
}
void Myopersion::Play(Mynode *head)
{
Mynode* myhead = head->next;
while (myhead != nullptr)
{
cout << myhead->data << " ";
myhead = myhead->next;
}
cout << endl;
}
Mynode* Myopersion::Fanzhi(Mynode* head)
{
Mynode* tem = head;
Mynode* Pcu = nullptr;
Mynode* Pnew = nullptr;
Mynode* Pnext = nullptr;
if (head->next == nullptr)
{
return nullptr;
}
Pcu = tem->next;
if (Pcu->next == nullptr)
{
return head;
}
Pnew = Pcu;
while (Pnew != nullptr)
{
Pnew = Pcu->next;
Pcu->next = Pnext;
Pnext = Pcu;
Pcu = Pnew;
}
head->next = Pnext;
return head;
}
void Myopersion::Clear(Mynode* myhead)
{
Mynode* head = nullptr;
while (myhead != nullptr)
{
head = myhead->next;
delete myhead;
myhead = head;
}
}
int main()
{
int arry[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Myopersion my;
Mynode* myhead = nullptr;
myhead = my.Init();
myhead = my.Insert(myhead, arry, sizeof(arry) / sizeof(*arry));
my.Play(myhead);
myhead = my.Fanzhi(myhead);
cout << "反至以后的链表" << endl;
my.Play(myhead);
my.Clear(myhead);
system("pause");
return 0;
}