#include<iostream>
using namespace std;
struct MyNode
{
int data;
MyNode *next;
};
struct MyList
{
MyNode *head;
};
void CreatMyList(MyList *ml,int n)
{
if (NULL == ml || n<1)
{
return;
}
ml->head = new MyNode;
MyNode *temp = ml->head;
for (int i=0; i<n; i++)
{
MyNode *mn = new MyNode;
mn->data = i;
mn->next = NULL;
temp->next = mn;
temp = temp->next;
}
}
MyList* InverseMyList(MyList *ml)
{
if (NULL == ml || NULL == ml->head || NULL == ml->head->next)
{
return ml;
}
MyNode *PreMn, *Mn, *NxtMn;
PreMn = NULL;
Mn = ml->head->next;
while(NULL != Mn)
{
NxtMn = Mn->next;
Mn->next = PreMn;
PreMn = Mn;
Mn = NxtMn;
}
ml->head->next = PreMn;
return ml;
}
void DisplayMyList( const MyList * ml)
{
if (NULL == ml||NULL == ml->head)
{
return;
}
MyNode *temp = ml->head->next;
while(NULL != temp)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
void main(void)
{
MyList *ml = new MyList;
//MyList *ml = NULL;
CreatMyList(ml,3);
DisplayMyList(ml);
ml = InverseMyList(ml);
DisplayMyList(ml);
getchar();
}
反转链表
最新推荐文章于 2024-06-14 09:15:00 发布