| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 507 人关注过本帖
标题:c++链表,求助
只看楼主 加入收藏
afraid
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-6-13
收藏
 问题点数:0 回复次数:1 
c++链表,求助
c++链表,求助
#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
//下面是出错信息
------------------Configuration: 链表 - Win32 Debug--------------------
Compiling...
链表.cpp
C:\Documents and Settings\Administrator\链表.cpp(22) : error C2143: syntax error : missing ';' before '<'
C:\Documents and Settings\Administrator\链表.cpp(22) : error C2501: 'list_item' : missing storage-class or type specifiers
C:\Documents and Settings\Administrator\链表.cpp(22) : error C2059: syntax error : ';'
C:\Documents and Settings\Administrator\链表.cpp(22) : error C2143: syntax error : missing ';' before '<'
C:\Documents and Settings\Administrator\链表.cpp(22) : error C2065: 'elemtype' : undeclared identifier
C:\Documents and Settings\Administrator\链表.cpp(36) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\链表.cpp(36) : error C2447: missing function header (old-style formal list?)
C:\Documents and Settings\Administrator\链表.cpp(39) : error C2954: template definitions cannot nest
C:\Documents and Settings\Administrator\链表.cpp(40) : error C2143: syntax error : missing ';' before '<'
C:\Documents and Settings\Administrator\链表.cpp(40) : error C2734: 'list_item' : const object must be initialized if not extern
C:\Documents and Settings\Administrator\链表.cpp(40) : error C2734: 'list_item' : const object must be initialized if not extern
C:\Documents and Settings\Administrator\链表.cpp(40) : error C2373: 'list_item' : redefinition; different type modifiers
        C:\Documents and Settings\Administrator\链表.cpp(22) : see declaration of 'list_item'
C:\Documents and Settings\Administrator\链表.cpp(40) : error C2059: syntax error : ';'
C:\Documents and Settings\Administrator\链表.cpp(40) : error C2143: syntax error : missing ';' before '<'
C:\Documents and Settings\Administrator\链表.cpp(46) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\Administrator\链表.cpp(46) : error C2447: missing function header (old-style formal list?)
C:\Documents and Settings\Administrator\链表.cpp(49) : error C2954: template definitions cannot nest
Error executing cl.exe.

链表.exe - 17 error(s), 0 warning(s)
//环境:windows sp3
vc++ 6.0 sp6 英文版
搜索更多相关主题的帖子: 链表 
2008-06-13 18:36
nujiahuy
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-9-28
收藏
得分:0 
把你定义的类的名称统一一下吧,否则就是ERROR。比如: list_ITem----->list_item
2008-06-14 10:30
快速回复:c++链表,求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020797 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved