1. Problem description
- It is known that two linked lists L1 and L2 represent two sets respectively, and their elements are arranged incrementally. Please design an algorithm to find the intersection of L1 and L2, and store it in the L1 list.
2. Topic analysis
- Only the elements that appear in both sets appear in the result table, and the merged new table points to with the header pointer L3. p1 and p2 are the working pointers of the linked list L1 and L2 respectively. They are initialized as the first node of the corresponding linked list. The comparison starts from the first node. When both linked lists L1 and L2 reach the end node of the table, if the equal elements in the two tables are extracted, the elements in the L1 table are deleted, and the elements in the L2 table are deleted. If the elements in one of the tables are small, the smaller elements in this table are deleted. , the working pointer of this table moves backward. When one of the linked lists La and Lb reaches the end node of the table and is empty, all elements in the other non empty table are deleted in turn.
// Linked list intersection void Intersection(LinkList &L1, LinkList &L2, LinkList &L3) { LNode *p1, *p2, *p3, *q, *u; p1 = L1->next; p2 = L2->next; // p1 and p2 are the working pointers of the linked list L1 and L2 respectively, which are initialized to the first node of the corresponding linked list. p3 = L3 = L1; // Using the head node of L1 as the head node of L3 while (p1&&p2) { if (p1->data == p2->data) //The intersection is incorporated into the result table. { p3->next = p1; p3 = p1; p1 = p1->next; u = p2; p2 = p2->next; delete u; } else if (p1->data < p2->data) { u = p1; p1 = p1->next; delete u; } else { u = p2; p2 = p2->next; delete u; } } while (p1) // Free node space { u = p1; p1 = p1->next; delete u; } while (p2) // Free node space { u = p2; p2 = p2->next; delete u; } p3->next = NULL; // Mark the end of the list. delete L2; // Release the head node of Lb }
3. Code implementation
- main.cpp
#include <iostream> using namespace std; typedef struct LNode { int data; struct LNode *next; }LNode, *LinkList; int InitList(LinkList &L) { L = new LNode; L->next = NULL; return 1; } void TraveList(LinkList L) { LNode *p; p = L->next; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } // Establishing chain list by tail insertion void CreateList(LinkList &L, int n) { L = new LNode; L->next = NULL; LNode *r; r = L; for (int i = 0; i < n; i++) { printf("Please enter the link list No.%d Values of elements:", i + 1); LNode *s; s = new LNode; scanf("%d", &s->data); s->next = NULL; r->next = s; r = s; } } // Linked list intersection void Intersection(LinkList &L1, LinkList &L2, LinkList &L3) { LNode *p1, *p2, *p3, *q, *u; p1 = L1->next; p2 = L2->next; // p1 and p2 are the working pointers of the linked list L1 and L2 respectively, which are initialized to the first node of the corresponding linked list. p3 = L3 = L1; // Using the head node of L1 as the head node of L3 while (p1&&p2) { if (p1->data == p2->data) //The intersection is incorporated into the result table. { p3->next = p1; p3 = p1; p1 = p1->next; u = p2; p2 = p2->next; delete u; } else if (p1->data < p2->data) { u = p1; p1 = p1->next; delete u; } else { u = p2; p2 = p2->next; delete u; } } while (p1) // Free node space { u = p1; p1 = p1->next; delete u; } while (p2) // Free node space { u = p2; p2 = p2->next; delete u; } p3->next = NULL; // Mark the end of the list. delete L2; // Release the head node of Lb } int main() { LinkList L1, L2, L3; if (InitList(L1)) { printf("L1 Initialization successful!\n"); } else { printf("L1 initialization failed!\n"); } if (InitList(L2)) { printf("L2 Initialization successful!\n"); } else { printf("L2 initialization failed!\n"); } if (InitList(L3)) { printf("L3 Initialization successful!\n"); } else { printf("L3 initialization failed!\n"); } printf("Please input L1 Length:"); int n1; scanf("%d", &n1); CreateList(L1, n1); TraveList(L1); printf("Please input L2 Length:"); int n2; scanf("%d", &n2); CreateList(L2, n2); TraveList(L2); Intersection(L1, L2, L3); printf("Linked list intersection:\n"); TraveList(L3); system("pause"); return 0; }
- Operation result