cpp24.cpp

来自「C++参考书」· C++ 代码 · 共 81 行

CPP
81
字号
	
// Coded by plusir -- Jan.04.2003.
// Standard C++ Bible -- (P327-11-24)

#include <iostream>
using namespace std ;

class ListEntry
{
	public:
		ListEntry( char * ) ;
		~ListEntry( void ) ;
		ListEntry* renNextEntry( void ) const ;
		void display( void ) const ;

		static ListEntry *firstEntry ;

	private:
		static ListEntry *lastEntry ;
		ListEntry* nextEntry ;
		char *listValue ;
} ;

ListEntry::ListEntry( char *s )
{
	if ( firstEntry == NULL )
		firstEntry = this ;

	if ( lastEntry != 0 )
		lastEntry->nextEntry = this ;

	lastEntry = this ;
	listValue = new char[strlen( s ) + 1] ;
	strcpy( listValue, s ) ;
	nextEntry = NULL ;
}

ListEntry::~ListEntry( void )
{
	delete [] listValue ;
}

ListEntry* ListEntry::renNextEntry( void ) const
{
	return nextEntry ;
}

void ListEntry::display( void ) const
{
	cout << listValue <<  endl ;
}

ListEntry* ListEntry::firstEntry = NULL ;
ListEntry* ListEntry::lastEntry = NULL ;

int main()
{
	while ( true ) {

		cout << "\nEnter a name( \"end\" when done ): " ;
		char name[25] ;
		cin >> name ;

		if ( strncmp( name, "end", 3 ) == 0 )
			break ;

		new ListEntry( name ) ;
	}

	ListEntry *next = ListEntry::firstEntry ;

	while ( next != NULL ) {
		next->display() ;
		ListEntry *hold = next ;
		next = next->renNextEntry() ;
		delete hold ;
	}

	return 0 ;
}

⌨️ 快捷键说明

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