A. DS单链表--类实现

题目描述
用C++语言和类实现单链表,含头结点
属性包括:data数据域、next指针域
操作包括:插入、删除、查找
注意:单链表不是数组,所以位置从1开始对应首结点,头结点不放数据
类定义参考
输入

第1行先输入n表示有n个数据,接着输入n个数据
第2行输入要插入的位置和新数据
第3行输入要插入的位置和新数据
第4行输入要删除的位置
第5行输入要删除的位置
第6行输入要查找的位置
第7行输入要查找的位置
输出

数据之间用空格隔开,
第1行输出创建后的单链表的数据
每成功执行一次操作(插入或删除),输出执行后的单链表数据
每成功执行一次查找,输出查找到的数据
如果执行操作失败(包括插入、删除、查找等失败),输出字符串error,不必输出单链表
输入样例:
6 11 22 33 44 55 66
3 777
1 888
1
11
0
5
输出样例:
11 22 33 44 55 66
11 22 777 33 44 55 66
888 11 22 777 33 44 55 66
11 22 777 33 44 55 66
error
error
44
#include <iostream>
using namespace std;
#define ok 0
#define error -1

class ListNode
{
public:
    int data;
    ListNode* next;
    ListNode()
    {
        next = NULL;
    }
};

class Linklist
{
public:
    ListNode* head;
    int len;
    Linklist()
    {
        head = new ListNode();
        len = 0;
    }
    ~Linklist()
    {
        ListNode* p, * q;
        p = head;
        while (p != NULL)
        {
            q = p;
            p = p->next;
            delete q;
        }
        len = 0;
        head = NULL;
    }
    ListNode* LL_index(int i)    //从1算起,1的话就是首元结点
    {
        ListNode* p = head->next;
        int j = 0;                
        while (p && j < i - 1)        
        {
            p = p->next;
            j++;
        }
        if (!p || j > i - 1)
            return NULL;        //错误也是返回结点指针
        return p;

    }
    int LL_get(int i)        //获取第i个元素的数据
    {
        ListNode* p = head->next;    //头结点是没有数据的,从首元结点算起,即head->next
        int j = 0;
        while (p && j < i - 1)
        {
            p = p->next;
            j++;
        }
        if (!p || j > i - 1)
            return error;
        return p->data;            //返回数据
    }
    int LL_intsert(int i, int item)    //把数值item插入第i个位置
    {
        ListNode* p = head;
        ListNode *s = new ListNode();//要插入的item存放在这个结点
        int j = 0;
        while (p && j < i - 1)        //找到线性表中第i-1个结点,然后修改其指向后继的指针
        {
            p = p->next;
            j++;
        }
        if (!p || j > i - 1)
            return error;
        s->data = item;
        s->next = p->next;        
        p->next = s;
        len++;
        return ok;
    }
    int LL_del(int i)            //删除第i个结点,不包括头结点,第一个结点从1算起
    {
        ListNode* p = head;                //必须从头结点开始判断,因为删除第1个结点时,头结点还需指向第二个结点
        ListNode* q = new ListNode();    //临时存储要删除的那个结点,然后让那个结点指向后继
        int j = 0;
        while (p->next&& j < i - 1)//找到第i-个结点
        {
            p = p->next;
            j++;
        }
        if (!(p->next) || j > i - 1)
            return error;
        q = p->next;
        p->next = q->next;
        delete q;    
        len--;
        return ok;
        
    }

    void LL_display()
    {
        ListNode* p;
        p = head->next;
        while (p)
        {
            cout << p->data << " ";;
            p = p->next;
        }
        cout << endl;
    }
};



int main()
{
    int n, item, pos,judge;
    cin >> n;
    Linklist L;
    Linklist* p;
    for (int i = 1; i <= n; i++)
    {
        cin >> item;
        L.LL_intsert(i, item);
    }
    L.LL_display();
    for (int i = 0; i < 2; i++)
    {
        cin >> pos >> item;
        judge=L.LL_intsert(pos, item);
        if (judge != -1)    L.LL_display();
        else cout << "error" << endl;
    }
    for (int i = 0; i < 2; i++)
    {
        cin >> pos;
        judge=L.LL_del(pos);
        if (judge!=-1)    L.LL_display();
        else cout << "error" << endl;
    }
    for (int i = 0; i < 2; i++)
    {
        cin >> pos;
        judge=L.LL_get(pos);
        if (judge!=-1)    cout<<judge<<endl;
        else cout << "error" << endl;
    }


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值