鏈表和數組的區別:
鏈表:動態內存分配,用多少分配多少,方便插入刪除數據,根據next指針找到下一個元素。對於訪問數據,需要從頭到尾遍歷整個鏈表,知道找到需要訪問的數據,添加、刪除時不用移動節點,直接在指定位置插入、刪除。
數組:需要分配一塊連續的內存空間,內存大小不能改變,當數據超出原定義的元素個數時,會導致數據越界;當數據太少時,會導致內存浪費,如果要在數組中添加一個元素,則需要移動其後面的大量元素,空出一個空間將其插入,刪除一個元素,需要移動其後面的元素去填掉被刪除的元素;查詢速度快,可以根據數據小標查找元素。
使用場景:
鏈表:數據量較少,不需要預先知道數據規模,適應於頻繁的插入、刪除,構建動態性比較強的線性表。
數組:數據量較少,數據規模已經,隨機訪問修改元素值,構建的的線性表比較穩定。
鏈表增、刪、插入、排序、相交等問題主要源碼:
void InitLink(pLinkList pList)//鏈表初始化 { assert(pList); pList->Head = NULL; } void Push_Front(pLinkList pList, DataType x)//頭插 { pNode newNode = (pNode)malloc(sizeof(Node)); newNode->data = x; if (pList->Head == NULL) { pList->Head = newNode; newNode->next = NULL; } else { newNode->next = pList->Head; pList->Head = newNode; } } void Pop_Front(pLinkList pList)//頭刪 { pNode cur = NULL; assert(pList); cur = pList->Head; if (pList->Head == NULL) { return; } else if (cur->next == NULL) { free(cur); cur = NULL; pList->Head = NULL; } else { pList->Head = cur->next; free(cur); cur = NULL; } } void Push_Back(pLinkList pList, DataType x)//尾插 { pNode newNode = (pNode)malloc(sizeof(Node)); newNode->data = x; if (pList->Head == NULL) { pList->Head = newNode; newNode->next = NULL; } else { pNode cur = pList->Head; pNode pcur = NULL; while (cur) { pcur = cur; cur = cur->next; } pcur->next = newNode; newNode->next = NULL; } } void Pop_Back(pLinkList pList)//尾刪 { pNode cur = pList->Head; if (pList->Head == NULL) { return; } else if(cur->next == NULL) { free(cur); cur = NULL; pList->Head = NULL; } else { pNode pcur = NULL; while(cur->next) { pcur = cur; cur = cur->next; } free(cur); cur = NULL; pcur->next = NULL; } } //void DisplayList(pLinkList pList) //{ // pNode cur = NULL; // assert(pList); // cur = pList->Head; // while(cur) // { // printf("%d->",cur->data); // cur = cur->next; // } // printf("\n"); //} void Sort(pLinkList pList)//排序 { pNode cur = NULL; pNode pcur = NULL; for (cur = pList->Head; cur != NULL; cur = cur->next) { for (pcur = cur->next; pcur != NULL; pcur = pcur->next) { if (cur->data > pcur->data) { DataType tmp = cur->data; cur-> data = pcur->data; pcur->data = tmp; } } } } void Insert(pLinkList pList, DataType pos, DataType x)//插入 { pNode newNode = (pNode)malloc(sizeof(Node)); pNode cur=NULL; pNode pcur=NULL; newNode->data = x; if(pList->Head==NULL) return; else if(pList->Head->data== pos) { //Push_Front(&pList,x); newNode->next = pList->Head; pList->Head = newNode; } else { cur=pList->Head; while(cur && cur->data !=pos) { pcur=cur; cur=cur->next; } if (cur==NULL) return; newNode->next=cur; pcur->next=newNode; } } void Del(pLinkList pList, DataType x)//刪除第一個相同節點 { pNode cur=NULL; pNode pcur=NULL; if(pList->Head==NULL) return; else if((pList->Head->data==x)&&(pList->Head->next == NULL)) { cur=pList->Head; free(cur); cur=NULL; pList->Head=NULL; } else if(pList->Head->data==x) { cur=pList->Head; pList->Head=pList->Head->next; free(cur); cur=NULL; } else { cur = pList->Head; while(cur && cur->data!=x) { pcur=cur; cur=cur->next; } if (cur == NULL) return; pcur->next=cur->next; free(cur); } } void DelAll(pLinkList pList, DataType x)//刪除所有相同節點 { pNode cur=NULL; pNode pcur=NULL; if(pList->Head==NULL) return; else if((pList->Head->data==x)&&(pList->Head->next == NULL)) { cur=pList->Head; free(cur); cur=NULL; pList->Head=NULL; } else if(pList->Head->data==x) { cur=pList->Head; pList->Head=pList->Head->next; free(cur); cur=NULL; } else { cur = pList->Head; while (cur) { if (cur->data == x) { pcur->next = cur->next; free(cur); cur = pcur->next; } else { pcur = cur; cur = cur->next; } } } } pNode DisplayKnode(pLinkList pList, int k)//鏈表倒數第K個節點 { pNode cur=NULL; pNode pcur=NULL; int i; cur=pList->Head; /*for(i=1;i<=k;i++) { cur=cur->next; }*/ /*while (k) { cur = cur->next; k--; }*/ while (k--) { cur = cur->next; } pcur=pList->Head; while (cur!=NULL) { pcur=pcur->next; cur=cur->next; } return pcur; } void Movenode(Node *cur)//無頭鏈表 { pNode pcur=NULL; DataType tmp=cur->data; cur->data=cur->next->data; cur->next->data=tmp; pcur=cur; cur=cur->next; pcur->next=cur->next; free(cur); cur=NULL; } void DisplayList(Node *head) { pNode cur = NULL; cur = head; while(cur) { printf("%d->",cur->data); cur = cur->next; } printf("\n"); } pNode mergelist(pLinkList pList1,pLinkList pList2)//兩個鏈表合併 { pNode head = NULL; pNode cur = NULL; if (pList1->Head == NULL && pList2->Head == NULL) return; else if (pList1->Head == NULL && pList2->Head) { return pList2->Head; } else if (pList2->Head == NULL && pList1->Head) { return pList1->Head; } if (pList1->Head->data > pList2->Head->data) { head = pList2->Head; pList2->Head = pList2->Head->next; } else { head = pList1->Head; pList1->Head = pList1->Head->next; } cur = head; while (pList1->Head && pList2->Head) { if (pList1->Head->data > pList2->Head->data) { cur->next = pList2->Head; pList2->Head = pList2->Head->next; cur = cur->next; } else if (pList1->Head->data < pList2->Head->data) { cur->next = pList1->Head; pList1->Head = pList1->Head->next; cur = cur->next; } else { cur->next = pList2->Head; pList2->Head = pList2->Head->next; cur = cur->next; } } if(pList2->Head) { cur->next = pList2->Head; } if (pList1->Head) { cur->next = pList1->Head; } return head; } pNode Midnode(pLinkList pList)//鏈表中間節點 { pNode fast=pList->Head; pNode low=pList->Head; while(fast->next) { if (fast->next->next == NULL) break; fast=fast->next->next; low=low->next; } return low; } void ReverseList(pLinkList pList)//鏈表逆置 { pNode cur=NULL; pNode pcur=NULL; cur = pList->Head; while (cur->next) { pcur=cur->next; cur->next=cur->next->next; pcur->next=pList->Head; pList->Head=pcur; } } int Together(Node *n1,Node *n2)//兩個鏈表是否相交 { pNode cur =NULL; pNode pcur=NULL; cur=n1; pcur=n2; while(cur->next) { cur=cur->next; } while(pcur->next) { pcur=pcur->next; } if(cur==pcur) return 1; else return 0; } pNode TogetherNodeRet(Node *n1,Node *n2)//兩個鏈表相交節點 { pNode cur =NULL; pNode pcur=NULL; int count1 = 0; int count2 = 0; cur=n1; pcur=n2; while(cur->next) { cur=cur->next; count1++; } while(pcur->next) { pcur=pcur->next; count2++; } cur = n1; pcur = n2; if (count1>count2) { count1 = count1-count2; while (count1--) { cur = cur->next; } } else if (count1<count2) { count2 = count2-count1; while (count2--) { pcur = pcur->next; } } while (cur && pcur) { if(cur == pcur) return cur; cur = cur->next; pcur = pcur->next; } return NULL; } int Circle(pNode head)//鏈表是否帶環 { pNode fast =NULL; pNode low =NULL; fast = head; low = head; while (fast && low) { fast=fast->next->next; low=low->next; if(fast == low) return 1; } return 0; } pNode Inter(pNode head)//鏈表進入環節點 { pNode fast =NULL; pNode low =NULL; pNode cur =NULL; pNode pcur =NULL; fast = head; low = head; while (fast && low) { fast=fast->next->next; low=low->next; if(fast == low) break; } cur=fast; pcur=head; while(cur!=pcur) { cur=cur->next; pcur=pcur->next; } return cur; } pNode TogetherCircle(pNode head1,pNode head2)//兩個帶環鏈表是否相交 { pNode fast=NULL; pNode low=NULL; pNode p=NULL; pNode q=NULL; pNode cur = NULL; fast = head1; low = head1; while (fast && low) { fast=fast->next->next; low=low->next; if(fast==low) break; } if (fast) p = fast; fast=head2; low=head2; while(fast) { fast=fast->next->next; low=low->next; if(fast==low) break; } if (fast) q=fast; if (q == NULL && p) return NULL; else if (p == NULL && q) return NULL; else if (p == NULL && q == NULL) Together(head1,head2); else if (p == q) return p; cur = p; cur = cur->next; while(cur != p) { if(cur==q) return cur; cur=cur->next; } return NULL; }