纠错题和填空题一般都比较简单,主要是弄清楚什么时候调用的是什么对象(函数)就可以了。程序设计题是难点,一般考察的是字符串的运用或者链表的运用。我把一些比较常用的算法列出来,基本上考的都是这几种算法的组合。当然,理解思路是最重要的,具体操作方式要灵活处理。
(1)获取字符串数组的长度。
for ( int length = 0 ; str[length] ; length ++ ){}
注意这个时候的length是字符串的长度,而字符串的下标是从0开始的,所以最后一个元素应该是str[length-1]。
(2)排序法中交换typeName类型的数组tN的某两个元素。
typeName temp;
temp = tN[i] ;
tN[i] = tN[j] ;
tN[j] = temp;
这个算法常和for语句联合使用。
(3)将某数组中的元素顺序倒置(逆序),len已知。
for ( int j = 0 ; j < len ; j ++ , len --)
{
typeName temp;
temp = tN[j] ;
tN[j] = tN[len] ;
tN[len] = temp;
}
(4)将int类型的数num转化为二进制的字符串str。
if ( num%2 )
str[i]=’1’+NULL;
else
str[i]=’0’+NULL;
除二取余法,还需要将所得结果逆序,详细代码我就不写了。
(5)查找数组中的某个元素key,已知长度len。
for ( int i = 0 ; i < len ; i ++ )
{
if ( s[i] == key )
//找到以后需要进行的操作
}
(6)链表中的操作
orderedNodeType.h
template<class Type>
class orderedLinkedListType : public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const ;//查找固定项目
void insertNode(const Type& newItem) const ;//将项目插在有序列表的适当位置(保证插入后列表仍然有序)
void deleteNode ( const Type& deleteItem) ;//删除一个节点
};
/ranNodeType.h
template<class Type>
struct ranNodeType
{
Type info ;
ranNodeType<Type> *link ;
};
template<class Type>
class linkedListType
{
public:
const linkedListType<Type> & operator = ( const linkedListType<Type>& ); //重定义赋值函数
void initializeList () ; //初始化类
bool isEmpty () const ; //判定列表是否为空
void print (ostream & ) const ; //输出列表
int length () const ; //返回列表长度
void destroyList() ; //销毁列表
Type front () const ; //返回第一个节点的值
Type back () const ; //返回最后一个节点的值
bool search ( const Type& searchItem ) const ;//查找列表中的给定项
void insertFirst ( const Type& newItem ) ; //在开头插入一个新项
void insertLast (const Type& newItem ); //在末尾插入一个新项
void deleteNode (const Type& deleteItem ); //删除一项
linkedListType();//构造函数
linkedListType( const linkedListType <Type>& otherList);//复制构造函数
~linkedListType() ;//析构函数
ranNodeType<Type> *first , *last ;
protected :
int count ;
private:
void copyList(const linkedListType<Type>& otherList);
};
/orderNodeType.cpp
template<class Type>
bool orderedLinkedListType<Type>::search(const Type& searchItem) const //查找固定项目
{
ranNodeType<Type> *temp ;
bool found = false ;
while ( temp->info > searchItem && first !=NULL )
temp = temp->link ;
if ( temp->info == searchItem )
{
found = true ;
}
return found;
}
template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newItem) const//将项目插在有序列表(从大到小)的适当位置(保证插入后列表仍然有序)
{
ranNodeType<Type> *temp , *trailTemp , *newNode ;
temp = first ;
if ( first == NULL)
temp->info = newItem ;
newNode = new ranNodeType<Type> ;
newNode ->info = newItem ;
newNode ->link = NULL ;
while ( temp->info > newItem )
{
trailTemp = temp ;
temp = temp->link ;
}
if ( temp != NULL )
{
newNode -> link = temp ;
trailTemp -> link = newNode ;
}
else
trailTemp -> link = newNode ;
count ++;
}
template<class Type>
void orderedLinkedListType<Type>::deleteNode ( const Type& deleteItem)//删除一个节点
{
if ( !search( deleteItem ) )
cout << "Can not found the deleteItem .";
else if ( !isEmpty() )
{
ranNodeType<Type> *temp ;
while ( temp->info > deleteItem )
temp = temp->link ;
delete temp ;
count -- ;
}
}
//ranNodeType.cpp
template<class Type>
bool linkedListType<Type>::isEmpty () const //判定列表是否为空
{
return ( first == NULL );
}
template<class Type>
linkedListType<Type>::linkedListType()//构造函数
{
first = NULL;
last = NULL ;
count = 0 ;
}
template<class Type>
void linkedListType<Type>::destroyList() //销毁列表
{
ranNodeType<Type> *temp ;
while ( first !=NULL )
{
temp = first ;
first = first->link ;
delete temp ;
}
last = NULL ;
count = 0 ;
}
template<class Type>
void linkedListType<Type>::initializeList () //初始化列表
{
destroyList ();
}
template<class Type>
void linkedListType<Type>::print ( ostream & outdata ) const //输出列表
{
ranNodeType<Type> *temp ;
temp = first ;
while ( temp != NULL )
{
outdata << temp->info << " ";
temp = temp->link ;
}
}
template<class Type>
int linkedListType<Type>::length () const //返回列表长度
{
return count ;
}
template<class Type>
Type linkedListType<Type>::front () const //返回第一个节点的值
{
assert( first != NULL );
return first->link ;
}
template<class Type>
Type linkedListType<Type>::back () const //返回最后一个节点的值
{
assert( last != NULL );
return last->link ;
}
template<class Type>
bool linkedListType<Type>::search ( const Type& searchItem ) const //查找列表中的给定项
{
ranNodeType<Type> * temp;
bool found = false ;
temp = first ;
while ( found != true && temp !=NULL )
{
if ( searchItem == temp ->info )
found = true ;
else
temp = temp-> link ;
}
return found ;
}
template<class Type>
void linkedListType<Type>::insertFirst ( const Type& newItem ) //在开头插入一个新项
{
ranNodeType<Type> *newNode ;
newNode = new ranNodeType<Type> ;
assert ( newNode != NULL );
newNode->info = newItem ;
newNode->link = first ;
first = newNode;
count ++ ;
if ( last == NULL )
last = newNode ;
}
template<class Type>
void linkedListType<Type>::insertLast (const Type& newItem ) //在末尾插入一个新项
{
ranNodeType<Type> *newNode ;
newNode = new ranNodeType<Type> ;
assert ( newNode != NULL );
newNode->info = newItem ;
newNode->link = NULL;
if ( first == NULL )
{
first = newNode ;
last = newNode ;
}
else
{
last->link = newNode ;
last = newNode;
}
count ++ ;
}
template<class Type>
void linkedListType<Type>::deleteNode (const Type& deleteItem ) //删除一项
{
ranNodeType<Type> *temp , *trailTemp;
bool found ;
if ( first == NULL )
cout << "Can not delete an empty list."
<< endl ;
else
{
if ( first->info == deleteItem )
{
temp = first ;
first = first->link ;
count -- ;
if ( first == NULL )
last = NULL;
delete temp ;
}
else
{
found = false ;
temp = first->link ;
trailTemp = first ;
while ( temp != NULL && !found )
{
if ( temp ->info != deleteItem )
{
trailTemp = temp ;
temp = temp ->link ;
}
else
found = true ;
}
if ( found )
{
trailTemp->link = temp ->link ;
if ( last == temp )
{
last = trailTemp ;
}
count--;
delete temp ;
}
else
cout << "Can not found the deleteItem ." << endl ;
}
}
}
template<class Type>
const linkedListType<Type> & linkedListType<Type>::operator =
( const linkedListType<Type>& otherList ) //重定义赋值函数
{
if ( this != &otherList )
{
copyList ( otherList ) ;
}
return this ;
}
template<class Type>
linkedListType<Type>::linkedListType( const linkedListType <Type>& otherList)//复制构造函数
{
first = NULL;
copyList ( otherList );
}
template<class Type>
linkedListType<Type>::~linkedListType() //析构函数
{
destroyList () ;
}
//private:
template<class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& otherList)
{
ranNodeType<Type> *newNode , *temp ;
if ( first != NULL )
destroyList ();
if ( otherList.first ==NULL )
{
first = NULL;
last = NULL ;
count = 0 ;
}
else
{
temp = otherList.first ;
while ( temp !=NULL )
{
insertLast ( temp -> info );
temp = temp->link ;
}
}
}
OK把链表补充完了..