本文整理汇总了C++中DList::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ DList::insert方法的具体用法?C++ DList::insert怎么用?C++ DList::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DList
的用法示例。
在下文中一共展示了DList::insert方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: uTest
bool uTest( UnitTest *utest_p)
{
DList<int> list;
list.push_back(5);
list.push_back(10);
Unit *u = list.first();
UTEST_CHECK( utest_p, u);
UTEST_CHECK( utest_p, list.size() == 2);
int& f = u->val();
f = 15;
int sum = 0;
for ( Unit* e = list.first (); e!= 0; e = e->next() )
{
sum += e->val();
}
UTEST_CHECK( utest_p, sum == 25);
u = list.first();
UTEST_CHECK( utest_p, u->next() == list.last() );
UTEST_CHECK( utest_p, u->val() == 15);
Unit * last = list.erase( u);
UTEST_CHECK( utest_p, last->next() == 0);
UTEST_CHECK( utest_p, last->prev() == 0);
UTEST_CHECK( utest_p, last == list.first());
UTEST_CHECK( utest_p, last == list.last());
// Test clear
list.clear();
UTEST_CHECK( utest_p, list.size() == 0);
UTEST_CHECK( utest_p, list.empty() );
UTEST_CHECK( utest_p, list.first() == 0);
UTEST_CHECK( utest_p, list.last() == 0);
//--- Test insert and reverse
list.push_front( 10);// list: 10
list.push_front( 20);// list: 20 10
list.push_front( 40);// list: 40 20 10
u= list.first()->next();
list.insert( u, 30); // list: 40 30 20 10
list.reverse(); // list: 10 20 30 40
UTEST_CHECK( utest_p, list.first()->val() == 10);
UTEST_CHECK( utest_p, list.first()->next()->val() == 20);
UTEST_CHECK( utest_p, list.first()->next()->next()->val() == 30);
UTEST_CHECK( utest_p, list.last()->val() == 40);
return utest_p->result();
}
示例2: first
DList<T>& DList<T>::copy()
{
first();
DList<T> copyList;
Node<T> *tmp = first;
while(tmp)
{
copyList.insert(temp->item);
temp = temp->next;
}
return copyList;
}
示例3: main
int main( int argc, char* argv[] ) {
// verify argument
if ( argc != 2 ) {
cerr << "usage: statistics size" << endl;
return -1;
}
// verify size
int size = atoi( argv[1] );
if ( size < PATTERN_MAX ) {
cerr << "usage: size >= " << PATTERN_MAX << endl;
return -1;
}
// initialize list items
srand( 1 );
int *items = new int[size];
initArray( items, size, -1 );
printArray( items, size, "items" );
// initialize access pattern
int *pattern = new int[PATTERN_MAX];
initArray( pattern, PATTERN_MAX, size );
printArray( pattern, PATTERN_MAX, "pattern" );
// initialize pattern frequency
int *frequency = new int[PATTERN_MAX];
for ( int i = 1; i < PATTERN_MAX; i++ )
frequency[i] = i + frequency[i - 1];
printArray( frequency, PATTERN_MAX, "frequency" );
// generate access sequence
int *sequence = new int[SEQ_MAX];
for ( int i = 0; i < SEQ_MAX; i++ ) {
int random = rand( ) % ( frequency[PATTERN_MAX - 1] + 1 );
int hit;
for ( hit = 0; hit < PATTERN_MAX; hit++ ) {
if ( random <= frequency[hit] ) {
break;
}
}
sequence[i] = items[pattern[hit]];
}
printArray( sequence, SEQ_MAX, "sequence" );
// now conduct performance evaluation
// doubly linked list
DList<int> dlist;
for ( int i = 0; i < size; i++ )
dlist.insert( items[i], i );
for ( int i = 0; i < SEQ_MAX; i++ )
dlist.find( sequence[i] );
cout << "dlist's find cost = " << dlist.getCost( ) << endl;
// mtf list
MtfList<int> mtflist;
for ( int i = 0; i < size; i++ )
mtflist.insert( items[i], i );
for ( int i = 0; i < SEQ_MAX; i++ )
mtflist.find( sequence[i] );
cout << "mtflist's find cost = " << mtflist.getCost( ) << endl;
// transpose list
TransposeList<int> translist;
for ( int i = 0; i < size; i++ )
translist.insert( items[i], i );
for ( int i = 0; i < SEQ_MAX; i++ )
translist.find( sequence[i] );
cout << "translist's find cost = " << translist.getCost( ) << endl;
// skip list
SList<int> skiplist;
for ( int i = 0; i < size; i++ )
skiplist.insert( items[i] );
for ( int i = 0; i < SEQ_MAX; i++ )
skiplist.find( sequence[i] );
cout << "skip's find cost = " << skiplist.getCost( ) << endl;
return 0;
}