int main() { DList<int> Lista; //insertamos elementos for(int i = 0; i < 10; i++) { Lista.insertFirst(i); } //Utilizamos el iterador cout<<"Valores lista"<<endl; for(DList<int>::Iterator it(Lista);it.hasCurrent();it.next()) { cout<<it.getCurrent()->getData()<<" "; } cout<<endl; cout<<"Valores menor de la lista"<<endl; cout<<searchMin(Lista)<<endl; //simpleSort(Lista); //quickSort(Lista); //mergeSort(Lista); sort(Lista); cout<<"Valores lista ordenada"<<endl; for(DList<int>::Iterator it(Lista);it.hasCurrent();it.next()) { cout<<it.getCurrent()->getData()<<" "; } cout<<endl; DList<int> lista2; //probamos el intercambio entre listas lista2.swap(Lista); //probamos la asignacion estre listas Lista = lista2; if(lista2 == Lista) { cout<<"Listas son iguales"<<endl; } //Probamos los [] sobre la lista //OJO esta operacion hace muy lento el acceso a la misma for(int i = 0;i < Lista.getSize();i++) { cout<<Lista[i]<<" "; } cout<<endl; return 0; }
int main() { DList<int> list; srand((unsigned)time(NULL)); for(int i = 0 ; i < 5 ; ++i) { list.findWithIndex(i); list.insertNext(rand() % 50); } std::cout << list.getSize() << std::endl; std::cout << "Before Sorting" << std::endl; list.printAll(); list.sorting(comp); std::cout << "\nAfter Sorting" << std::endl; list.printAll(); return 0; }
int main(int argc, const char * argv[]) { DList<int> myList; //for(int i = 0; i < 10; ++i) // myList.addFront(i); myList.addBack(50); myList.addBack(100); cout << myList.getFront() << endl; cout << myList.getBack() << endl; while(!myList.isEmpty()) cout << myList.removeFront() << endl; cout << myList.getSize() << endl; return 0; }
void UtilTestCase::testDList() { DList<int> l; CPPUNIT_ASSERT(l.isEmpty()); CPPUNIT_ASSERT(l.getSize() == 0); CPPUNIT_ASSERT(l.getHeader()->getNext() == l.getHeader()); CPPUNIT_ASSERT(l.removeFirst() == NULL); CPPUNIT_ASSERT(l.removeLast() == NULL); DLink<int> n1(1); CPPUNIT_ASSERT(n1.get() == 1); DLink<int> n2(2); CPPUNIT_ASSERT(n2.get() == 2); n2.set(3); CPPUNIT_ASSERT(n2.get() == 3); // 测试addFirst和addLast l.addFirst(&n1); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getList() == &l); CPPUNIT_ASSERT(n1.getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); l.addLast(&n2); CPPUNIT_ASSERT(l.getSize() == 2); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getNext() == &n2); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); CPPUNIT_ASSERT(n2.getList() == &l); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == &n1); // 测试moveToFirst和moveToLast l.moveToFirst(&n2); CPPUNIT_ASSERT(l.getSize() == 2); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getPrev() == &n2); CPPUNIT_ASSERT(n1.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == &n1); l.moveToLast(&n2); CPPUNIT_ASSERT(l.getSize() == 2); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getNext() == &n2); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == &n1); // 测试removeFirst CPPUNIT_ASSERT(l.removeFirst() == &n1); CPPUNIT_ASSERT(n1.getList() == NULL); CPPUNIT_ASSERT(n1.getNext() == NULL); CPPUNIT_ASSERT(n1.getPrev() == NULL); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == l.getHeader()); CPPUNIT_ASSERT(l.removeFirst() == &n2); CPPUNIT_ASSERT(n2.getList() == NULL); CPPUNIT_ASSERT(n2.getNext() == NULL); CPPUNIT_ASSERT(n2.getPrev() == NULL); CPPUNIT_ASSERT(l.isEmpty()); CPPUNIT_ASSERT(l.getSize() == 0); CPPUNIT_ASSERT(l.getHeader()->getNext() == l.getHeader()); // 测试removeLast l.addLast(&n1); n1.addAfter(&n2); CPPUNIT_ASSERT(l.removeLast() == &n2); CPPUNIT_ASSERT(n2.getList() == NULL); CPPUNIT_ASSERT(n2.getNext() == NULL); CPPUNIT_ASSERT(n2.getPrev() == NULL); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n1); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n1); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getNext() == l.getHeader()); CPPUNIT_ASSERT(n1.getPrev() == l.getHeader()); CPPUNIT_ASSERT(l.removeLast() == &n1); CPPUNIT_ASSERT(n1.getList() == NULL); CPPUNIT_ASSERT(n1.getNext() == NULL); CPPUNIT_ASSERT(n1.getPrev() == NULL); CPPUNIT_ASSERT(l.isEmpty()); CPPUNIT_ASSERT(l.getSize() == 0); CPPUNIT_ASSERT(l.getHeader()->getNext() == l.getHeader()); // 测试DLink::unLink l.addLast(&n1); n1.addAfter(&n2); n1.unLink(); n1.unLink(); CPPUNIT_ASSERT(n1.getList() == NULL); CPPUNIT_ASSERT(n1.getNext() == NULL); CPPUNIT_ASSERT(n1.getPrev() == NULL); CPPUNIT_ASSERT(l.getSize() == 1); CPPUNIT_ASSERT(!l.isEmpty()); CPPUNIT_ASSERT(l.getHeader()->getNext() == &n2); CPPUNIT_ASSERT(l.getHeader()->getPrev() == &n2); CPPUNIT_ASSERT(l.getHeader()->getNext()->getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getNext() == l.getHeader()); CPPUNIT_ASSERT(n2.getPrev() == l.getHeader()); }