【C++OJ_链表】单链表(类与构造)

【C++OJ_链表】单链表(类与构造)

题目描述

单链表节点的存储结构为:

struct SNode

{

int data;

SNode *next;

};

定义单链表类CList,数据成员有表头指针(SNode *head),成员函数有:

  1. 构造函数:初始化head.

  2. createList(int *value, int n): 用value中的n个数据创建单链表.

  3. printList(): 以head为表头依次输出每个节点的数据值。

  4. insertNode(int pos, int value): 在单链表第pos个节点位置后插入新节点,数据为value。如插入不成功输出error。

  5. removeNode(int pos): 删除第pos个节点,若删除不成功输出error。

  6. 析构函数:释放链表每个节点的堆内存。

输入

第一行:测试次数

每次测试格式为:

数据个数n 数据1 数据2 … 数据n

插入次数m

插入位置1 数据1

插入位置m 数据m

删除次数k

删除位置1

删除位置k

输出

每次测试产生的输出格式为:

输出创建链表后全部节点的数据

对每组插入测试数据:

输出插入操作后链表全部节点的数据

对每组删除测试数据:

输出删除操作后链表全部节点的数据

输入样例

2
5 2 3 5 7 3
2
1 40
7 60
2
7
3
6 1 2 10 0 1 5
2
1 40
7 60
2
1
3

输出样例

2 3 5 7 3
2 40 3 5 7 3
error
error
2 40 5 7 3
1 2 10 0 1 5
1 40 2 10 0 1 5
1 40 2 10 0 1 5 60
40 2 10 0 1 5 60
40 2 0 1 5 60

参考代码

#include <iostream>
using namespace std;

struct SNode
{
    int data;
    SNode *link;
    SNode(int d = 0, SNode *l = NULL) : data(d), link(l) {}
};

class CList
{
private:
    SNode *head;

public:
    CList(SNode * = NULL);
    void createList(int *value, int n);
    void printList();
    int insertNode(int pos, int value);
    int removeNode(int pos);
    ~CList(){}
};
CList::CList(SNode *h) : head(h)
{
}
void CList::createList(int *value, int n)
{
    while (--n >= 0)
    {
        head = new SNode(value[n], head);
    }
    printList();
}
void CList::printList()
{
    SNode *p;
    for (p = head; p->link != NULL; p = p->link)
    {
        cout << p->data << ' ';
    }
    cout << p->data << endl;
}
int CList::insertNode(int pos, int value)
{
    if (pos == 1)
    {
        SNode *p = new SNode(value, head->link);
        head->link = p;
        printList();
        return 0;
    }
    else
    {
        SNode *p = new SNode;
        SNode *r;
        r = head;
        p->data = value;
        pos--;
        while (pos--)
        {
            if (r->link != NULL)
                r = r->link;
            else
            {
                cout << "error\n";
                return 1;
            }
        }
        p->link = r->link;
        r->link = p;
        printList();
        return 0;
    }
}
int CList::removeNode(int pos)
{
    if (pos == 1)
    {
        SNode *p;
        p = head;
        head = head->link;
        delete p;
        printList();
        return 0;
    }
    else
    {
        SNode *p, *r;
        r = head;
        pos--;
        while (pos--)
        {
            p = r;
            if (r->link != NULL)
                r = r->link;
            else
            {
                cout << "error\n";
                return 1;
            }
        }
        p->link = r->link;
        delete r;
        printList();
        return 0;
    }
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        CList l;
        int tt;
        cin >> tt;
        int *value = new int[tt];
        int i;
        for (i = 0; i < tt; i++)
        {
            cin >> value[i];
        }
        l.createList(value, tt);
        int in;
        cin >> in;
        while (in--)
        {
            int pos, val;
            cin >> pos >> val;
            l.insertNode(pos, val);
        }
        int re;
        cin >> re;
        while (re--)
        {
            int pos;
            cin >> pos;
            l.removeNode(pos);
        }
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ferry_xie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值