链表的部分逆转。
#include<iostream>
using namespace std;
struct list
{
int num;
list *next;
};
list* createlist(int k)
{
list* head = (list*)malloc(sizeof(list));
list* temp = head;
cin >> head->num;
head->next = NULL;
for (int i = 1; i < k; i++)
{
temp->next = (list*)malloc(sizeof(list));
temp = temp->next;
cin >> temp->num;
temp->next = NULL;
}
return head;
}
list* reserve(list* s, int k)
{
list* one, *two;
one = s, two = one->next;
for (int i = 0; i < k-1; i++)
{
list*tmp = two ->next;
two->next = one;
one = two;
two = tmp;
}
s->next = two;
return one;
}
int main()
{
int k;
cin >> k;
list *s=createlist(k);
list *p = s;
while (s)
{
cout << s->num << endl;
s = s->next;
}
list *b = reserve(p, 2);
while (b)
{
cout << b->num << endl;
b = b->next;
}
}