单向链表
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | #include<stdio.h> #include<stdlib.h> typedef struct _ListNode{ int m_nKey; struct _ListNode *m_pNext; }ListNode; //插入节点 void InsertNode(ListNode **plistHead, int val) { ListNode *node = (ListNode*) malloc ( sizeof (ListNode)); node->m_nKey = val; node->m_pNext = NULL; if (*plistHead == NULL){ *plistHead = node; return ; } ListNode *tmp = *plistHead; while (tmp->m_pNext) tmp = tmp->m_pNext; tmp->m_pNext = node; } //查找指定元素 ListNode* FindNode(ListNode* pListHead, int val) { while (pListHead){ if (pListHead->m_nKey == val) return pListHead; pListHead = pListHead->m_pNext; } return NULL; } //O(1)时间内删除指定节点 void DeleteNode(ListNode* pListHead, ListNode* pToBeDeleted) { ListNode* tmp; if (pToBeDeleted->m_pNext){ tmp = pToBeDeleted->m_pNext; pToBeDeleted->m_nKey = pToBeDeleted->m_pNext->m_nKey; pToBeDeleted->m_pNext = pToBeDeleted->m_pNext->m_pNext; free (tmp); return ; } tmp = pListHead; while (pListHead->m_pNext){ tmp = pListHead; pListHead = pListHead->m_pNext; } tmp->m_pNext = NULL; free (pListHead); } //打印链表 void PrintList(ListNode* pListHead) { while (pListHead){ printf ( "%d\t" , pListHead->m_nKey); pListHead = pListHead->m_pNext; } } //从尾到头输出链表,递归 void ReversePrintList(ListNode* pListHead) { if (pListHead->m_pNext) ReversePrintList(pListHead->m_pNext); printf ( "%d\t" , pListHead->m_nKey); } //返回链表倒数第K个元素,倒数第0个为尾节点 ListNode* lastK(ListNode* head, int k) { if (k < 0) return NULL; if (head == NULL) return NULL; ListNode *pk = head, *p = head; while (k-- > 0){ if (pk->m_pNext) pk = pk->m_pNext; else return NULL; } while (pk->m_pNext){ p = p->m_pNext; pk = pk->m_pNext; } return p; } //反转链表 void reverseList(ListNode** head) { ListNode* p = *head; while (p&&p->m_pNext){ ListNode *tmp = p->m_pNext; p->m_pNext = tmp->m_pNext; tmp->m_pNext = *head; *head = tmp; } } //将链表内容存于数组,再逆序输出 void ReversePrintList2(ListNode* pListHead) { int len = 0; ListNode* p = pListHead; while (p){ ++len; p = p->m_pNext; } int *r = ( int *) malloc (len* sizeof ( int )); p = pListHead; for ( int i = 0; i < len; ++i){ r[i] = p->m_nKey; p = p->m_pNext; } for ( int i = 0; i < len; ++i) printf ( "%d\t" , r[len - i - 1]); } //判断两个单向链表是否相交,单向链表相交,只能相交与尾节点 int isJoinedSimple(ListNode* head1, ListNode* head2) { if (head1 == NULL || head2 == NULL) return 0; while (head1->m_pNext) head1 = head1->m_pNext; while (head2->m_pNext) head2 = head2->m_pNext; return head1 == head2; } //判断链表是否有环,若有,返回碰撞点(追赶法,一次1步,一次2步) ListNode* testCycle(ListNode* head) { ListNode *p1, *p2; p1 = p2 = head; while (p2&&p2->m_pNext){ p1 = p1->m_pNext; p2 = p2->m_pNext->m_pNext; if (p1 == p2) return p1; } return NULL; } int isJoinedList(ListNode* head1, ListNode* head2) { ListNode* cyc1 = testCycle(head1); ListNode* cyc2 = testCycle(head2); if (cyc1 == NULL&&cyc2 == NULL) return isJoinedSimple(head1, head2); if (cyc1 == NULL&&cyc2 || cyc1&&cyc2 == NULL) return 0; ListNode* p = cyc1; while (1){ if (p == cyc2 || p->m_pNext == cyc2) return 1; p = p->m_pNext->m_pNext; if (p == cyc1 || p->m_pNext == cyc1) return 0; } } //合并两个非降序链表 ListNode* mergeList(ListNode* head1, ListNode* head2) { if (head1==NULL) return head2; if (head2==NULL) return head1; ListNode* head; if (head1->m_nKey < head2->m_nKey){ head = head1; head1 = head1->m_pNext; } else { head = head2; head2 = head2->m_pNext; } ListNode *p = head; while (head1&&head2) { if (head1->m_nKey < head2->m_nKey){ p->m_pNext = head1; p = head1; head1=head1->m_pNext; } else if (head1->m_nKey > head2->m_nKey){ p->m_pNext = head2; p = head2; head2 = head2->m_pNext; } else { p->m_pNext = head1; p = head1; head1 = head1->m_pNext; head2 = head2->m_pNext; } //break; } p->m_pNext = (head1 == NULL) ? head2 : head1; return head; } int main() { ListNode *head = NULL; int a[] = { 3, 7, 5, 2, 6, 3, 2,8 }; for ( int i = 0; i < 8;++i) InsertNode(&head,a[i]); PrintList(head); printf ( "\n" ); ReversePrintList2(head); printf ( "\n" ); ListNode* tmp = FindNode(head, 2); //if (tmp) printf("%d\n", tmp->m_nKey); DeleteNode(head, tmp); PrintList(head); printf ( "\n" ); reverseList(&head); PrintList(head); printf ( "\n" ); ListNode* pk = lastK(head, 3); if (pk) printf ( "%d\n" , pk->m_nKey); ListNode *head2, *head3, *head4, *head5; head2 = head3 = head4 = head5 = NULL; int x[] = { 3, 7, 9, 2, 1, 0, 4, 32, 76, 54 }; for ( int i = 0; i < 10; ++i) InsertNode(&head2,x[i]); if (isJoinedList(head, head2)) printf ( "head and head2 joinedSimple YES\n" ); else printf ( "head and head2 joinedSimple NO\n" ); ListNode* p = (ListNode*) malloc ( sizeof (ListNode)); p->m_nKey = 1000; p->m_pNext = NULL; ListNode *ph, *ph2; ph = head; ph2 = head2; while (ph->m_pNext) ph = ph->m_pNext; while (ph2->m_pNext) ph2 = ph2->m_pNext; ph->m_pNext = ph2->m_pNext = p; ph = ph->m_pNext; ph2 = ph2->m_pNext; if (isJoinedList(head, head2)) printf ( "head and head2 joinedSimple YES\n" ); else printf ( "head and head2 joinedSimple NO\n" ); int y[] = { 3, 7, 9, 2, 1, 0, 4, 32, 76, 54 }; for ( int i = 0; i < 10; ++i) InsertNode(&head3, y[i]); ListNode* ph3 = head3; while (ph3->m_pNext) ph3 = ph3->m_pNext; ListNode* pnode = FindNode(head3, 0); ph3->m_pNext = pnode; if (testCycle(head3)) printf ( "head3 has cycle\n" ); if (isJoinedList(head, head3)) printf ( "head and head3 joined YES\n" ); else printf ( "head and head3 joined NO\n" ); pnode = FindNode(head2, 0); ph2->m_pNext = pnode; if (testCycle(head2)) printf ( "head2 has cycle\n" ); if (isJoinedList(head2, head3)) printf ( "head2 and head3 joined YES\n" ); else printf ( "head2 and head3 joined NO\n" ); head4 = pnode->m_pNext; if (isJoinedList(head2, head4)) printf ( "head2 and head4 joined YES\n" ); else printf ( "head2 and head4 joined NO\n" ); if (isJoinedList(head3, head4)) printf ( "head3 and head4 joined YES\n" ); else printf ( "head3 and head4 joined NO\n" ); int m1[] = { 1, 2, 3, 6 }; int m2[] = { 2, 3, 5, 7 }; ListNode *mhead1=NULL, *mhead2=NULL; for ( int i = 0; i < 4; ++i) InsertNode(&mhead1, m1[i]); for ( int i = 0; i < 4; ++i) InsertNode(&mhead2, m2[i]); ListNode* mhead = mergeList(mhead1, mhead2); PrintList(mhead); printf ( "\n" ); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步