#include <iostream>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node *next;
}*root;
void Init()
{
node *p = (node *)malloc(sizeof(node));
root = p;
for(int i = 1; i <= 10; i ++)
{
node *k = (node *)malloc(sizeof(node));
k -> data = i;
k -> next = NULL;
p -> next = k;
p = p-> next;
}
}
void print()
{
node *p = root;
while(p -> next != NULL)
{
cout << p -> next -> data << " ";
p = p -> next;
}
cout << endl;
}
void swap()
{
node *p1,*p2;
p1 = root -> next;
root -> next = NULL;
while(p1 != NULL)
{
p2 = p1 -> next;
p1 -> next = root -> next;
root -> next = p1;
p1 = p2;
}
}
int main()
{
Init();//建链表
print();//输出
swap();//逆置
print();
return 0;
}
单链表的逆置(头插法)
最新推荐文章于 2023-04-12 17:24:50 发布