ex1903.cpp

来自「teach yourself C++ in 21 days 第五版」· C++ 代码 · 共 55 行

CPP
55
字号
template <class Type>
List<Type>::~List()
{
     ListCell *pt = head;
     
     while ( pt )
     {
          ListCell *tmp = pt;
          pt = pt->next;
          delete tmp;
     }
     head = tail = 0;
}

template <class Type>
void List<Type>::insert(Type value)
{
     ListCell *pt = new ListCell( value, head );
     assert (pt != 0);
     
     // this line added to handle tail
     if ( head == 0 ) tail = pt;
     
     head = pt;
     theCount++;
}

template <class Type>
void List<Type>::append( Type value )
{
     ListCell *pt = new ListCell( value );
     if ( head == 0 )
          head = pt;
     else
          tail->next = pt;
     
     tail = pt;
     theCount++;
}

template <class Type>
int List<Type>::is_present( Type value ) const
{
     if ( head == 0 ) return 0;
     if ( head->val == value || tail->val == value )
          return 1;
     
     ListCell *pt = head->next;
     for (; pt != tail; pt = pt->next)
          if ( pt->val == value )
               return 1;
          
     return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?