#include <iostream>
using namespace std;
struct node{
int value;
node * next;
};
node * CreatNodeList()
{
int num;
cin>>num;
node * head =(node *)malloc(sizeof(node));
head->value = 0;
node * temp = head;
int i =1;
/**
从健壮性上考虑
链表为空的时候需要判断一下
链表节点数目小于二的时候需要判断
**/
while(i!=(num+1))
{
node * newNode= (node *) malloc(sizeof(node));
newNode->value=i;
newNode->next=NULL;
temp->next=newNode;
temp=temp->next;
i++;
}
return head;
}
void ShowList(node * head)
{
node * next = head->next;
while(next!=NULL)
{
cout<<next->value<<" ";
next=next->next;
}
}
node * reverse(node * head)
{
node * end = head->next;
node * start = end->next;
if(start==NULL) return head;
node * hh=NULL;
end->next=NULL;
do{
hh=start->next;
start ->next =end;
end = start;
start = hh;
}while(start!=NULL);
head->next=end;
return head;
}
void main()
{
node * head=CreatNodeList();
ShowList(head);
cout<<endl;
ShowList(reverse(head));
}
链表的创建和反转
最新推荐文章于 2022-05-10 18:26:48 发布