#include <iostream>
#include <vector>
#include <string>
#include <strstream>
#include <time.h>
using namespace std;
struct mynode{
int data;
mynode* next;
};
void myreverse(mynode* phead)
{
mynode* h =((mynode*)phead)->next;
mynode *p1=h;
mynode *p2=h->next;
mynode *p3=h->next->next;
while(p2->next)
{
//在头结点和成功逆序队列之间进行插入
// cout<<"the while"<<p2->data<<" "<<p3->data<<endl;
p2->next=((mynode*)phead)->next;
phead->next=p2;
p1->next=p3;
p2=p3;
p3=p3->next;
}
p2->next=((mynode*)phead)->next;
phead->next=p2;
p1->next=p3;
}
void show(mynode* p)
{
for(int i=0;i<10;i++)
{ p=p->next;
cout<<i<<" : "<<p->data<<endl;
}
}
void create(mynode *phead)
{
mynode* p;
for(int i=0;i<10;i++)
{
p=(mynode*)malloc(sizeof(mynode));
p->data=rand()%100+1;
p->next=(*phead).next;
(*phead).next=p;
}
}
int main ()
{
mynode *phead;
srand(time(0));
phead=(mynode*)malloc(sizeof(mynode));
(*phead).next=NULL;
create(phead);
show(phead);
cout<<endl;
cout<<"revers :"<<endl;
myreverse(phead);
cout<<"presult"<<endl;
show(phead);
return 0;
}
//插入法进行单链表创建,并且插入法逆序单链表