DS experiment 4--seeking the intersection and difference of singly linked lists (including test effect and implementation)

This experiment includes finding the intersection and difference of singly linked lists, and acting on a singly linked list. In order to test the two functions, several functions are needed. First: 1. Initialization 2. Tail interpolation 3. Delete node 4. Traverse the node. After these four functions are written, we can happily test the intersection and difference of 1. Let's talk about how intersection is done first.

intersection

Original title reproduction:

  • 1. It is known that two linked lists A and B respectively represent two sets, and their elements are arranged in increasing order. Please design an algorithm to find the intersection of A and B with thousands, and store it in the A linked list.

When you get the question, first look at his input, output and function, and then the function limits his output. The input is two singly linked lists, A is to be operated, so you need to add &. Core keywords: increment, intersection.
Assuming A = {1,2,3,4,8}, B={2,4,7,8,9}
then A∩B={2,4,8}

Algorithm ideas

  1. The two operation pointers p and q point to the data fields of the two linked lists
  2. The while loop judges that if p->data and q->data are equal, do not operate directly to the next one. If it is greater than q, jump to the next address. If it is less than q, delete the node, and finally the desired result is obtained.

According to the idea, the algorithm is very simple to convert into code.

void Inter(LinkList &A,LinkList B){
    
    
    LinkList  p = A->next;
    LinkList  q = B->next;
    while(p && q){
    
    
        if(p->data == q->data){
    
    
            p =p->next;
            q = q->next;
        }else if(p->data>q->data){
    
    
            q = q->next;
        }else{
    
    
            p = ListDelete(A,p->data);
            p = p->next;
        }
    }
}

difference

topic reproduction

  • 2. It is known that two linked lists A and B respectively represent two sets, and their elements are arranged in increasing order. Please design an algorithm
  • Find the difference of two sets A and B (that is, a set consisting only of elements that appear in A but not in B), store them in the same form, and return the number of elements in the set.

Suppose A = {1,2,3,4,8} B={2,4,7,8,9}
AB = {1,3} 2

Algorithm steps, the same as above

  1. The two operation pointers p and q point to the data fields of the two linked lists
  2. The while loop judges that if p->data and q->data are equal, delete the node. If it is greater than q, jump to the next address. If it is less than q, the counter +1.
int Differ(LinkList &A,LinkList B){
    
    
    int cnt=0;
    LinkList p = A->next;
    LinkList q = B->next;
    while(p && q){
    
    
        if(p->data == q->data){
    
    
            p = ListDelete(A,p->data);
            p = p->next;
            q = q->next;

        }else if(p->data>q->data){
    
    
            q = q->next;
        }else {
    
    
            p = p->next;
            cnt ++;
        }

    }
    return cnt;

}

Test effect

insert image description here

Complete source code


#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 10
using namespace std;
typedef int Status;
typedef int ElementType;

typedef struct LNode{
    
    
    ElementType data;
    struct LNode* next;
}LNode,*LinkList;

Status InitList(LinkList &L){
    
    
    L = new LNode;
    L->next = NULL;
    return OK;
}
void CreateList_H(LinkList &L,int A[],int n){
    
    
    InitList(L);
    LinkList r = L;
    for(int i =0;i<n;i++){
    
    
        LinkList  p = new LNode;
        p->data = A[i];
        p->next = NULL;
        r->next = p;
        r = p;
    }
}
void Print(LinkList L){
    
    
    LinkList p = L->next;
    while(p){
    
    
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}
LinkList ListDelete(LinkList &L,int x){
    
    
    LinkList p = L,pre;
    while(p->next != NULL && p->data !=x){
    
    
        pre = p;
        p=p->next;
    }
    pre->next = p->next;
    delete p;
    return pre;

}
/*
 *  * 1、已知两个链表A和B分别表示两个集合,其元素递增排列。
 *  请设计一个算法,用千求出A与B的交集,并存放在A链表中。
 * A = {1,2,3,4,8},B={2,4,7,8,9}
 * A∩B= {2,4,8}
 * */
void Inter(LinkList &A,LinkList B){
    
    
    LinkList  p = A->next;
    LinkList  q = B->next;
    while(p && q){
    
    
        if(p->data == q->data){
    
    
            p =p->next;
            q = q->next;
        }else if(p->data>q->data){
    
    
            q = q->next;
        }else{
    
    
            p = ListDelete(A,p->data);
            p = p->next;
        }
    }
}
/*
 * 2、巳知两个链表 A和 B分别表示两个集合,其元素递增排列。请设计算法
 * 求出两个集合 A和B 的差集(即仅由在A中出现而不在B中出现的元素所构成的集合),并以同样的形式存储,同时返回该集合的元素个数。
 * A = {1,2,3,4,8} B={2,4,7,8,9}
 * A -B={1,3} 2个
 * */
int Differ(LinkList &A,LinkList B){
    
    
    int cnt=0;
    LinkList p = A->next;
    LinkList q = B->next;
    while(p && q){
    
    
        if(p->data == q->data){
    
    
            p = ListDelete(A,p->data);
            p = p->next;
            q = q->next;

        }else if(p->data>q->data){
    
    
            q = q->next;
        }else {
    
    
            p = p->next;
            cnt ++;
        }

    }
    return cnt;

}
int main(){
    
    
    LinkList  A,B;
    int a[5]={
    
    1,2,3,4,8};
    int b[5] ={
    
    2,4,7,8,9};
    CreateList_H(A,a,5);
    CreateList_H(B,b,5);
    cout << "A list:" << endl;
    Print(A);
    cout << "B list:" << endl;
    Print(B);
    cout << "A and B intersect: " << endl;
    Inter(A,B);
    Print(A);
    cout << "A and B Difference:" << endl;
    CreateList_H(A,a,5);
    int cnt = Differ(A,B);

    Print(A);
    cout << "cnt:" << endl;
    cout << cnt << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_37149062/article/details/123607337