23作业2 c++基础2编程题7-8 单向链表3

文章详细描述了如何使用C++编程语言实现一个动态单向链表,包括链表的创建、插入有序整数以及输出链表的有序序列。通过给定的示例展示了如何处理重复输入并保持链表有序。
摘要由CSDN通过智能技术生成

7-8 单向链表3

分数 10

全屏浏览

切换布局

作者 wangxiu

单位 福州大学

编程实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:

输入一个正整数 n(0<n<=9)和一组(n个)升序的整数,建立单向链表,再输入一个整数 x,把 x 插入到这组数据中,使该组数据仍然有序。

输入输出示例:括号内为说明

输入样例:

4               (repeat=4) 
5               (数据的个数n=5)
1 2 4 5 7       (5个有序整数)
3               (待插入整数x=3)
4               (数据的个数n=4)
1 2 5 7	        (4个有序整数)
-10             (待插入整数x=-10)
3               (数据的个数n=3)
1 2 4	        (3个有序整数)
100             (待插入整数x=100)
5               (数据的个数n=5)
1 2 4 5 7       (5个有序整数)
4               (待插入整数x=4)

输出样例:

size=6:1 2 3 4 5 7 
size=5:-10 1 2 5 7 
size=4:1 2 4 100 
size=6:1 2 4 4 5 7

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

我的解答:

#include<iostream>
using namespace std;

//创建链表的一个框框
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) :val(x), next(nullptr) {}
};

//创建链表
class LinkedList {
public:
    ListNode* head;
    //创建头指针
    LinkedList() {
        head = NULL;
    }
    //输入框框函数
    void append(int val) {
        ListNode* newNode = new ListNode(val);
        if (!head) {
            head = newNode;
            return;
        }
        ListNode* current = head;
        while (current->next) {
            current = current->next;
        }
        current->next = newNode;
    }
    //输入链表
    void input(int n) {
        int val;
        for (int i = 0; i < n;i++){
            cin >> val;
            append(val);
        }
    }
    //插入按照顺序...
    void insert(int val) {
        ListNode* newNode = new ListNode(val);
        
        if (!head || val < head->val) {
            newNode->next = head;
            head = newNode;
            return;
        }
        
        ListNode* current = head;
        while (current->next && current->next->val < val) {
            current = current->next;
        }
        
        newNode->next = current->next;
        current->next = newNode;
    }

    int lcnt() {
        ListNode* current = head;
        int cnt = 0;
        while (current) {
            cnt++;
            current = current->next;
        }
        return cnt;
    }

    void print() {
        ListNode* current = head;
        while (current) {
            cout << current->val;
            if (current->next) {
                cout << " ";
            }
            current = current->next;
        }
        cout << endl;
    }
};


int main() {
    LinkedList list[10];
    int repeat;
    cin >> repeat;
    for (int i = 0; i < repeat; i++) {
        int n, x;
        cin >> n;
        list[i].input(n);
        cin >> x;
        list[i].insert(x);
    }
    for (int i = 0; i < repeat; i++) {
        cout << "size="<<list[i].lcnt()<<":";
        list[i].print();
    }
    return 0;
}

我总感觉,事实上用list[]的方式解题会有点奇怪

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值