Implement intersection, union, and difference operations of sets based on two-way circular linked lists

1. This code can be run directly, but some special symbols such as ? may become ? , you can modify it yourself

2. Experimental objectives: Through this experiment, students can review and consolidate the loop structure, loop control conditions, branch structures, arrays/linked lists, function calls and other related contents in C language, and realize that when using arrays to store collections, they need to Record the number of collection elements, otherwise the output result will have data out of bounds.

3. Code implementation (the two collections in the code are A and B respectively)

In this set calculator, you will play with the cute little Ivy. You can enter the following commands to interact with Ivy, and after the end, you can voluntarily choose whether to give a score and let her follow your commands (what do you want? , the serious kind,,, doge)

Chinese menu page:

1—-Input set A and set B

2—-View sets A and B

3—-Output the intersection of sets A and B

4—-Output the union of sets A and B

5—-Output the difference set of sets A and B

Note: Enter a negative number to terminate the program! ! !

Note: When entering a set, such as when entering set A, you need to enter a number, change the line, and then enter the next number. Otherwise, the program cannot detect whether the number is legal (because scanf is used, and I don’t know how to improve it. Take a look, maybe someone can give me some advice in the comment area~~, it’s in case 1)

// main.c
//Data Structure-Lab Assignment 1
//
// Created by *** on 08/09/2023.
// Intersect, merge, complement
//If a check function is added to the adding function, the adding function can be called directly in the intersection function without checking again.
//Boolean function header file
//How to change the font, background, etc. of the output part
//How to use comparison operators in switch
//Only expressions can be entered in the switch label, not declarations.
//The return value of scanf is the number of reads
//If isdigit is yes, return 0, if not, return 1
//Application of goto function
//fflush

#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<ctype.h>
#include<string.h>
#define DELAY_TIME 100000000

typedef int ElemType;
typedef struct ListNode{
    ElemType val;
    struct ListNode *next;
    struct ListNode *prev;
}MyLinkedList;

//Creation of circular doubly linked list, virtual head node and virtual tail node
MyLinkedList* MyLinkedListCreat(void){
    MyLinkedList *head, *tail;
    head = (MyLinkedList*)malloc( sizeof(MyLinkedList));
    tail = (MyLinkedList*)malloc( sizeof(MyLinkedList));
    
    head->next = tail;
    head->prev = tail;
    tail->next = head;
    tail->prev = head;
    head->val = -1;
    tail->val = -1;
    return head;
}

//Deletion of circular double-linked list (only fixed-point deletion, q points to the node to be deleted)
void MyLinkedListDelete(MyLinkedList *obj, struct ListNode *q){
    struct ListNode *p, *m;
    p = q->prev;
    m = q->next;
    
    p->next = q->next;
    m->prev = q->prev;
    q->next = NULL;
    q->prev = NULL;
    free(q);
}

//Addition of circular doubly linked list (only the tail is added, that is, the position before the virtual tail node)

bool MyLinkedListAddAtTail(MyLinkedList* obj, ElemType data){
    if(obj == NULL)
        return false;
    struct ListNode *newtail, *p, *tail;
    tail = obj->prev;
    p = tail->prev;
    
    struct ListNode *q;
    //newtail = (struct ListNode*)malloc( sizeof(struct ListNode) );
    q = obj->next;
    
    while(q & amp; & amp; q->val > 0 & amp; & amp; q != tail){
        
        if(q->val == data){
            //printf("Please enter a positive number again:\\
");
            return false;
        }
        q = q->next;
    }
    
    newtail = (struct ListNode*)malloc( sizeof(struct ListNode) );
    p->next = newtail;
    tail->prev = newtail;
    newtail->next = tail;
    newtail->prev = p;
    newtail->val = data;
    
    return true;
}

//Clear circular linked list
MyLinkedList* MyLinkedListFree(MyLinkedList *L){
    if(L == NULL || L->next == NULL)
        return NULL;
    struct ListNode *p = L->next;
    struct ListNode *q;
    while(p & amp; & amp; p != L->prev){ // The virtual tail node cannot be deleted, why? ?
        q = p->next;
        free(p);
        p = q;
    }
    L->next = L->prev; // Deletion of double linked list, pay attention to the analysis steps
    L->prev->prev = L;
    return L;
} //

//intersection function
MyLinkedList* FindIntersection(MyLinkedList *A, MyLinkedList *B){
    MyLinkedList *C;
    C = MyLinkedListCreat();
    struct ListNode *a, *b;
    a = A->next;
    b = B->next;
    
    for(; a & amp; & amp; (a->val > 0) ; a = a->next){
        for(b = B->next; b & amp; & amp; b->val > 0; b = b->next){
            if(a->val == b->val){
                MyLinkedListAddAtTail(C, a->val);
                break;
            }
            else
                continue;
        }
    }
    
    return C;
}

//Union function
MyLinkedList* FindUnion(MyLinkedList *A, MyLinkedList *B){
    MyLinkedList *C;
    C = MyLinkedListCreat();
    struct ListNode *a, *b;
    a = A->next;
    b = B->next;
    
    while( a & amp; & amp; b & amp; & amp; (a->val > 0 || b->val > 0) ){
        
        if(a->val > 0){
            MyLinkedListAddAtTail(C, a->val);
            a = a->next;
        }
        if(b->val > 0){
            MyLinkedListAddAtTail(C, b->val);
            b = b->next;
        }
        
    }
   
    return C;
}

//Difference set function
MyLinkedList* FindDifference(MyLinkedList *A, MyLinkedList *B){
    MyLinkedList *C;
    C = MyLinkedListCreat();
    struct ListNode *a, *b;
    a = A->next;
    b = B->next;
    int ok = 0;
    
    for(; a & amp; & amp; (a->val > 0) ; a = a->next){
        for(b = B->next; b & amp; & amp; b->val > 0; b = b->next){
            if(a->val == b->val){
                OK = 1;
                break;
            }
        }
        if(ok==0)
            MyLinkedListAddAtTail(C, a->val);
    }
    
    return C;
}


//delay function
void delay(unsigned long time){
    while(time--);
}

//Use the output of the delay function
void printer(char *text, int length){
    for(int i = 0; i<= length - 1; i + + ){
        printf("%c", text[i]);
        fflush(stdin);
        delay(DELAY_TIME);
    }
}

//Main function
int main(int argc, const char * argv[]) {
    
    // menu
    printer("? Welcome to the simple set calculator ?(Enter anything to continue)",
           strlen("? Welcome to the simple set calculator ?(Enter anything to continue)"));
    
    getchar();
    printer("My name is Ivy. It's so great to meet you.",
           strlen(" My name is Ivy. It's so great to meet you."));
    getchar();
    printf("------------------------------------------------");
    getchar();
    printer(" Please enter the corresponding number to order me to do something.",
           strlen(" Please enter the corresponding number to order me to do something."));
    getchar();
    printer(" 1 ---- Enter set A and B\\
 2 ---- View set A and B\\
 3 ---- Find the intersection of A and B\\
 4 ---- Find the union set of A and B\\
 5 ---- Find the difference set of A and B\\
 Attention: Enter a negative number to quit.",
           strlen(" 1 ---- Enter set A and B\\
 2 ---- View set A and B\\
 3 ---- Find the intersection of A and B\\
 4 ---- Find the union set of A and B\\
 5 ---- Find the difference set of A and B\\
 Attention: Enter a negative number to quit."));
    getchar();
    printf("------------------------------------------------");
    getchar();
    printer(" Wish you enjoy playing with the software\\
 I will be glad if you could offer me the feedback\\
 Thank you so much  (Enter anything to continue)\\
",
           strlen(" Wish you enjoy playing with the software\\
 I will be glad if you could offer me the feedback\\
 Thank you so much  (Enter anything to continue)\\
" ));
    printf("------------------------------------------------");
    getchar();
    
    int order, num, ok = 0;
    struct ListNode *a, *b, *i, *u, *d;
    MyLinkedList *A, *B, *I, *U, *D;
    char a1 = '\\
', b1 = '\\
';
    printf("Please give me an order:");
    A = MyLinkedListCreat();
    B = MyLinkedListCreat();
    
    //Determine the command entered by the user
    while(scanf("%d", & amp;order)){
        if(order < 0){
            break;
        }
        switch(order){
            case 1: //Input set, can be entered repeatedly
                if(ok){
                    printer("Do you want to recreat set A?(Please enter Y/N)\\
",
                            strlen("Do you want to recreat set A?(Please enter Y/N)\\
"));
                    fflush(stdin);
                    if((a1 = getchar()) == 'Y')
                        MyLinkedListFree(A);
                    fflush(stdin);
                    printer("Do you want to recreat set B?(Please enter Y/N)\\
",
                            strlen("Do you want to recreat set B?(Please enter Y/N)\\
"));
                    fflush(stdin);
                    if((b1 = getchar()) == 'Y')
                       MyLinkedListFree(B);
                }
                if(ok == 0 || (ok & amp; & amp;(a1 == 'Y'))){
                    printer("Please enter the first set A:(Enter a letter to finish)\\
",
                           strlen("Please enter the first set A:(Enter a letter to finish)\\
"));
                    while( scanf("%d", & amp;num) ){ // Boss, take a look, this is it, QAQ, I hope there is a better solution
                        if(num < 0){
                            goto final;
                        }
                        if(!MyLinkedListAddAtTail(A, num)){
                            printer("Please enter another positive number again.\\
",
                                    strlen("Please enter another positive number again.\\
"));
                            continue;
                        }
                        MyLinkedListAddAtTail(A, num);
                    }
                }
                fflush(stdin);
                if(ok == 0 || (ok & amp; & amp;(b1 == 'Y'))){
                    printer("\\
Please enter the second set B:(Enter a letter to finish)\\
",
                           strlen("\\
Please enter the second set B:(Enter a letter to finish)\\
"));
                    while( (scanf("%d", & amp;num) ) ){ // Same as above
                        if(num < 0 ){
                            goto final;
                        }
                        if(!MyLinkedListAddAtTail(B, num)){
                            printer("Please enter another positive number again.\\
",
                                   strlen("Please enter another positive number again.\\
"));
                            continue;
                        }
                        MyLinkedListAddAtTail(B, num);
                    }
                }
                fflush(stdin);
                printer("The two sets you entered are A and B:\\
 A is ",
                       strlen("The two sets you entered are A and B:\\
 A is "));
                a = A->next;
                while(a->val > 0){
                    printf("%d ", a->val);
                    a = a->next;
                }
                
                printer("\\
 B is ", strlen("\\
 B is "));
                b = B->next;
                while(b->val > 0){
                    printf("%d ", b->val);
                    b = b->next;
                }
                printf("\\
 ---------------------------------------------\ \
");
                ok++;
                break;
            case 2: // View the input collection
                if(A == NULL)
                    printer("Sorry, but you haven't told me what set A is.\\
", strlen("Sorry, but you haven't told me what set A is.\ n"));
                else{
                    printer("The first set A you inputed is :\\
",
                            strlen("The first set A you inputed is :\\
"));
                    a = A->next;
                    while(a->val > 0){
                        printf("%d ", a->val);
                        a = a->next;
                    }
                }
                printf("\\
");
                if(B == NULL)
                    printer("Sorry, but you haven't told me what set B is.\\
",
                           strlen("Sorry, but you haven't told me what set B is.\\
"));
                else{
                    printer("The second set B you inputed is :\\
",
                            strlen("The second set B you inputed is :\\
"));
                    b = B->next;
                    while(b->val > 0){
                        printf("%d ", b->val);
                        b = b->next;
                    }
                }
                printf("\\
 ---------------------------------------------\ \
");
                break;
            case 3: //Intersection of output sets
                I = FindIntersection(A, B);
                i = I->next;
                printer("The Intersection of A and B is ",
                       strlen("The Intersection of A and B is "));
                while(i->val > 0){
                    printf("%d ", i->val);
                    i = i->next;
                }
                printf("\\
 ------------------------------------------------\ \
");
                break;
            case 4: // Union of output sets
                U = FindUnion(A, B);
                u = U->next;
                printer("The Union Set of A and B is ", strlen("The Union Set of A and B is "));
                while(u->val > 0){
                    printf("%d ", u->val);
                    u = u->next;
                }
                printf("\\
 ------------------------------------------------\ \
");
                break;
            case 5: // Output the difference set of A-B
                D = FindDifference(A, B);
                d = D->next;
                printer("The Difference Set of A and B is ",
                       strlen("The Difference Set of A and B is "));
                while(d->val > 0){
                    printf("%d ", d->val);
                    d = d->next;
                }
                printf("\\
 ---------------------------------------------\ \
");
                break;
            default:
                printer("Your does not meet the requirement, please print again.\\
 Don't be so naughty, and try to be a good kid!",
                       strlen("Your does not meet the requirement, please print again.\\
 Don't be so naughty, and try to be a good kid!"));
                printf("\\
 ---------------------------------------------\ \
");
                  
                
                break;
        }
        printer("\\
Please enter another number to make another order\\
(Enter a negetive number to quit):\\
",
               strlen("\\
Please enter another number to make another order\\
(Enter a negetive number to quit):\\
"));
    }
        
// rating operation
final:printer("?Do you enjoy playing with me?\\
 Please rate me for my performance from 5 to 0( Enter any other numbers to quit )\\
",
             strlen("?Do you enjoy playing with me?\\
 Please rate me for my performance from 5 to 0( Enter any other numbers to quit )\\
"));
             
      printf("---------------------------------------------\\
\ ");
        
        int rate;
        scanf("%d", & amp;rate);
        
        switch(rate){
                
            case 5:
                printer("Thank you sooo much, sweet heart.\\
 It's so nice to enjoy the time with you.\\
 I'll try to work harder!!!\\
 Have a good day and welcome to play with me again.",
                       strlen("Thank you sooo much, sweet heart.\\
 It's so nice to enjoy the time with you.\\
 I'll try to work harder!!!\\
 Have a good day and welcome to play with me again."));
                break;
            case 4:
                printer("Thank you buddy, good luck.\\
",
                       strlen("Thank you buddy, good luck.\\
"));
                break;
            case 3:
                printer("Decent rate, thanks.\\
",
                       strlen("Decent rate, thanks.\\
"));
            case 2:
                printer("I'll try to improve myself day in, day out.\\
 Thank you so much for your feedback.\\
",
                        strlen("I'll try to improve myself day in, day out.\\
 Thank you so much for your feedback.\\
"));
                break;
            case 1:
                printer("I'll try to improve myself day in, day out.\\
 Thank you so much for your feedback.\\
",
                        strlen("I'll try to improve myself day in, day out.\\
 Thank you so much for your feedback.\\
"));
                break;
            case 0:
                printer("I'll try to improve myself day in, day out.\\
 Thank you so much for your feedback.\\
",
                        strlen("I'll try to improve myself day in, day out.\\
 Thank you so much for your feedback.\\
"));
                break;
            default:
                printer("Thank you\\
",
                        strlen("Thank you\\
"));
        }
        printer("\\
Goodbye~\\
",
               strlen("\\
Goodbye~\\
"));

        return 0;
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 54148 people are learning the system