Set operation of static linked list
Title:
Using static linked list algorithm to realize set operation (A-B) U (B-A)
Example 2-3: suppose that the terminal inputs the set element, first establishes A static linked list S representing set A, and then looks up the S table while inputting the elements of set B. if there is the same element as B, then delete it from the S table, otherwise insert this element into the S table.
This algorithm is designed very cleverly. In the algorithm, R is always set to point to the last element in input set A, to ensure that the element to be inserted in B is only compared with the element in A, and will not be compared with the element in B inserted later. At this point, you need to consider the details, the movement of R. when the deleted element is the element pointed to by R (that is, the last element in A), you need to move r forward.
In addition, the insertion of elements in B follows the insertion as r, so it should be noted that in the final output, the insertion elements in B are in reverse order.
typedef int Status; /* Status Is the type of function whose value is the function result status code, such as OK */ typedef int Boolean; /* Boolean Is a boolean type with a value of TRUE or FALSE */ typedef char ElemType; #Include < malloc. H > / * malloc(), etc*/ #Include < stdio. H > / * EOF (= ^ Z or F6),NULL*/ #include<process.h> /* exit() */ /* Function result status code */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 //#define OVERFLOW -2 /* ----------------------- Storage structure of static single chain table of linear table------------------------*/ //See type description below P31 in textbook #define MAXSIZE 100 / * maximum length of linked list*/ typedef struct { ElemType data; int cur; }component, SLinkList[MAXSIZE]; /* ---------------------------------------------------------------------------*/ void visit(ElemType c) { printf("%c ", c); } /* ---------------------------- Basic operation implementation of static single chain table of linear table needed---------------------------------*/ int Malloc(SLinkList space) /* Algorithm 2.15 */ { /* If the spare list is not empty, then the allocated node subscript (the first node of the spare list) will be returned, otherwise 0 will be returned */ int i = space[0].cur; if (i) /* Spare list is not empty */ space[0].cur = space[i].cur; /* The head node of the spare list points to the second node of the original spare list */ return i; /* Return to the coordinates of the newly opened node */ } void Free(SLinkList space, int k) /* Algorithm 2.16 */ { /* Reclaim the free node with subscript k to the spare list (become the first node of the spare list) */ space[k].cur = space[0].cur; /* The cursor of the recycle node points to the first node of the standby linked list */ space[0].cur = k; /* The head node of the spare list points to the newly recovered node */ } void InitSpace(SLinkList L) /* Algorithm 2.14. */ { /* Each component in a dimension group L is chained into a spare list, with L[0].cur as the head pointer. "0" means null pointer */ int i; for (i = 0; i < MAXSIZE - 1; i++) L[i].cur = i + 1; L[MAXSIZE - 1].cur = 0; } Status ListTraverse(SLinkList L, int n, void(*vi)(ElemType)) { /* Call function vi() for each data element of the linked list whose header sequence is n in L. If vi() fails, the operation fails */ int i = L[n].cur; /* Point to first element */ while (i) /* Not to the end of static list */ { vi(L[i].data); /* Call vi() */ i = L[i].cur; /* Point to next element */ } printf("\n"); return OK; } /* -----------------------------------------------------------------------------------------------------*/ void difference(SLinkList space, int *S) /* Algorithm 2.17 */ { /* Input the elements of set a and B in turn, and establish the representation set (A-B) ∪ (B-A) in the one-dimensional array space */ /* S is its head pointer. Assuming that the spare space is large enough, space[0].cur is the head pointer of the spare space */ int r, p, m, n, i, j, k; ElemType b; InitSpace(space); /* Initialize spare space */ *S = Malloc(space); /* Generate S's header node */ r = *S; /* r Current last node pointing to S */ printf("Please enter a collection A and B Number of elements of m,n:"); scanf_s("%d %d%*c", &m, &n); /* %*c Eat carriage return */ printf("Please enter a collection A Elements of%d One):", m); for (j = 1; j <= m; j++) /* Set up A linked list of set A */ { i = Malloc(space); /* Distribution node */ scanf_s("%c", &space[i].data); /* Enter the element value of A */ space[r].cur = i; /* Insert at end of table */ r = i; } scanf_s("%*c"); /* %*c Eat carriage return */ space[r].cur = 0; /* The pointer of the end node is null */ printf("Please enter a collection B Elements of%d One):", n); for (j = 1; j <= n; j++) { /* Enter the elements of B in turn. If they are not in the current table, insert them. Otherwise, delete them */ scanf_s("%c", &b); p = *S; k = space[*S].cur; /* k Point to the first node in set A */ while (k != space[r].cur&&space[k].data != b) { /* Find in current table */ p = k; k = space[k].cur; } if (k == space[r].cur) { /* This element does not exist in the current table. It is inserted after the node indicated by r, and the position of r remains the same */ i = Malloc(space); space[i].data = b; space[i].cur = space[r].cur; space[r].cur = i; } else /* The element is already in the table, delete it */ { space[p].cur = space[k].cur; Free(space, k); if (r == k) r = p; /* If the tail element is deleted, the tail pointer needs to be modified */ } } } void main() { int k; SLinkList s; difference(s, &k); ListTraverse(s, k, visit); }
Operation result: