2 u014695538 u014695538 于 2014.07.01 12:05 提问

C++代码,链表,无法解析的字符

#ifndef LIST_H
#define LIST_H

#include"ListNode.h"

template< typename NODETYPE >
class List
{
public:
List();
~List();
void insertAtFirst( const NODETYPE & );
void insertAtLast( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
ListNode< NODETYPE > *firstPtr;
ListNode< NODETYPE > *lastPtr;
ListNode< NODETYPE > *getNewNode( const NODETYPE & );
};

#endif

#ifndef LISTNODE_H
#define LISTNODE_H

template< typename NODETYPE > class List;

template< typename NODETYPE >
class ListNode
{
friend class List< NODETYPE >;

public:
ListNode( const NODETYPE & ) ;
NODETYPE getData( ) const ;
private:
NODETYPE data;
ListNode< NODETYPE > *nextPtr;
};

#endif

#include"List.h"
#include
using namespace std;

template< typename NODETYPE >
List< NODETYPE >::List():firstPtr( NULL ),lastPtr( NULL )
{

}

template< typename NODETYPE >
List< NODETYPE >::~List()
{
if( !isEmpty() )
{
ListNode< NODETYPE > *currentPtr=firstPtr;
ListNode< NODETYPE > *tempPtr=NULL;

    while( currentPtr!=NULL )
    {
        tempPtr=currentPtr;
        cout<<tempPtr->data<<"\t";
        currentPtr=currentPtr->nextPtr;
        delete tempPtr;
    }
}

}

template< typename NODETYPE >
void List< NODETYPE >::insertAtFirst( const NODETYPE &value)
{
ListNode< NODETYPE > *newPtr=getNewNode( value )

if( isEmpty() )
{
    firstPtr=lastPtr=newPtr;
}
else
{
    newPtr->nextPtr=firstPtr;
    firstPtr=newPtr;
}

}

template< typename NODETYPE >
void List< NODETYPE >::insertAtLast( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr=getNewNode( value );

if( isEmpyt() )
{
    firstPtr=lastPtr=newPtr
}
else
{
    newPtr->nextPtr=firstPtr;
    firstPtr=newPtr;
}

}

template< typename NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if( isEmpty() )
{
return false;
}
else
{
ListNode< NODETYPE > *tempPtr=firstPtr;

    if( firstPtr==lastPtr )
    {
        firstPtr=lastPtr=NULL;
        delete tempPtr;
        return true;
    }
    else 
    {
        firstPtr=firstPtr->nextPtr;
        value=tempPtr->data;
        delete tempPtr;
        return true;
    }
}

}

template< typename NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if( isEmpty() )
{
return false;
}
else
{
ListNode< NODETYPE > *tempPtr=lastPtr;

    if( firstPtr==lastPtr )
    {
        firstPtr=lastPtr=NULL;
        delete tempPtr;
        return true;
    }
    else
    {
        ListNode< NODETYPE > *currentPtr=firstPtr;

        while( cuurentPtr!=lastPtr )
        {
            currentPtr=currentPtr->nextPtr;
        }
        lastPtr=currentPtr;
        currentPtr->nextPtr=NULL;
        value=tempPtr->data;
        delete tempPtr;
        return true;
    }
}

}

template< typename NODETYPE >
bool List< NODETYPE >::isEmpty() const
{
return ( firstPtr==NULL );
}

template< typename NODETYPE >
ListNode< NODETYPE > *List< NODETYPE >::getNewNode( const NODETYPE &value )
{
return ( new ListNode< NODETYPE >( value ) );
}

template< typename NODETYPE >
void List< NODETYPE >::print() const
{
if( isEmpty() )
{
cout<<"The list is empty"<<endl;
return;
}

ListNode<NODETYPE> *currentPtr=firstPtr;

cout<<"The list is:"<<endl;

while( currentPtr!=NULL )
{
    cout<<currentPtr->data;
    currentPtr=currentPtr->nextPtr;
}

cout<<"\n\n";

}

#include"ListNode.h"

template< typename NODETYPE >
ListNode < NODETYPE > ::ListNode(const NODETYPE& info ):data( info ), nextPtr( NULL )
{

}

template< typename NODETYPE >
NODETYPE ListNode< NODETYPE > ::getData() const
{
return data;
}

#include
#include
#include"List.h"
using namespace std;

void instructions();

template< typename T >
void testList( List< T > &, const string &);

int main()
{
List< int > integerList;
testList( integerList, "integer" );
}

void instructions()
{
cout<<"Enter one of the followings:\n"
<<"1 to insert at beginning of list\n"
<<"2 to insert at back of list\n"
<<"3 to delete from beginning of list\n"
<<"4 to delete from back of list\n "
<<"5 to print the list\n";
}
template< typename T >
void testList( List< T > listObject, const string &typeName )
{

cout<<"Testing a list of "<< typeName<<" values\n ";
instructions();

int choice;
T value;

do
{
    cout<<"?";
    cin>>choice;

    switch( choice )
    {
    case 1:
        cout<<"Enter"<<typeName<<";";
        cin>>value;
        listObject.insertAtFirst( value );
        listObject.print();
        break;
    case 2:
        cout<<"Enter"<<typeName<<":";
        cin>>value;
        listObject.insertAtLast( value );
        listObject.print();
        break;
    case 3:
        if( listObject.removeFromFront( value ) )
        {
            cout<<value<<"removed from front\n";
        }
        listObject.print();
        break;
    case 4:
        if( listObject.removeFromBack( value ) )
        {
            cout<<value<<"removed from back\n";
        }
        listObject.print();
        break;
    }
}while(choice<5);

cout<<"End";

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!