编辑代码

#include <stdio.h>  
#include <stdlib.h>  
  
// 定义链表节点结构  
typedef struct Node {  
    int data;  
    struct Node* next;  
} Node;  
  
// 创建一个新节点  
Node* createNode(int data) {  
    Node* newNode = (Node*)malloc(sizeof(Node));  
    if (!newNode) {  
        printf("内存分配失败\n");  
        exit(1);  
    }  
    newNode->data = data;  
    newNode->next = NULL;  
    return newNode;  
}  
  
// 使用尾插法批量插入节点  
void appendNodes(Node** head, int* dataArray, int count) {  
    Node* tail = NULL; // 初始时尾指针为空  
    Node* current = *head; // 当前指针指向头节点  
  
    // 遍历数据数组  
    for (int i = 0; i < count; i++) {  
        Node* newNode = createNode(dataArray[i]); // 创建新节点  
  
        // 如果链表为空,则新节点成为头节点  
        if (*head == NULL) {  
            *head = newNode;  
            tail = newNode;  
        } else {  
            // 否则,将新节点添加到链表末尾  
            tail->next = newNode;  
            tail = newNode;  
        }  
    }  
}  
  
// 打印链表  
void printList(Node* head) {  
    Node* current = head;  
    while (current != NULL) {  
        printf("%d -> ", current->data);  
        current = current->next;  
    }  
    printf("NULL\n");  
}  
  
// 释放链表内存  
void freeList(Node* head) {  
    Node* current = head;  
    Node* nextNode;  
    while (current != NULL) {  
        nextNode = current->next;  
        free(current);  
        current = nextNode;  
    }  
}  



int differCollection(Node* L1, Node* L2, Node* L3) {
    Node *p1 = L1, *p2 = L2;
    Node *p3 = L3;
    Node *tail = L3;
    int count = 0;
    while(p1 && p2) {
        if(p1->data < p2->data){
            tail->next = p1;
            tail = p1;
            count++;
            p1 = p1->next;
        }else if(p1->data > p2->data){
            p2 = p2->next;
        }else {
            p1 = p1->next;
            p2 = p2->next;
        }
    }

    while(p1) {
        tail->next = p1;
        tail=p1;
        count++;
        p1 = p1->next;
        
    }
    
    return count;
     
}

// 删除元素值最小元素
void delMin(Node* h) {
    Node *pre = h->next;
    Node *p = pre->next;
    // 设置最小值为首元
    Node *minp = h->next;
    Node *minpre = h;

    while(p != NULL) {
        if(p->data < minp->data){
            minp = p;
            minpre = pre;
        }

        pre = p;
        p = p->next;
    }

    minpre->next = minp->next;
    free(minp);

}

// 删除元素为x的项
void delX(Node *h, int x){
    Node *p = h->next;
    Node *pre = h;
    Node *temp;
    while(p != NULL) {
        if(p->data == x) {
            temp = p;
            pre->next = p->next;
            p = p->next;
            free(temp);
        }else{
            pre = p;
            p = p->next;
        }
    }
    
}


void delX2(Node *h, int x){
    Node *p = h->next;
    Node *head = p;
    Node *tail = p;
    while(p!=NULL){
        if(p->data != x){
            tail->next = p;
            tail = p;
        }
        p=p->next;
    } 
}
  
int main() {  
    Node* head1 = NULL; // 初始化头指针为空  
    int dataArray1[] = {2, 3, 2, 1, 2, 11, 23}; // 要插入的数据数组  
    int count1 = sizeof(dataArray1) / sizeof(dataArray1[0]); // 数据数组中的元素个数  
  


    Node* head2 = NULL; // 初始化头指针为空  
    int dataArray2[] = {1, 2, 3, 4, 6, 9, 12}; // 要插入的数据数组  
    int count2 = sizeof(dataArray2) / sizeof(dataArray2[0]); // 数据数组中的元素个数
    // 使用尾插法批量插入节点  
    appendNodes(&head1, dataArray1, count1);  
    appendNodes(&head2, dataArray2, count2);  
    
    Node* head3 = (Node*)malloc(sizeof(Node));
    int count = differCollection(head1, head2, head3);
    printf("个数%d\n", count);
    // 打印链表  
    printList(head1);  
    // printList(head2); 


    delMin(head1);
    printList(head1);


    delX2(head1, 2);
    printList(head1);
    return 0;  
}