有序链表操作

  • 实验目的
      1. 掌握有序链表的基本操作:插入、删除、查找。
      2. 掌握链表遍历器的使用方法。
      3. 禁止采用先数组排序,然后了对排序的数组创建链表办法。
  • 实验内容
  1. 输入n个不为零的整数作为节点元素值,遇到0代表输入结束(不创建元素值为0的节点),创建第一个有序链表。输出整个链表。如果输入的第一个数为0,则创建空链表(没有任何元素的链表)。如果是空链表,则输出内容为<null>(以下相同)。
  2. 再次输入n个不为零的整数作为节点元素值,遇到0代表输入结束(不创建元素值为0的节点),创建第二个有序链表。输出整个链表。
  3. 使用链表遍历器实现上面两个有序链表的合并,输出合并后的有序链表。
  4. 输入一个整数,将该数插入到合并后的有序链表正确位置,输出整个链表。
  5. 输入一个整数,在链表中查找这个数,找到第一个后将该节点删除,输出整个链表。
  6. 输入一个整数,在合并后的链表中进行搜索,输出其在链表中的第一次出现的位置。如果不存在输出0。

#include <bits/stdc++.h>
using namespace std;

struct chainNode {
    int element;
    chainNode *next;
    chainNode() {
        element = NULL;
        next = NULL;
    }
    chainNode(const int &element) {
        this->element = element;
        this->next = NULL;
    }
    chainNode(const int &element, chainNode *next) {
        this->element = element;
        this->next = next;
    }
};


class chain {
    public:
        chain(int capacity = 10) {
            firstNode = NULL;
            listSize = 0;
        }
        void insert(const int &iElement);
        void output() const;
        void find(int iElement);
        void deletion(int &iElement);
    public:
        chainNode *firstNode;
        int listSize;
};

void chain::output() const {
    if (listSize == 0) {
        cout << "<null>" << endl;
    } else {
        cout << firstNode->element;
        chainNode *temp = firstNode->next;
        while (temp) {
            cout << "," << temp->element;
            temp = temp->next;
        }
        cout << endl;
    }
}

void chain::find(int iElement) {
    if (listSize == 0) {
        cout << 0 << endl;
        return;
    } else {
        int index = 1;
        chainNode *temp = firstNode;
        while (temp) {
            if (temp->element == iElement) {
                cout << index << endl;
                return;
            }
            temp = temp->next;
            index++;
        }
        cout << 0 << endl;
    }
}

void chain::deletion(int &iElement) {
    if (listSize == 0) {
        return;
    } else if (firstNode->element == iElement) {
        firstNode = firstNode->next;
        listSize--;
        return;
    } else {
        chainNode *temp = firstNode;
        while (temp) {
            if (temp->next->element == iElement) {
                temp->next = temp->next->next;
                return;
            }
            temp = temp->next;
        }
        listSize--;
    }
}

void chain::insert(const int &iElement) {
    chainNode *temp = new chainNode(iElement);
    if (listSize == 0) {
        firstNode = temp;
    } else {
        chainNode *node = firstNode;
        chainNode *p;
        int index = 0;
        for (; index < listSize  && node->element < iElement; index++) {
            p = node;
            node = node->next;
        }
        if (index == 0) {
            temp->next = node;
            firstNode = temp;
        } else {
            p->next = temp;
            temp->next = node;
        }
    }
    listSize++;
}

void combine(chain &list1, chain &list2) {
    if (list1.listSize == 0 && list2.listSize == 0) {
        cout << "<null>" << endl;
        return;
    } else if (list1.listSize == 0) {
        list1 = list2;
    } else if (list2.listSize == 0) {
        list1.output();
        return;
    } else {
        chainNode *p = list1.firstNode;
        chainNode *q = list2.firstNode;
        chainNode *temp;
        while (p->element > q->element) {
            temp = p;
            p = q;
            q = q->next;
            p->next = temp;
        }
        while (p->next && q->next) {
            if (p->next->element < q->element) {
                p = p->next;
            } else {
                temp = q;
                q = q->next;
                temp->next = p->next;
                p->next = temp;
                p = p->next;
            }
        }
        if (q->next) {
            p->next = q->next;
        }
        list1.listSize += list2.listSize;
        list1.insert(q->element);
        list1.output();
    }
}

int main() {
    cout << "Input1" << endl;
    chain list1(10);
    int elmt = 0;
    cin >> elmt;

    while (elmt) {
        list1.insert(elmt);
        cin >> elmt;
    }
    cout << "Output1" << endl;
    list1.output();
    cout << "Input2" << endl;
    chain list2(10);
    cin >> elmt;

    while (elmt) {
        list2.insert(elmt);
        cin >> elmt;
    }
    cout << "Output2" << endl;
    list2.output();
    cout << "Combine" << endl;
    combine(list1, list2);
    cout << "Insert" << endl;
    cin >> elmt;
    list1.insert(elmt);
    cout << "Insertion" << endl;
    list1.output();
    cout << "Delete" << endl;
    cin >> elmt;
    list1.deletion(elmt);
    cout << "Deletion" << endl;
    list1.output();
    cout << "Find" << endl;
    cin >> elmt;
    cout << "Position" << endl;
    list1.find(elmt);
    cout << "End";
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值