数据结构 - 从一个链表中删除在另一个链表中的元素(C++)

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

/*
 * Created by Chimomo
 *
 * Sort both lists in ascending order. And then use the following code.
 */

#include <iostream>

using namespace std;

struct Node {
    char data;
    Node *next;
};

Node *create() {
    Node *head = NULL;
    Node *rear = head;
    Node *p; // The pointer points to the new created node.
    char tmp;

    do {
        cout << "Please input integer or char '#':";
        cin >> tmp;
        if (tmp != '#') {
            p = new Node;
            p->data = tmp;
            p->next = NULL;

            if (head == NULL) {
                head = p;
            } else {
                rear->next = p;
            }

            rear = p;
        }
    } while (tmp != '#');

    return head;
}

void print(Node *head) {
    Node *p = head;
    if (head != NULL) {
        do {
            cout << p->data << ' ';
            p = p->next;
        } while (p != NULL);
    }
    cout << endl;
}

Node *del(Node *list1, Node *list2) {
    if (!list1 || !list2) {
        return NULL;
    }

    Node *p = new Node;
    p->next = list1;
    Node *pL1 = list1, *pL2 = list2, *head = p;

    while (pL1 && pL2) {
        if (pL1->data < pL2->data) {
            pL1 = pL1->next;
            p = p->next;
        } else if (pL1->data > pL2->data) {
            pL2 = pL2->next;
        } else {
            pL1 = pL1->next;
            p->next = pL1;
        }
    }

    list1 = head->next;
    return list1;
}

int main() {
    cout << "Please input the first list:" << endl;
    Node *list1 = create();
    cout << endl << "Please input the second list:" << endl;
    Node *list2 = create();

    cout << endl << "------" << endl;
    cout << "The first list is: ";
    print(list1);
    cout << "The second list is: ";
    print(list2);

    Node *list = del(list1, list2);
    cout << "The result list is: ";
    print(list);

    return 0;
}

// Output:
/*
Please input the first list:
Please input integer or char '#':1
1
Please input integer or char '#':1
1
Please input integer or char '#':1
1
Please input integer or char '#':1
1
Please input integer or char '#':2
2
Please input integer or char '#':3
3
Please input integer or char '#':4
4
Please input integer or char '#':4
4
Please input integer or char '#':5
5
Please input integer or char '#':5
5
Please input integer or char '#':5
5
Please input integer or char '#':6
6
Please input integer or char '#':6
6
Please input integer or char '#':6
6
Please input integer or char '#':7
7
Please input integer or char '#':8
8
Please input integer or char '#':9
9
Please input integer or char '#':9
9
Please input integer or char '#':9
9
Please input integer or char '#':#
#

Please input the second list:
Please input integer or char '#':1
1
Please input integer or char '#':3
3
Please input integer or char '#':5
5
Please input integer or char '#':7
7
Please input integer or char '#':9
9
Please input integer or char '#':9
9
Please input integer or char '#':9
9
Please input integer or char '#':9
9
Please input integer or char '#':9
9
Please input integer or char '#':#
#

------
The first list is: 1 1 1 1 2 3 4 4 5 5 5 6 6 6 7 8 9 9 9
The second list is: 1 3 5 7 9 9 9 9 9
The result list is: 2 4 4 6 6 6 8

*/

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页