예제 #1
0
int main(){
	List x;
	OrderedList y;

	x.insert(5);
	x.insert(1);
	x.insert(3);

	cout << x << endl; //should print 5, 1, 3,
	x.replace(2, 6);
	cout << x << endl; //should print 5, 6, 3,
	x.insert(2);
	cout << x << endl; //should print 5, 6, 3, 2,

	y.insert(5);
	y.insert(1);
	y.insert(3);
	cout << y << endl; //should print 1, 3, 5,

	y.replace(2, 6);
	cout << y << endl; //should print 1, 5, 6,
	y.insert(2);
	cout << y << endl; //should print 1, 2, 5, 6,

	return 1;
}
예제 #2
0
int main (int argc, char const *argv[])
{
	OrderedList<int>* ol = new OrderedList<int>();
		
	
	//*******************************************************************
	//*                         Testing At
	//*******************************************************************
	cout<<"testing at:\n";
	ol->insert(new int(45)); cout<<"\tinsert";
	ol->insert(new int(35)); cout<<"\tinsert";
	ol->insert(new int(25)); cout<<"\tinsert";
	ol->insert(new int(60)); cout<<"\tinsert";
	cout<<endl;

	print_list(ol);

	int* c = ol->at(0);
	cout<<"\tindex 0 is: "<<*c<<endl;
	c = ol->at(2);
	cout<<"\tindex 2 is: "<<*c<<endl;
	c = ol->at(ol->size()-1);
	cout<<"\tindex at the end is: "<<*c<<endl;
	
	cout <<"Scan in reverse:\n";
	vector<int*> v = ol->scan_rev();
	for (int i = 0; i < v.size(); ++i) { //olist->scan() returns a vector of pointers
		int* p = v[i];
		cout << *p << endl;        //to ints
	}
	cout<<endl;
	
	
}
예제 #3
0
int main() {
  OrderedList* ol = new OrderedList();

  ol->insert(5);
  ol->print();

  ol->insert(7);
  ol->print();

  ol->insert(6);
  ol->print();

  ol->insert(10);
  ol->print();

  ol->insert(8);
  ol->print();

  return 0;
}
예제 #4
0
void testOrderedList() {
    OrderedList<int> mycontainer;

    cout << "\n\n Begin test function for the List<T> class\n";

    // Testing the insert, isEmpty, length, toString functions
    cout << "Testing size of new empty container: " << mycontainer.length() << endl;
    cout << "List contains: " << mycontainer.toString() << "\n";

    cout << "Testing insert(1), length(), and isEmpty() functions. mycontainer is empty? "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");
    mycontainer.insert(1);
    cout << "List contains: " << mycontainer.toString() << "\n";
    cout << "Size is " << mycontainer.length() << endl;

    cout << "Testing insert(2), length(), and isEmpty() functions. mycontainer is empty? "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");
    mycontainer.insert(2);
    cout << "List contains: " << mycontainer.toString() << "\n";
    cout << "Size is " << mycontainer.length() << endl;

    cout << "Testing insert(4), length(), and isEmpty() functions. mycontainer is empty? "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");
    mycontainer.insert(4);
    cout << "List contains: " << mycontainer.toString() << "\n";
    cout << "Size is " << mycontainer.length() << endl;

    cout << "Testing insert(2), length(), and isEmpty() functions. mycontainer is empty? "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");
    mycontainer.insert(2);
    cout << "List contains: " << mycontainer.toString() << "\n";
    cout << "Size is " << mycontainer.length() << endl;

    cout << "Testing insert(3), length(), and isEmpty() functions. mycontainer is empty? "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");
    mycontainer.insert(3);
    cout << "List contains: " << mycontainer.toString() << "\n";
    cout << "Size is " << mycontainer.length() << endl;

    cout << "Testing insert(7), length(), and isEmpty() functions. mycontainer is empty? "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");
    mycontainer.insert(7);
    cout << "List contains: " << mycontainer.toString() << "\n";
    cout << "Size is " << mycontainer.length() << endl << endl;


    int size = mycontainer.length();

    cout << "Testing extract(int), first(), last(), length() and isEmpty() functions \n"
            << "in a for loop with iterations greater than container size.";

    for (int i = 0; i < size + 1; i++) {
        cout << "\nIteration: " << i + 1 << "\n";
        if (!mycontainer.isEmpty()) {
            cout << "\tmycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n");
            cout << "\tList size before remove is " << mycontainer.length() << endl;
            cout << "\tFirst of container is " << mycontainer.first() << endl;
            cout << "\tLast of container is " << mycontainer.last() << endl;
            cout << "\tExtracting: " << i + 1 << endl << endl;
            mycontainer.extract(i + 1);
            cout << "\tmycontiner has: " << mycontainer.toString() << "\n";
        } else {
            cout << "\tmycontainer is empty.\n";
        }

        cout << "\tList size is now: " << mycontainer.length() << endl;
    }
    cout << "\nFinished for loop\n";

    cout<<"\nClearing container and filling it with int starting with 5 up to 10\n";
    mycontainer.clear();
    for (int i = 1; i < 7; i++)
        mycontainer.insert(i + 4);
    cout<<"mycontainer has: "<<mycontainer.toString()<<"\n";

    cout << "\nTesting removeFirst(),removeLast(), first(), last(), length() and isEmpty() functions \n"
            << "in a for loop with iterations greater than container size.";

    for (int i = 0; i < size + 1; i++) {
        cout << "\nIteration: " << i + 1 << "\n";
        if (!mycontainer.isEmpty()) {
            cout << "\tmycontainer is empty? " << (mycontainer.isEmpty() ? " true\n" : "false\n");
            cout << "\tList size before remove is " << mycontainer.length() << endl;
            cout << "\tFirst of container is " << mycontainer.first() << endl;
            cout << "\tLast of container is " << mycontainer.last() << endl;
            cout << "\tRemoving First: \n";
            mycontainer.removeFirst();
            cout << "\tmycontainer has: " << mycontainer.toString() << "\n";
            cout << "\tRemoving Last: \n";
            mycontainer.removeLast();
            cout << "\tmycontainer has: " << mycontainer.toString() << "\n";
        } else {
            cout << "\tmycontainer is empty.\n";
        }

        cout << "\tList size is now: " << mycontainer.length() << endl;
    }
    cout << "\nFinished for loop\n";

    cout << "\nTesting the reference for first() and last() functions.\n";
    cout << "Start with int test = 7. mycontainer.insert(test)\n";
    int test = 7;
    mycontainer.insert(test);
    cout << "Testing with test = 8. test=mycontainer.front(). mycontainer.front() = 13 \n";
    test = 8;
    test = mycontainer.first();
    mycontainer.first() = 13;
    cout << "Test is now " << test << " front of container is " << mycontainer.first()
            << " back of container is " << mycontainer.last() << endl;
    test = 11;
    mycontainer.insert(test);
    cout << "Back of container is: " << mycontainer.last() << endl;
    cout << "Test is now " << test << " front of container is " << mycontainer.first()
            << " back of container is " << mycontainer.last() << endl;
    mycontainer.last() = test;
    cout << "Test is now " << test << " front of container is " << mycontainer.first()
            << " back of container is " << mycontainer.last() << endl;

    cout << "mycontainer size is " << mycontainer.length() << endl;

    cout << "\nTesting the clear() function: \n";
    mycontainer.clear();
    cout << "mycontainer size now is " << mycontainer.length()
            << " mycontainer is empty: "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");


    cout << "\n\nTesting insertBefore and insertAfter fucntions.\n";
    cout << "\nBegin by inserting ints starting at 13\n";
    for (int i = 0; i < 5; i++) {
        mycontainer.insert(i + 13);
    }

    cout << "\n\nTesting contains(400): " << (mycontainer.contains(400) ? " true\n" : "false\n");
    cout << "\n\nTesting contains(711): " << (mycontainer.contains(711) ? " true\n" : "false\n");

    cout << "\nTesting assignment operator: container2 = mycontainer\n";
    cout << "Filling mycontainer with ints starting at 42\n";
    size = 5;
    // Fill mycontainer with ints to test copy constructor
    for (int i = 0; i < size; i++) {
        mycontainer.insert(i + 42);
    }


    cout << "mycontainer size now is " << mycontainer.length()
            << " mycontainer is empty: "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");
    cout << "mycontainer has: " << mycontainer.toString() << "\n";

    OrderedList<int> container2;
    container2 = mycontainer;
    cout << "mycontainer size is: " << mycontainer.length() << endl;
    cout << "container2 size is: " << container2.length() << endl;
    cout << "Testing the contents of container2 and mycontainer:\n";
    cout << "mycontainer has: " << mycontainer.toString() << "\n";
    cout << "container2 has: " << container2.toString() << "\n";

    cout << "\nTesting the copy constructor.\n";

    cout << "mycontainer size now is " << mycontainer.length()
            << " mycontainer is empty: "
            << (mycontainer.isEmpty() ? " true\n" : "false\n");

    OrderedList<int> container3(mycontainer);

    cout << "container3 size is: " << container3.length();
    cout << "\nTesting the contents of mycontainer and container3:\n";
    cout << "mycontainer has: " << mycontainer.toString() << "\n";
    cout << "container3 has: " << container2.toString() << "\n";

    cout << "\nEnd of test function for the List<T> class\n\n";
}
예제 #5
0
int main(int argc, char** argv) {
    NodeList<int> llista;
    OrderedList<int> llistaOrd;
    cout << "Vols introduir les dades per consola o per fitxer (C/F)?" << endl;
    if(cin.get() == 'F') {
        FILE * pFile;
        pFile = fopen("fitxer.txt" , "r");
        
        int n;
        ifstream fe("fitxer.txt");
        while (scanf("%d", &n) == 1) {
            llista.insert(n);
            llistaOrd.insert(n);
        }
    }
    else {
        cout << "Introdueix una seqüència de nombres acabada per 99:" << endl;
        int aux;
        cin >> aux;
        while(aux != 99) {
            llista.insert(aux);
            llistaOrd.insert(aux);
            cin >> aux;
        }
    }
    cout << "Llista: ";
    llista.show();
    cout << "Llista Ordenada: ";
    llistaOrd.show();
    cout << endl << "Introdueix un nombre a cercar:" << endl;
    cin >> aux;
    cout << "Nombres posteriors a " << aux << " a la llista:" << endl;
    llista.begin();
    bool trobat = false;
    while(!llista.end()) {
        if(trobat) {
            cout << llista.get() << "  " ; 
        }
        else {
            if(llista.get() == aux) trobat = true;
        }
        llista.next();
    }
    cout << endl << "Nombres posteriors a " << aux << " a la llista ordenada:" <<endl;  
    trobat = false;
    llistaOrd.begin();
    while(!llistaOrd.end()) {
        if(trobat) {
            cout << llistaOrd.get() << "  " ; 
        }
        else {
            if(llistaOrd.get() == aux) trobat = true;
        }
        llistaOrd.next();
    }    
    cout << endl << "Llista esborrada i recursos alliberats" << endl;
    llista.begin();
    while(!llista.end()) {
        llista.remove();
    }
    cout << "Llista ordenada esborrada i recursos alliberats:" << endl;
    llistaOrd.begin();
    while(!llistaOrd.end()) {
        
        llistaOrd.remove();
    }
    cout << "Llista:" << endl;
    llista.show();
    cout << "Llista ordenada:" << endl;
    llistaOrd.show();
    return 0;
}