顺序表-链表实现

#include
#include
#include
#define ll long long 
using namespace std;


class Node {
public:
    int data;
    Node* next;
    Node() { next = NULL; }
    Node(int data, Node* next) {
        this->data = data;
        this->next = next;
    }
};

class Linearlist {
    Node* head;
    int size;
public:
    Linearlist(){
        int n;
        cin >> n;
        size = n;
        head = new Node(size,NULL);
        Node* p = head;
        for (int i = 0; i < size; i++) {
            cin >> n;
            Node* q = new Node(n, NULL);
            p->next = q;
            p = q;
        }
        
    }

    // 插入
    void insert(int idx, int v) {
        if (idx > size + 1 || idx < 1) {
            cout << "error" << endl;
            return;
        }
        Node* p = head;
        for (int i = 1; i < idx; i++) {
            p = p->next;
        }
        Node* q = new Node(v, p->next);
        p->next = q;
        size++;
        print();
    }

    // 删除
    void erease(int idx) {
        if (idx > size || idx < 1) {
            cout << "error" << endl;
            return;
        }
        Node* p = head;
        for (int i = 1; i < idx; i++) {
            p = p->next;
        }
        Node* q = p->next;
        p->next = q->next;
        delete q;
        size--;
        print();
    }

    // 查询
    void check(int idx) {
        if (idx > size || idx < 1) {
            cout << "error" << endl;
            return;
        }
        Node* p = head;
        for (int i = 1; i <= idx; i++) {
            p = p->next;
        }
        cout << p->data;
    }
    void print() {
        Node* p = head;
        for (int i = 1; i <= size; i++) {
            p = p->next;
            cout << p->data << " ";
        }
        cout << endl;
    }

    // 交换链表中两节点的位置
    void swap(int idx1, int idx2) {
        if (idx1 > size || idx1 < 1 || idx2 > size || idx2 < 1) {
            cout << "error" << endl;
            return;
        }
        Node* p = head;
        for (int i = 1; i <= idx1; i++) {
            p = p->next;
        }
        Node* q = head;
        for (int i = 1; i <= idx2; i++) {
            q = q->next;
        }
        Node* tmp1 = p->next;
        Node* front = head;
        for (int i = 1; i < idx2; i++) {
            front = front->next;
        }
        p->next = q->next;
        front->next = p;

        front = head;
        for (int i = 1; i < idx1; i++) {
            front = front->next;
        }
        q->next = tmp1;
        front->next = q;
        print();
    }

    // 按递增顺序合并两个递增的链表
    void merge(Linearlist& list2) {
        Node* p = head->next;
        Node* q = list2.head->next;
        Node* c = head;
        while (p && q) {
            if (p->data <= q->data) {
                c->next = p;
                c = p;
                p = p->next;
            }
            else {
                c->next = q;
                c = q;
                q = q->next;
            }
        }
        c->next = p ? p : q;

        p = head;
        for (int i = 1; i <= size + list2.size; i++) {
            p = p->next;
            cout << p->data << " ";
        }
        cout << endl;
    }
};

你可能感兴趣的:(数据结构,链表,c++,算法)