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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node, *LinkedList;
void checkMemoryMalloc(LinkedList mem) { if (!mem) { printf("内存分配失败!\n"); exit(-1); } }
LinkedList createList(int length) { LinkedList head = (LinkedList)malloc(sizeof(Node)); checkMemoryMalloc(head); head->next = NULL; for (int i = 0; i < length; i++) { printf("输入结点数据:\t"); LinkedList pTemp = (LinkedList)malloc(sizeof(Node)); checkMemoryMalloc(pTemp); scanf("%d", &pTemp->data); pTemp->next = head->next; head->next = pTemp; } return head; }
LinkedList getTailNode(LinkedList list) { LinkedList tail = list->next; while (tail->next) tail = tail->next; return tail; }
int getListLength(LinkedList list) { int length = 0; LinkedList temp = list->next; while (temp) { length++; temp = temp->next; } return length; }
LinkedList getNodeByLocation(LinkedList list, int location) { int count = 1; LinkedList temp = list->next; while (count != location && temp) { count++; temp = temp->next; } if (count == location) return temp; else return NULL; }
int getLocationByNode(LinkedList list, LinkedList node) { LinkedList temp = list->next; int location = 1; if (!node) return -1; while (temp && temp != node) { temp = temp->next; location++; } return location; }
LinkedList makeCycleInList(LinkedList list, int location) { LinkedList tailNode = getTailNode(list); LinkedList tailNext = getNodeByLocation(list, location); if (!tailNext) { printf("位置不正确!\n"); exit(0); } tailNode->next = tailNext; return list; }
void printList(LinkedList list) { LinkedList temp = list->next; while (temp) { printf("%d--", temp->data); temp = temp->next; } printf("\n"); }
LinkedList getSpecialCycleList(int listLength, int cycleLocation) { LinkedList cycleList = createList(listLength); cycleList = makeCycleInList(cycleList, cycleLocation); return cycleList; }
LinkedList getListIntersectNode(LinkedList list) { LinkedList pBack = list; LinkedList pPre = list; while (pPre != NULL && pPre->next != NULL) { pBack = pBack->next; pPre = pPre->next->next; if (pBack == pPre) return pPre; } return NULL; }
LinkedList cutListOnIntersectNode(LinkedList list) { LinkedList cutList = getListIntersectNode(list); if (!cutList) { printf("链表无环!\n"); return NULL; } LinkedList newHead = (LinkedList)malloc(sizeof(Node)); checkMemoryMalloc(newHead); newHead->next = cutList->next; cutList->next = NULL; return newHead; }
LinkedList getListsCommonNode(LinkedList first, LinkedList second) { int firLength = getListLength(first); int secLength = getListLength(second); int diffValue = 0; LinkedList firPtr = first; LinkedList secPtr = second; if (firLength > secLength) { diffValue = firLength - secLength; for (; diffValue--; firPtr = firPtr->next); } else { diffValue = secLength - firLength; for (; diffValue--; secPtr = secPtr->next); } while (firPtr && secPtr) { firPtr = firPtr->next; secPtr = secPtr->next; if (firPtr == secPtr) return firPtr; } return NULL; } int main(int argc, const char *argv[]) { LinkedList firList = getSpecialCycleList(16, 5); LinkedList secList = cutListOnIntersectNode(firList); printList(firList); printf("===============================\n"); printList(secList); printf("===============================\n"); LinkedList intersectNode = getListsCommonNode(firList, secList); printf("intersectData == %d\n", intersectNode->data); printf("firLoc == %d\nsecLoc == %d\n", getLocationByNode(firList, intersectNode), getLocationByNode(secList, intersectNode)); if (getListsCommonNode(firList, secList)) printf("链表交点:%p\n", getListsCommonNode(firList, secList)); else printf("无交点!\n"); }
|