Experimental ideas
1. Build a linked list;
2. Function of each function
3. Debug
Require:
1 The purpose of the experiment
This experiment is to realize the operations of union, intersection and difference of sets (represented by a singly linked list).
2 Experimental content
Implement the union, intersection and difference operations of sets (represented by a singly linked list), and design a main program on this basis to complete the following functions:
(1) Initialize the set A {'c','a','e','h'} , B {'f','h','b','g','d','a'} and the empty set C
(2) Create three linked lists to store sets A , B and C respectively
(3) Output the result of the union operation of sets A and B
(4) Output the result of the intersection of sets A and B
(5) Output the difference operation result of sets A and B
(6) Release three linked lists
The specific code is as follows:
#include<stdio.h> #include<stdlib.h> //******macro definition parameters****** #define OK 1 #define NO 0 #define DATA_MAX_SIZE 20 //******Define data type aliases****** typedef int Status; typedef char Excelelem; typedef int Numelem; //******Declaration data structure****** typedef struct Node { Excelelem book; struct Node *next; }LNode,*LinkList; typedef struct { Excelelem name[100]; Numelem length; LinkList next; }HeadList,*HeadLinkList; //******Initialize linked list****** LinkList init(int *i) { LinkList Head,p,q; Excelelem ch; Head=q=NULL; while((ch=getchar())!='\n') { p=(LinkList)malloc(sizeof(LNode)); p->book=ch; if (!(*i)) Head=q=p,(*i)++; else { q->next=p; q=p (*i)++; } } if(p) p->next=NULL; return Head; } HeadLinkList HeadInit() { //Omit the header information Head->name HeadLinkList Head; Head=(HeadLinkList)malloc(sizeof(HeadList)); Head->length=0; Head->next=init(&Head->length); return Head; } //******Output the information in the linked list****** void DATA_cout(HeadLinkList Head) { LinkList p=Head->next; while(p!=NULL) { printf("%c",p->book); p=p->next; } printf("\n"); return ; } //******Return memory****** void DATA_Free(HeadLinkList Head) { LinkList q=Head->next,p; while (q!=NULL) { p=q q=q->next; free(p); } Head->length=0; Head->next=NULL; return ; } //******Insert an element ch****** before i position void DATA_Insert(HeadLinkList Head,Numelem k,Excelelem ch) { int i=1; LinkList q=Head->next,p,t; if(Head->length && (Head->length<k || k<1)) { printf("Warning! The location of %d is invalid\n",k); return ; } while(p && i++ < k) p=q,q=q->next; t=(LinkList)malloc(sizeof(LNode)); t->book=ch; if(k==1) { Head->next=t; t->next=q; } else { t->next=p; q->next=t; } Head->length++; return ; } //******Find if the character ch****** appears int DATA_find(HeadLinkList Head,Excelelem ch) { LinkList q=Head->next; while(q!=NULL && q->book!=ch) q=q->next; if(q==NULL) return NO; return OK; } //******AWAY****** void SetJiao(HeadLinkList A,HeadLinkList B,HeadLinkList C) { LinkList q=A->next; DATA_Free(C); //Initialize the result set C to prevent memory leaks while (q!=NULL) { if(DATA_find(B,q->book) && !DATA_find(C,q->book)) DATA_Insert(C,1,q->book); q=q->next; } return ; } //******A+B****** void SetBing(HeadLinkList A,HeadLinkList B,HeadLinkList C) { LinkList q=A->next; DATA_Free(C); while(q!=NULL) { if(!DATA_find(C,q->book)) DATA_Insert(C,1,q->book); q=q->next; } q=B->next; while(q!=NULL) { if(!DATA_find(C,q->book)) DATA_Insert(C,1,q->book); q=q->next; } return ; } //******A-B****** void SetCha(HeadLinkList A,HeadLinkList B,HeadLinkList C) { LinkList q=A->next; DATA_Free(C); while(q!=NULL) { if(!DATA_find(B,q->book) && !DATA_find(C,q->book)) DATA_Insert(C,1,q->book); q=q->next; } return ; } /*************************** The above is an O(n*n) algorithm There is also an O(n) algorithm, that is, preprocessing when building a set, mapping the information of the set to an ASCLL table, and then looking up the table directly, eliminating the step of traversing the linked list. The look-up table does not make redundant judgments. During preprocessing, the boundaries of the data should be recorded. Intersection operation: if book[ch]==2 C.Insert(ch); And operation: if book[ch]!=0 C.Insert(ch); Difference operation: if book[ch]==1 C.Insert(ch); *****************************/ intmain() { HeadLinkList A,B,C; C=(HeadLinkList)malloc(sizeof(HeadList)); C->next=NULL; C->length=0; printf("******initialization set A******\n"); A=HeadInit(); printf("******initialization set B******\n"); B=HeadInit(); printf("*****The result of set AB*****\n"); SetJiao(A,B,C); DATA_cout(C); printf("*****The result of set A+B*****\n"); SetBing(A,B,C); DATA_cout(C); printf("*****The result of set AB*****\n"); SetCha(A,B,C); DATA_cout(C); printf("***********************\n"); printf("*******Release memory*******\n"); DATA_Free(A),DATA_Free(B),DATA_Free(C); free(A),free(B),free(C); printf("**********End!**********\n"); return 0; }
Summarize:
There are many ways to implement set operations. The specific steps include searching and deduplication. There is also an algorithm in Yan Weimin's "Data Structure C Language Edition" book, but it is also O(n*n). The idea is basically similar to the above code. However, if the input characters ASCLL are not very different, you can build a table, so that it can be done once and for all, but if the input data is relatively sparse, the time will not make any difference, and even O(n*n) will be faster, depending on the requirements. Depends!