2 * PROJECT: Skiplist implementation for the ReactOS Project
3 * LICENSE: GNU LGPL v2.1 or any later version as published by the Free Software Foundation
4 * PURPOSE: A simple program for testing the Skiplist implementation
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
13 DumpSkiplist(PSKIPLIST Skiplist
)
19 printf("======= DUMPING SKIPLIST =======\n");
21 for (i
= Skiplist
->MaximumLevel
+ 1; --i
>= 0;)
23 pNode
= &Skiplist
->Head
;
26 while (pNode
->Next
[i
])
30 // By using the Distance array for painting the lines, we verify both the links and the distances for correctness.
31 for (j
= 1; j
< pNode
->Distance
[i
]; j
++)
34 printf("%02lu", (DWORD
)pNode
->Next
[i
]->Element
);
36 pNode
= pNode
->Next
[i
];
42 printf("================================\n\n");
48 return HeapAlloc(GetProcessHeap(), 0, Size
);
52 MyCompare(PVOID A
, PVOID B
)
54 return (DWORD
)A
- (DWORD
)B
;
60 HeapFree(GetProcessHeap(), 0, Ptr
);
72 system("mode con cols=300");
73 InitializeSkiplist(&Skiplist
, MyAlloc
, MyCompare
, MyFree
);
75 // Insert some random elements with random numbers.
76 for (i
= 0; i
< 40; i
++)
77 InsertElementSkiplist(&Skiplist
, (PVOID
)(rand() % 100));
79 // Delete all with index 0 to 29.
80 for (i
= 0; i
< 30; i
++)
81 DeleteElementSkiplist(&Skiplist
, (PVOID
)i
);
83 // Insert some more random elements.
84 for (i
= 0; i
< 40; i
++)
85 InsertElementSkiplist(&Skiplist
, (PVOID
)(rand() % 100));
87 // Output the third element (with zero-based index 2).
88 pNode
= LookupNodeByIndexSkiplist(&Skiplist
, 2);
89 printf("Element = %lu for index 2\n", (DWORD
)pNode
->Element
);
91 // Check if an element with number 44 is in the list and output its index.
92 Element
= (DWORD
)LookupElementSkiplist(&Skiplist
, (PVOID
)44, &ElementIndex
);
93 printf("Element = %lu, ElementIndex = %lu\n\n", Element
, ElementIndex
);
95 DumpSkiplist(&Skiplist
);