标签:
#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"); }
标签:
原文地址:http://www.cnblogs.com/jokoz/p/4695673.html