c++的简单链表...
时间:2010-08-12 来源:wuguangmin
实现的是一个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;
}