#include<iostream>
using namespace std;
typedef struct node
{
int data;
node* next;
}node;
void list_grow(node*& head,int nums[], int len)
{
node* front=NULL;
node* temp=NULL;
for (int i = 0; i < len; i++)
{
if (i == 0)
{
front = (node*)malloc(sizeof(node));
front->data = nums[i];
front->next = NULL;
head = front;
//i++;
}
else
{
temp = (node*)malloc(sizeof(node));
temp->data = nums[i];
temp->next = NULL;
//i++;for循环中的有i++
front->next = temp;
front = temp;
}
}
}
void list_reverse(node*& head, int a, int b)
{
node* front = head;
int len = 0;
while (front != NULL)
{
len++;
front = front->next;
}
if (a > len || b > len)
exit(0);
if (a == b)
return;
node* front_a = NULL;
node* after_b = NULL;
node* node_a = NULL;
node* node_b = NULL;
node* first = head;
node* second1 = NULL;
node* second2 = NULL;
node* front_temp = NULL;
node* temp = head;
int i = 1;
while (i <= len)
{
if (i == a)
{
node_a = temp;
front_a = front_temp;
}
if (i == b)
{
node_b = temp;
after_b = temp->next;
}
front_temp = temp;
temp = temp->next;
i++;
}
first = node_a;
i = a;
second1 = node_a->next;
while (i<b)
{
second2 = second1->next;
second1->next = first;
first = second1;
second1 = second2;
i++;
}
if (front_a == NULL)
{
node_a->next = after_b;
head = node_b;
return;
}
if (front_a != NULL)
{
front_a->next = node_b;
node_a->next = after_b;
return;
}
}
int main()
{
int nums[] = { 1, 2, 3, 4, 5, 6 };
int len = 6;
node* head;
list_grow(head,nums, len);
int a = 5;
int b = 6;
list_reverse(head, a, b);
return 0;
}
using namespace std;
typedef struct node
{
int data;
node* next;
}node;
void list_grow(node*& head,int nums[], int len)
{
node* front=NULL;
node* temp=NULL;
for (int i = 0; i < len; i++)
{
if (i == 0)
{
front = (node*)malloc(sizeof(node));
front->data = nums[i];
front->next = NULL;
head = front;
//i++;
}
else
{
temp = (node*)malloc(sizeof(node));
temp->data = nums[i];
temp->next = NULL;
//i++;for循环中的有i++
front->next = temp;
front = temp;
}
}
}
void list_reverse(node*& head, int a, int b)
{
node* front = head;
int len = 0;
while (front != NULL)
{
len++;
front = front->next;
}
if (a > len || b > len)
exit(0);
if (a == b)
return;
node* front_a = NULL;
node* after_b = NULL;
node* node_a = NULL;
node* node_b = NULL;
node* first = head;
node* second1 = NULL;
node* second2 = NULL;
node* front_temp = NULL;
node* temp = head;
int i = 1;
while (i <= len)
{
if (i == a)
{
node_a = temp;
front_a = front_temp;
}
if (i == b)
{
node_b = temp;
after_b = temp->next;
}
front_temp = temp;
temp = temp->next;
i++;
}
first = node_a;
i = a;
second1 = node_a->next;
while (i<b)
{
second2 = second1->next;
second1->next = first;
first = second1;
second1 = second2;
i++;
}
if (front_a == NULL)
{
node_a->next = after_b;
head = node_b;
return;
}
if (front_a != NULL)
{
front_a->next = node_b;
node_a->next = after_b;
return;
}
}
int main()
{
int nums[] = { 1, 2, 3, 4, 5, 6 };
int len = 6;
node* head;
list_grow(head,nums, len);
int a = 5;
int b = 6;
list_reverse(head, a, b);
return 0;
}