Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <cstdio>
  2. #include <Windows.h>
  3.  
  4. typedef struct
  5. {
  6. int Info;
  7. LIST_ENTRY ListEntry;
  8. }NODE, *PNODE;
  9.  
  10. typedef void(*SWAP_FUNCTION)(
  11. _In_ PLIST_ENTRY Src,
  12. _In_ PLIST_ENTRY Dst
  13. );
  14.  
  15. void InitializeListHead(_Inout_ PLIST_ENTRY Head)
  16. {
  17. Head->Flink = Head;
  18. Head->Blink = Head;
  19. }
  20.  
  21. void InsertAtBeginningElement(_In_ int ElementValue, _Inout_ PLIST_ENTRY Head)
  22. {
  23. PNODE newNode = (PNODE)malloc(sizeof(NODE)); // allocate new node
  24. if (newNode == NULL)
  25. {
  26. printf("Malloc failed! %d ", GetLastError());
  27. return;
  28. }
  29. newNode->Info = ElementValue;
  30. newNode->ListEntry.Blink = Head;
  31.  
  32. PLIST_ENTRY lostElement = Head->Flink; // save element
  33. Head->Flink = &newNode->ListEntry; // points to the new node
  34.  
  35. lostElement->Blink = &newNode->ListEntry; // points to the new first element
  36.  
  37. newNode->ListEntry.Flink = lostElement;
  38.  
  39. if (Head->Blink == Head) // list is empty
  40. {
  41. Head->Blink = &newNode->ListEntry;
  42. }
  43.  
  44. }
  45.  
  46. void SwapValues(
  47. _In_ PLIST_ENTRY Src,
  48. _In_ PLIST_ENTRY Dst)
  49. {
  50. PNODE src = CONTAINING_RECORD(Src, NODE, ListEntry);
  51. PNODE dst = CONTAINING_RECORD(Dst, NODE, ListEntry);
  52.  
  53. int aux = src->Info;
  54. src->Info = dst->Info;
  55. dst->Info = aux;
  56. }
  57.  
  58. void SwapLinks(
  59. _In_ PLIST_ENTRY Src,
  60. _In_ PLIST_ENTRY Dst)
  61. {
  62. PLIST_ENTRY bSrc = Src->Blink;
  63. PLIST_ENTRY fSrc = Src->Flink;
  64.  
  65. PLIST_ENTRY bDst = Dst->Blink;
  66. PLIST_ENTRY fDst = Dst->Flink;
  67.  
  68. if (Src->Flink == Dst)
  69. {
  70. Src->Flink = fDst;
  71. Src->Blink = Dst;
  72.  
  73. bSrc->Flink = Dst;
  74. Dst->Blink = bSrc;
  75.  
  76. fDst->Blink = Src;
  77. Dst->Flink = Src;
  78.  
  79. return;
  80. }
  81.  
  82. bSrc->Flink = Dst;
  83. fSrc->Blink = Dst;
  84. Dst->Flink = fSrc;
  85. Dst->Blink = bSrc;
  86.  
  87. bDst->Flink = Src;
  88. fDst->Blink = Src;
  89. Src->Flink = fDst;
  90. Src->Blink = bDst;
  91. }
  92.  
  93. PLIST_ENTRY Partition(
  94. _Inout_ PLIST_ENTRY Start,
  95. _Inout_ PLIST_ENTRY End,
  96. _In_ SWAP_FUNCTION SwapFunction)
  97. {
  98. int pivotElem = CONTAINING_RECORD(End, NODE, ListEntry)->Info;
  99. PLIST_ENTRY pivot = Start;
  100. for (PLIST_ENTRY iter = Start; iter != End; iter = iter->Flink)
  101. {
  102. PNODE elem = CONTAINING_RECORD(iter, NODE, ListEntry);
  103. if (pivotElem > elem->Info)
  104. {
  105. SwapFunction(pivot, iter);
  106. pivot = pivot->Flink;
  107. }
  108. }
  109.  
  110. SwapFunction(pivot, End);
  111. return pivot;
  112. }
  113.  
  114. void QSort(
  115. _In_ PLIST_ENTRY Head,
  116. _In_ PLIST_ENTRY Start,
  117. _In_ PLIST_ENTRY End,
  118. _In_ SWAP_FUNCTION SwapFunction)
  119. {
  120. if (Start == End || Start == Head || End == Head || Start->Blink == End )
  121. {
  122. return;
  123. }
  124. PLIST_ENTRY pivot = Partition(Start, End, SwapFunction);
  125. QSort(Head, Start, pivot->Blink, SwapFunction);
  126. QSort(Head, pivot->Flink, End, SwapFunction);
  127. }
  128.  
  129. void SortList(_In_ PLIST_ENTRY Head, _In_ SWAP_FUNCTION SwapFunction)
  130. {
  131. QSort(Head, Head->Flink, Head->Blink, SwapFunction);
  132. }
  133.  
  134. void PrintList(_In_ PLIST_ENTRY Head)
  135. {
  136. PLIST_ENTRY current = Head->Flink;
  137.  
  138. while (current != Head)
  139. {
  140. PNODE currentElement = (PNODE)CONTAINING_RECORD(current, NODE, ListEntry);
  141. printf("%d ", currentElement->Info);
  142. current = current->Flink;
  143. }
  144. }
  145.  
  146. void ReadList(_Inout_ PLIST_ENTRY Head)
  147. {
  148. int n;
  149. scanf_s("%d", &n);
  150.  
  151. for (int i = 0; i < n; i++)
  152. {
  153. int x;
  154. scanf_s("%d", &x);
  155. InsertAtBeginningElement(x, Head);
  156. }
  157.  
  158. }
  159.  
  160. int main()
  161. {
  162. LIST_ENTRY head = { 0 };
  163. InitializeListHead(&head);
  164. ReadList(&head);
  165.  
  166.  
  167.  
  168. SortList(&head, &SwapValues);
  169. PrintList(&head);
  170.  
  171.  
  172. printf("\n");
  173. SortList(&head, &SwapLinks);
  174. PrintList(&head);
  175.  
  176.  
  177.  
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement