Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
4
Indexable
Never
#include <iostream>



using namespace std;

struct node{
    int data;
    struct node *next;
};
class linked_list{
private:
    node *head,*tail;
    int size;
public:
    linked_list(){
        head = NULL;
        tail = NULL;
        size = 0;
    }
    void add_node(int n){
        node *tmp = new node;
        tmp->data = n;
        tmp->next = NULL;

        if(head == NULL){
            head = tmp;
            tail = tmp;
        }else{
            tail->next = tmp;
            tail = tail->next;
        }
        size++;
    }
    int getSize() {
        return this->size;
    }
    void show() {
        node *tmp = head;
        while (tmp != NULL) {
            cout << tmp->data << " ";
            tmp = tmp->next;
        }
        cout << endl;
    }
    int at(int index) {
        node *tmp = head;
        int counter = 0;
        while (counter < index) {
            tmp = tmp->next;
            counter++;
        }
        return tmp->data;
    }
};

int main(){
    int n, m;
    cin >> n >> m;
    int num;
    linked_list A;
    for (int i = 0; i < n; ++i) {
        cin >> num;
        A.add_node(num);
    }
    linked_list B;
    for (int i = 0; i < m; ++i) {
        cin >> num;
        B.add_node(num);
    }
    
    linked_list Z;
    if (n < m) {
        for (int i = 0; i < n; ++i) {
            Z.add_node(A.at(i));
            Z.add_node(B.at(i));
        }
        for (int i = n; i < m; ++i) {
            Z.add_node(B.at(i));
        }
    } else {
        for (int i = 0; i < m; ++i) {
            Z.add_node(A.at(i));
            Z.add_node(B.at(i));
        }
        for (int i = m; i < n; ++i) {
            Z.add_node(A.at(i));
        }
    }

    cout << Z.getSize() << endl;
    Z.show();

    return 0;
}