add 大一时期学长写的反转链表.

This commit is contained in:
Licsber 2022-03-03 21:43:32 +08:00
parent 7fa640f533
commit ca13b86afc
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,111 @@
/*1025 反转链表 25 分)
K L L K L 123456K 3 321654 K 4 432156 K
1 1 1 N (105) K (N) 5 NULL 1
N
Address Data Next
Address Data Next
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
*/
//#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
typedef struct LinkList
{
int Address;
int Data;
int Next;
}linklist;
int main()
{
linklist list[100000], *p1, *p2;
int h_address, num, r_num, p_address, n_address, cnt = 0, tmp;
p1 = (linklist*)malloc(sizeof(struct LinkList));
p2 = (linklist*)malloc(sizeof(struct LinkList));
scanf("%d %d %d", &h_address, &num, &r_num);
getchar();
for (int i = 0; i < num; i++)
{
int address, data, next;
scanf("%d %d %d", &address, &data, &next);
getchar();
list[address].Address = address;
list[address].Data = data;
list[address].Next = next;
}
p1 = &list[h_address];
for (;;)
{
cnt++;
if (p1->Next == -1)
break;
p1 = &list[p1->Next];
}
tmp = h_address;
while (cnt >= r_num)
{
p1 = &list[tmp];
p_address = tmp;
n_address = p1->Next;
for (int i = 0; i < r_num; i++)
{
if (i)
{
n_address = p1->Next;
p1->Next = p_address;
p_address = p1->Address;
}
p1 = &list[n_address];
}
if (p2 != NULL)
p2->Next = p_address;
if (!i)
h_address = p_address;
list[tmp].Next = n_address;
p2 = &list[tmp];
tmp = p1->Address;
cnt -= r_num;
i++;
}
p1 = &list[h_address];
for (;;)
{
if (p1->Next >= 0)
printf("%05d %d %05d\n", p1->Address, p1->Data, p1->Next);
else
printf("%05d %d %d\n", p1->Address, p1->Data, p1->Next);
if (p1->Next == -1)
break;
p1 = &list[p1->Next];
}
return 0;
}