首页
社区
课程
招聘
[旧帖] [原创]c++实现链表 0.00雪花
发表于: 2009-5-1 15:11 1859
申请推荐此帖 编辑 删除

[旧帖] [原创]c++实现链表 0.00雪花

2009-5-1 15:11
1859
#ifndef LIST_H
#define LIST_H
template<typename elemtype>class list_item
{
public:
list_item( elemtype, list_item<elemtype>* );
list_item( const list_item<elemtype>& );

const elemtype date () const;
const list_item<elemtype>* next() const;
void get_date ( const elemtype );
void get_next ( const list_item<elemtype>* );

void operator =( const list_item<elemtype>& );
private:
elemtype  _date;
list_item<elemtype> *_next;
};//单链表数据项类

//数据项类代码实现
template<typename elemtype>
list_item<elemtype>::list_item( elemtype ia = 0,
       list_item<elemtype> *p = 0 )
{
   get_date( ia );
   if( p == NULL )
    get_next( NULL );
   else
   {
    get_next( p->next() );
    p->get_next( this );
   }
  }
template<typename elemtype>
const elemtype
list_item<elemtype>::date() const
{
  return _date;
}
template<typename elemtype> const
list_item<elemtype>* list_item<elemtype>::
next() const  
{
  return _next;
}
template<typename elemtype>
void list_item<elemtype>::get_date( const elemtype de )
{
  _date = de;
}
template<typename elemtype>
void list_item<elemtype>::
get_next( const list_item<elemtype> *pev )
{
  _next = ( list_item<elemtype>* )pev;
}

template<typename elemtype> class list
{
public:
list();
list( const list<elemtype>& );
~list();

const int size() const;
const bool empty() const;
void insert( const elemtype, const elemtype );
void insert_front( const elemtype );
void insert_end( const elemtype );
void remove( const elemtype );
void remove_all();
void remove_front();
void print() const;
const list_item<elemtype>* find( const elemtype );

void operator =( const list<elemtype>& );

private:
//
void down_size();
void add_size();

//
list_item<elemtype> *at_front;
list_item<elemtype> *at_end;
list_item<elemtype> *at_move;
int               _size;
};//链表类定义

//函数实现代码
//私有函数集合
template<typename elemtype>
void list<elemtype>::add_size()
{
  ++_size;
}
template<typename elemtype>
void list<elemtype>::down_size()
{
  --_size;
}

//公有函数集合
template<typename elemtype>
list<elemtype>::list() {
  at_front = NULL;
  at_end = NULL;
  _size = 0;
}
template<typename elemtype>
list<elemtype>::~list()
{
  remove_all();
}
template<typename elemtype>
const bool list<elemtype>::empty() const
{
  return size() == 0 ? false : true;
}
template<typename elemtype>
const int list<elemtype>::size() const
{
  return _size;
}
template<typename elemtype>
void list<elemtype>::insert_front( const elemtype iva )
{
  list_item<elemtype> *pv =
    new list_item<elemtype>( iva, 0 );
  if( !at_front )
  {
   at_front = at_end = pv;
  }
  else
  {
   pv->get_next( at_front );
   at_front = pv;
  }
  add_size();
}
template<typename elemtype>
void list<elemtype>::insert_end( const elemtype iva )
{
  if( at_end == NULL)
  {
   at_end = at_front =
     new list_item<elemtype>( iva, 0 );
  }
  else
   at_end = new list_item<elemtype>( iva, at_end );
  add_size();
}
template<typename elemtype> void list<elemtype>::
insert( const elemtype ixa, const elemtype iva )
{
  list_item<elemtype> *pev =
    ( list_item<elemtype>* )find( iva );
  if( pev == NULL )
  {
   cerr << "err!" <<endl;
   return;
  }
  if( pev == at_front )
   insert_front( ixa );
  else {
   new list_item<elemtype>( ixa, pev );
   add_size();
  }
}
template<typename elemtype> const
list_item<elemtype>* list<elemtype>::
find( const elemtype iva )
{
  list_item<elemtype> *at_move = at_front;
  while( at_move != NULL )
  {
   if( at_move->date() == iva )
    return at_move;
   at_move = ( list_item<elemtype>* )at_move->next();
  }
   return NULL;
}
template<typename elemtype>
void list<elemtype>::remove_front()
{
  if( at_front )
  {
   list_item<elemtype> *pev = at_front;
   at_front = ( list_item<elemtype>* )at_front->next();
   delete pev;
   down_size();
  }
}
template<typename elemtype>
void list<elemtype>::remove( elemtype iva )
{
  list_item<elemtype> *pev = at_front;
  while( pev && pev->date() == iva )
  {
   pev = ( list_item<elemtype>* )pev->next();
   remove_front();
  }
  if( !pev )
   return ;
  list_item<elemtype> *prv = pev;
  pev = ( list_item<elemtype>* )pev->next();
  while( pev )
  {
   if( pev->date() == iva )
   {
    prv->get_next( pev->next() );   
    down_size();
    delete pev;
    pev = ( list_item<elemtype>* )prv->next();
    if( pev != NULL )
    {
     at_end = prv;
     return;
    }
   }
   else
   {
    prv = pev;
    pev = ( list_item<elemtype>* )pev->next();
   }
  }
}
template<typename elemtype>
void list<elemtype>::remove_all()
{
  while( at_front )
   remove_front();
  _size = 0;
  at_front = at_end = NULL;
}
template<typename elemtype>
void list<elemtype>::print() const
{
  list_item<elemtype> *pev = at_front;
  cout << '[' << size() << ']';
  cout << '{';
  for( int ix = 0; pev && ix < size(); ++ix )
  {
   cout << pev->date() << ' ';
   pev = ( list_item<elemtype>* )pev->next();
  }
  cout << '}' << endl;
}
#endif

email : jccg1000111596@163.com

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (7)
雪    币: 5198
活跃值: (1138)
能力值: (RANK:30 )
在线值:
发帖
回帖
粉丝
2
邀请码己发,请查收
2009-5-6 11:40
0
雪    币: 84
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我新手怎么发原创帖啊····
2009-5-6 13:37
0
雪    币: 44
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
不太懂啊 !!!!!! 新手难~~~~~~
2009-5-6 15:25
0
雪    币: 50
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
不太懂!!!!不懂
2009-5-8 13:23
0
雪    币: 2
活跃值: (26)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
发代码也能申请邀请码?我要试试
2010-10-6 19:01
0
雪    币: 459
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
游客?咋回事?
2010-10-6 19:03
0
雪    币: 3
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
看雪的管理员,你好,我kx不够 发不了邮件和悄悄话 只能这样来回复;

我转载到看雪的文章都是本人精心挑选的 有学习价值的文章;

文章中的水印并非我故意而为,是因为原图片就有水印,本人也没有办法。

http://bbs.pediy.com/showthread.php?p=1439643#post1439643

不知道这样的帖子会不会好一点,麻烦你看一下。
2016-8-3 13:50
0
游客
登录 | 注册 方可回帖
返回
//