c++的简单链表

实现的是一个int型数据链表

包括插入数据,删除数据,查询数据,链表的链接,链表的反转

#include <iostream>
using namespace std;
class ilist_item
{
public:
    ilist_item(int value,ilist_item *item_to_link_to=0);
    int value(){ return _value; }
    ilist_item* next() { return _next; }
    void next(ilist_item *link) { _next = link; }
    void value(int new_value) { _value = new_value; }
private:
    int _value;
    ilist_item *_next;
};

inline
ilist_item::ilist_item(int value,ilist_item *item):_value(value)
{
    if(!item)
    {
        _next = 0;
    }
    else
    {
        _next = item->_next;
        item->_next = this;
    }
}

class ilist
{
public:
    ilist():_at_front(0),_at_end(0),_size(0) {}
    ilist(const ilist &rhs);

    ilist& operator=(const ilist &rhs);

    int size() { return _size; }

    void bump_up_size() { ++_size; }
    void bump_down_size() {  --_size; }

    void insert_front(int value);
    void insert_end(int value);
    void insert(int value,ilist_item *ptr);
    void insert_all(const ilist &rhs);

    void remove_front();
    void remove_all();
    int remove(int value);

    void concat(const ilist &il);

    void reverse();

    ilist_item* find(int value);

    void display(ostream &os = cout);
private:
    ilist_item *_at_front;
    ilist_item *_at_end;
    int _size;
};

inline void
ilist::insert_front(int value)
{
    ilist_item *ptr = new ilist_item(value);
    if(!_at_front)
    {
        _at_front = _at_end = ptr;
    }
    else
    {
        ptr->next(_at_front);
        _at_front = ptr;
    }
    bump_up_size();
}

inline void
ilist::insert_end(int value)
{
    if(!_at_end)
    {
        _at_front = _at_end = new ilist_item(value);
    }
    else
    {
        _at_end = new ilist_item(value,_at_end);
    }
    bump_up_size();
}

inline void
ilist::insert(int value,ilist_item *ptr)
{
    if(!ptr)
    {
        insert_front(value);
    }
    else
    {
        bump_up_size();
        new ilist_item(value,ptr);
    }
   
}

ilist_item*
ilist::find(int value)
{
    ilist_item *ptr = _at_front;
    while(ptr)
    {
        if(ptr->value() == value)
        {
            break;
        }
        ptr = ptr->next();
    }
    return ptr;
}

void ilist::display(ostream &os)
{
    ilist_item *ptr = _at_front;
    os<<"/n("<<_size<<")(";
    while(ptr)
    {
        os<<ptr->value()<<" ";
        ptr = ptr->next();
    }
    os<<")/n";
}

inline void
ilist::remove_front()
{
    if(_at_front)
    {
        ilist_item *ptr = _at_front;
        _at_front = _at_front->next();
        bump_down_size();
        delete ptr;
    }
}

inline void
ilist::remove_all()
{
    while(_at_front)
    {
        remove_front();
    }
    _size = 0;
    _at_front = _at_end = 0;

}

inline int
ilist::remove(int value)
{
    ilist_item *plist = _at_front;
    int elem_cnt = 0;
    while(plist && plist->value() == value)
    {
        plist = plist->next();
        remove_front();
        ++elem_cnt;
    }

    if(!plist)
    {
        return elem_cnt;
    }

    ilist_item *prev = plist;

    while(plist)
    {
        if(plist->value() == value)
        {
            prev->next(plist->next());
            delete plist;
            ++elem_cnt;
            bump_down_size();
            plist = prev->next();
            if(!plist)
            {
                _at_end = prev;
                return elem_cnt;
            }
        }
        else
        {
            prev = plist;
            plist = plist->next();
             
        }
    }
    return elem_cnt;
   
}

void
ilist::concat(const ilist &il)
{
    ilist_item *ptr = il._at_front;
    while(ptr)
    {
        insert_end(ptr->value());
        ptr = ptr->next();
    }
}

void
ilist::reverse()
{
    ilist_item *ptr = _at_front;
    ilist_item *prev = 0;
   
    _at_front = _at_end;
    _at_end = ptr;

    while(ptr!=_at_front)
    {
        ilist_item *tmp = ptr->next();
        ptr->next(prev);
        prev = ptr;
        ptr = tmp;
    }
    _at_front->next(prev);

}

void
ilist::insert_all(const ilist &rhs)
{
    ilist_item *ptr = rhs._at_front;
    while(ptr)
    {
        insert_end(ptr->value());
        ptr = ptr->next();
    }
}

inline
ilist::ilist(const ilist &rhs):_at_front(0),_at_end(0)
{
    insert_all(rhs);
}

inline ilist&
ilist::operator =(const ilist &rhs)
{
    if(this != &rhs)
    {
        remove_all();
        insert_all(rhs);
    }
    return *this;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值