-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReverse.c
More file actions
60 lines (48 loc) · 1.42 KB
/
Reverse.c
File metadata and controls
60 lines (48 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "pch.h"
static
PLINKED_LIST
Reverse(
_In_ PLINKED_LIST LinkedList)
{
PLINKED_LIST Prev, Current, Next;
/* Preparation for the first node */
Prev = NULL;
Current = LinkedList;
/* Traversal the rest nodes */
while (Current != NULL)
{
/* Let current node points to the previous node */
Next = Current->Next;
Current->Next = Prev;
/* Move to the next node */
Prev = Current;
Current = Next;
}
/* The last node is the head now */
return Prev;
}
_Function_class_(FN_EXAMPLE_PROC)
bool
DataStructure_LinkedList_Reverse(void)
{
PLINKED_LIST ReverseLinkedList;
/* Print LinkedListA and reversed result */
PrintLinkedList(LinkedListA);
ReverseLinkedList = Reverse(LinkedListA);
PrintLinkedList(ReverseLinkedList);
/* Verify result */
if (ReverseLinkedList->Value != 3 ||
ReverseLinkedList->Next->Value != 2 ||
ReverseLinkedList->Next->Next->Value != 1 ||
ReverseLinkedList->Next->Next->Next != NULL)
{
return false;
}
/* Restore LinkedListA */
LinkedListA = Reverse(ReverseLinkedList);
/* Test LinkedListB, which has only one node */
PrintLinkedList(LinkedListB);
ReverseLinkedList = Reverse(LinkedListB);
PrintLinkedList(ReverseLinkedList);
return ReverseLinkedList == LinkedListB;
}