Пример #1
0
void processFile(char* filename) {
    
    //set up the input file stream and attempt to open the file
    ifstream fin;
    fin.open(filename);
    
    //if the file is successfully opened
    if (!fin.fail()) {
        
        //create a Doubly Linked List pointer (set to NULL)
        DLList* list = NULL;
        
        //set up reusable string stream object, int variable, and string nextLine
        stringstream ss;
        int value = 0;
        string nextLine;
        
        //while loop that continues until the end of the file is reached
        while (!fin.eof()){
            
            //get the next line from the file
            getline(fin, nextLine);
            
            //pull the first character from nextLine and make sure it is uppercase
            char firstCharacter = toupper(nextLine[0]);
            
            //if the character is C
            if (firstCharacter == 'C') {
                
                //if there is already a list, delete it first
                if (list != NULL) {
                    delete list;
                    list = NULL;
                }
                
                //create a new list and output success to the console
                list = new DLList;
                cout << "LIST CREATED" << endl;
                
            //if the first character is #
            } else if (firstCharacter == '#') {
                
                //do nothing
                
            } else {
                
                //any operation other than C requires a list instance
                //if there is no list instance
                if (list == NULL) {
                    
                    //output requirement to the console
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                    
                } else {
                    
                    //if there is a list instance perform an operation based on the first character of nextLine
                    switch(firstCharacter) {
                        
                        //if the character is X
                        case 'X':
                        
                            //clear the list and output success to the console
                            list->clear();
                            cout << "LIST CLEARED" << endl;
                        break;
                        
                        //if the character is D
                        case 'D':
                        
                            //delete the list, set the list pointer to NULL, and output success to the console
                            delete list;
                            list = NULL;
                            cout << "LIST DELETED" << endl;
                        break;
                        
                        //if the character is I
                        case 'I':
                        
                            //get the integer value that follows the character
                            ss.str(nextLine.substr(2));
                            ss >> value;
                            
                            //insert a Node into the list with the integer value from the input file as contents (ascending order)
                            list->insert(value);
                            
                            //output success to the console
                            cout << "VALUE " << value << " INSERTED" << endl;
                            
                            //clear the stringstream
                            ss.clear();
                        break;
                        
                        //if the character is F
                        case 'F':
                        
                            //get the integer that follows the character
                            ss.str(nextLine.substr(2));
                            ss >> value;
                            
                            //insert a Node at the head pointer with the value from the input file as contents
                            list->pushFront(value);
                            
                            //output success to the console
                            cout << "VALUE " << value << " ADDED TO HEAD" << endl;
                            
                            //clear the stringstream
                            ss.clear();
                        break;
                        
                        //if the character is B
                        case 'B':
                        
                            //get the integer that follows the character
                            ss.str(nextLine.substr(2));
                            ss >> value;
                            
                            //insert a Node at the tail pointer with the value from the input file as contents
                            list->pushBack(value);
                            
                            //output success to the console
                            cout << "VALUE " << value << " ADDED TO TAIL" << endl;
                            
                            //clear the stringstream
                            ss.clear();
                        break;
                        
                        //if the character is A
                        case 'A':
                        
                            //attempt to output the value of the Node a the head pointer to the console
                            try {
                                
                                //output success to the console
                                cout << "VALUE " << list->getFront() << " AT HEAD" << endl;
                                
                            //if the attempt is unsuccessful (head is NULL)
                            } catch (string err) {
                                
                                //output failure to the console
                                cout << err << endl;
                            }
                        break;
                        
                        //if the character is Z
                        case 'Z':
                        
                            //attempt to output the value of the Node at the tail pointer to the console
                            try {
                                
                                //output success to the console
                                cout << "VALUE " << list->getBack() << " AT TAIL" << endl;
                                
                            //if the attempt is unsuccessful (tail is NULL)    
                            } catch (string err) {
                                
                                //output failure to the console
                                cout << err << endl;
                            }
                        break;
                        
                        //if the character is T
                        case 'T':
                        
                            //if the list is empty
                            if (list->getSize() < 1) {
                                
                                //output failure to the console
                                cout << "LIST EMPTY" << endl;
                                
                            } else {
                                
                                //remove the Node at the head pointer and output success to the console
                                list->popFront();
                                cout << "REMOVED HEAD" << endl;
                            }
                        break;
                        
                        //if the character is K
                        case 'K':
                        
                            //if the list is empty
                            if (list->getSize() < 1) {
                                
                                //output failure to the console
                                cout << "LIST EMPTY" << endl;
                                
                            } else {
                                
                                //remove the Node at the tail pointer and output success to the console
                                list->popBack();
                                cout << "REMOVED TAIL" << endl;
                            }
                        break;
                        
                        //if the character is E
                        case 'E':
                        
                            //get the integer that follows the character
                            ss.str(nextLine.substr(2));
                            ss >> value;
                            
                            //attempt to remove all instances of Nodes with the integer value from the list
                            if (list->removeAll(value) == true) {
                                
                                //output success to the console
                                cout << "VALUE " << value << " ELIMINATED" << endl;
                                
                            } else {
                                
                                //output failure to the console
                                cout << "VALUE " << value << " NOT FOUND" << endl;
                            }
                            
                            //clear the stringstream
                            ss.clear();
                        break;
                        
                        //if the character is R
                        case 'R':
                        
                            //get the integer value that follows the character  
                            ss.str(nextLine.substr(2));
                            ss >> value;
                            
                            //attempt to remove the first instance of a Node with the integer value from the list
                            if (list->removeFirst(value) == true) {
                                
                                //output success to the console
                                cout << "VALUE " << value << " REMOVED" << endl;
                                
                            } else {
                                
                                //output failure to the console
                                cout << "VALUE " << value << " NOT FOUND" << endl;
                            }
                            
                            //clear the stringstream
                            ss.clear();
                        break;
                        
                        //if the character is G
                        case 'G':
                        
                            //get the integer value that follows the character
                            ss.str(nextLine.substr(2));
                            ss >> value;
                            
                            //attempt to locate a Node with the integer value
                            if (list->get(value) == true) {
                                
                                //output success to the console
                                cout << "VALUE " << value << " FOUND" << endl;
                                
                            } else {
                                
                                //output failure to the console
                                cout << "VALUE " << value << " NOT FOUND" << endl;
                            }
                            
                            //clear the stringstream
                            ss.clear();
                        break;
                        
                        //if the character is N
                        case 'N':
                        
                            //output the number of Nodes in the list
                            cout << "LIST SIZE IS " << list->getSize() << endl;
                        break;
                        
                        //if the character is P
                        case 'P':
                        
                            //if the list is empty
                            if (list->getSize() < 1){
                                
                                //output failure to the console
                                cout << "LIST EMPTY" << endl;
                                
                            } else {
                                
                                //output the contents of every node in the list, from head to tail
                                cout << *list << endl;
                            }
                        break;
                    }
                }
            }
        }
        
        //close the file input stream
        fin.close();
        
    } else {
Пример #2
0
void unittest ()
{
	cout << "\n  STARTING UNIT TEST!!!!!\n\n";
	
	DLList list;
	DLList list1;
	DLList list2;

	try {
		evaluate (list.getFront() == 0);
		cout << "Passed TEST 1: getFront '0' pushFront/getFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 1: getFront";
	}
	cout << endl;
	
	try {
		evaluate (list.getSize() == 0);
		cout << "Passed TEST 2: getSize '0' ";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 2: getSize";
	}
	cout << endl;
	
	try {
		evaluate (list.getBack() == 0);
		cout << "Passed TEST 3: getBack '0' ";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 3: getBack";
	}
	
	cout << "\n\n  FINISHED TESTING EMPTY LISTS!!!!!\n\n";
	
	list.pushFront(20);
	list.pushBack(5);
	list1.pushFront(63);
	list1.pushBack(12);
	list2.pushFront(14);
	list2.pushBack(99);	
	list.insert(44);
	list1.insert(87);
	list2.insert(56);
	
	try {
		evaluate (list.getFront() == 20);
		cout << "Passed TEST 4: getFront '20' pushFront/getFront ";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 4: getFront '20' pushFront/getFront";
	}
	cout << endl;
	
	try {
		evaluate (list1.getFront() == 63);
		cout << "Passed TEST 5: getFront '63' pushFront/getFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 5: getFront '63' pushFront/getFront";
	}
	cout << endl;
	
	cout << list2.getFront() << endl;
	try {
		evaluate (list2.getFront() == 14);
		cout << "Passed TEST 6: getFront '14' pushFront/getFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 6: getFront '14' pushFront/getFront";
	}
	cout << endl;
	
	try {
		evaluate (list.getBack() == 5);
		cout << "Passed TEST 7: getBack '5' pushBack/getBack";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 7: getBack '5' pushBack/getBack";
	}
	cout << endl;
	
	try {
		evaluate (list1.getBack() == 12);
		cout << "Passed TEST 8: getBack '12' pushBack/getBack";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 8: getBack '12' pushBack/getBack";
	}
	cout << endl;
	
	try {
		evaluate (list2.getBack() == 99);
		cout << "Passed TEST 9: getBack '99' pushBack/getBack";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 9: getBack '99' pushBack/getBack";
	}
	cout << endl;
	
	cout << "\n  FINISHED TESTING push/get-Back/Front!!!!!\n\n";
	
	try {
		evaluate (list.get(20) == true);
		cout << "Passed TEST 10: get '20' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 10: get '20' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list1.get(63) == true);
		cout << "Passed TEST 11: get '63' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 11: get '63' get(target";
	}
	cout << endl;
	
	try {
		evaluate (list2.get(14) == true);
		cout << "Passed TEST 12: get '14' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 12: get '14' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list.get(5) == true);
		cout << "Passed TEST 13: get '5' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 13: get '5' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list1.get(12) == true);
		cout << "Passed TEST 14: get '12' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 14: get '12' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list2.get(99) == true);
		cout << "Passed TEST 15: get '99' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 15: get '99' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list.get(1) == false);
		cout << "Passed TEST 16: get '1' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 16: get '1' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list1.get(2) == false);
		cout << "Passed TEST 17: get '2' get(target";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 17: get '2' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list2.get(3) == false);
		cout << "Passed TEST 18: get '3' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 18: get '3' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list.get(44) == true);
		cout << "Passed TEST 19: get '44' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 19: get '44' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list1.get(87) == true);
		cout << "Passed TEST 20: get '87' get(target";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 20: get '87' get(target)";
	}
	cout << endl;
	
	try {
		evaluate (list2.get(56) == true);
		cout << "Passed TEST 21: get '56' get(target)";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 21: get '56' get(target)";
	}
	cout << endl;
	
	DLList list3;
	
	list3.pushFront(4);
	list3.pushFront(5);
	list3.pushFront(6);
	list3.pushFront(7);

	list3.popFront();
	try {
		evaluate (list3.getFront() == 6);
		cout << "Passed TEST 22: popFront '6' popBack/popFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 22: popFront '6' popBack/popFront";
	}
	cout << endl;
	
	list3.popFront();
	try {
		evaluate (list3.getFront() == 5);
		cout << "Passed TEST 23: popFront '5' popBack/popFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 23: popFront '5' popBack/popFront";
	}
	cout << endl;
	
	list3.popFront();
	try {
		evaluate (list3.getFront() == 4);
		cout << "Passed TEST 24: popFront '4' popBack/popFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 24: popFront '4' popBack/popFront";
	}
	cout << endl;	
	
	list3.pushBack(9);
	list3.pushBack(10);
	list3.pushBack(11);		
	list3.pushBack(12);
	
	list3.popBack();
	try {
		evaluate (list3.getBack() == 11);
		cout << "Passed TEST 25: popBack '11' popBack/popFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 25: popBack '11' popBack/popFront";
	}
	cout << endl;

	list3.popBack();
	try {
		evaluate (list3.getBack() == 10);
		cout << "Passed TEST 26: popBack '10' popBack/popFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 26: popBack '10' popBack/popFront";
	}
	cout << endl;

	list3.popBack();
	try {
		evaluate (list3.getBack() == 9);
		cout << "Passed TEST 27: popBack '9' popBack/popFront";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 27: popBack '9' popBack/popFront";
	}
	cout << endl;
	
	cout << "\n  FINISHED TESTING get(target)-popBack/popFront!!!!!\n\n";

	DLList list4;	
	
	list4.pushFront(7);
	list4.pushFront(8);
	list4.pushFront(9);
	list4.pushFront(10);
	list4.pushFront(11);
	list4.pushFront(12);
	list4.pushBack(3);
	
	try {
		evaluate (list4.removeFirst(9) == true);
		cout << "Passed TEST 28: removeFirst '9' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 28: removeFirst '9' removeFirst/removeAll";
	}
	cout << endl;
	
	try {
		evaluate (list4.removeFirst(7) == true);
		cout << "Passed TEST 29: removeFirst '7' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 28: removeFirst '7' removeFirst/removeAll";
	}
	cout << endl;
	
	try {
		evaluate (list4.removeFirst(15) == false);
		cout << "Passed TEST 30: removeFirst '15' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 30: removeFirst '15' removeFirst/removeAll";
	}
	cout << endl;
	
	try {
		evaluate (list4.removeFirst(9) == false);
		cout << "Passed TEST 31: removeFirst '9' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 31: removeFirst '9' removeFirst/removeAll";
	}
	cout << endl;
	
	try {
		evaluate (list4.removeFirst(3) == true);
		cout << "Passed TEST 32: removeFirst '3' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 32: removeFirst '3' removeFirst/removeAll";
	}
	cout << endl;
	
	DLList list5;	
	list5.pushFront(4);	list5.pushFront(3);	list5.pushFront(7);	list5.pushFront(6);	list5.pushFront(8);	list5.pushFront(7);
	list5.pushFront(4);	list5.pushFront(3);	list5.pushFront(7);	list5.pushFront(6);	list5.pushFront(8);	list5.pushFront(7);
	list5.pushFront(4);	list5.pushFront(3);	list5.pushFront(7);	list5.pushFront(6);	list5.pushFront(8);	list5.pushFront(7);
	list5.pushFront(4);	list5.pushFront(3);	list5.pushFront(7);	list5.pushFront(6);	list5.pushFront(8);	list5.pushFront(7);
	list5.pushFront(4);	list5.pushFront(3);	list5.pushFront(7);	list5.pushFront(6);	list5.pushFront(8);	list5.pushFront(7);
	list5.pushFront(4);	list5.pushFront(3);	list5.pushFront(7);	list5.pushFront(6);	list5.pushFront(8);	list5.pushFront(7);
	list5.pushBack(4); list5.pushBack(3); list5.pushBack(7); list5.pushBack(6); list5.pushBack(8); list5.pushBack(7);
	list5.pushBack(4); list5.pushBack(3); list5.pushBack(7); list5.pushBack(6); list5.pushBack(8); list5.pushBack(7);
	list5.pushBack(4); list5.pushBack(3); list5.pushBack(7); list5.pushBack(6); list5.pushBack(8); list5.pushBack(7);

	try {
		evaluate (list5.removeAll(3) == true);
		cout << "Passed TEST 33: removeAll '3' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 33: removeAll '3' removeFirst/removeAll";
	}
	cout << endl;
	
	try {
		evaluate (list5.removeAll(7) == true);
		cout << "Passed TEST 34: removeAll '7' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 34: removeAll '7' removeFirst/removeAll";
	}
	cout << endl;

	try {
		evaluate (list5.removeAll(4) == true);
		cout << "Passed TEST 35: removeAll '4' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 35: removeAll '4' removeFirst/removeAll";
	}
	cout << endl;
		
	try {
		evaluate (list5.removeAll(6) == true);
		cout << "Passed TEST 36: removeAll '6' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 36: removeAll '6' removeFirst/removeAll";
	}
	cout << endl;
			
	try {
		evaluate (list5.removeAll(8) == true);
		cout << "Passed TEST 37: removeAll '8' removeFirst/removeAll";
	} catch (MyException e) {
		cout << "#" << e.message << " TEST 37: removeAll '8' removeFirst/removeAll";
	}
	cout << endl;
	
}
Пример #3
0
int main(int argc, char *argv[])
{
    if(argc <= 1){
        cout << "Error: No command-line argument." << endl;
    }
    else{
        DLList<int> *intList = NULL;
	
    	ifstream fileIn(argv[1]);
    	
    	if(!fileIn.good()){
    	    cout << "Error: File could not be opened." << endl;
    	}
    	else{
    	    string line;
            while(getline(fileIn, line)){
                char operation = line[0];
                
                //cout << operation << " : ";
                
                if(operation == '#'){
                    //cout << "COMMENT" << endl;
                }
                else if(operation == 'C'){
                    cout << "LIST CREATED" << endl;
                    if(intList != NULL){
                        delete intList;
                    }
                    intList = new DLList<int>;
                }
                else if(intList == NULL){
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                }
                else{
                    if(operation == 'X'){
                        cout << "LIST CLEARED" << endl;
                        intList->clear();
                    }
                    else if(operation == 'D'){
                        cout << "LIST DELETED" << endl;
                        delete intList;
                        intList = NULL;
                    }
                    else if(operation == 'A'){
                        try{
                            cout << "VALUE " << intList->getFront() << " AT HEAD" << endl;
                        }
                        catch (string e){
                            cout << e << endl;
                        }
                    }
                    else if(operation == 'Z'){
                        try{
                            cout << "VALUE " << intList->getBack() << " AT TAIL" << endl;
                        }
                        catch (string e){
                            cout << e << endl;
                        }
                    }
                    else if(operation == 'T'){
                        if(intList->getSize() > 0){
                            intList->popFront();
                            cout << "REMOVED HEAD" << endl;
                        }
                        else{
                            cout << "LIST EMPTY" << endl;
                        }
                    }
                    else if(operation == 'K'){
                        if(intList->getSize() > 0){
                            intList->popBack();
                            cout << "REMOVED TAIL" << endl;
                        }
                        else{
                            cout << "LIST EMPTY" << endl;
                        }
                    }
                    else if(operation == 'N'){
                        cout << "LIST SIZE IS " << intList->getSize() << endl;
                    }
                    else if(operation == 'P'){
                        if(intList->getSize() > 0){
                            cout << *intList << endl;
                        }
                        else{
                            cout << "LIST EMPTY" << endl;
                        }
                    }
                    else{
                        string str = line.substr(2);
                        stringstream ss(str);
                        int value = 0;
                        ss >> value;
                        
                        if(operation == 'I'){
                            cout << "VALUE " << value << " INSERTED" << endl;
                            intList->insert(value);
                        }
                        else if(operation == 'F'){
                            cout << "VALUE " << value << " ADDED TO HEAD" << endl;
                            intList->pushFront(value);
                        }
                        else if(operation == 'B'){
                            cout << "VALUE " << value << " ADDED TO TAIL" << endl;
                            intList->pushBack(value);
                        }
                        else if(operation == 'E'){
                            if(intList->removeAll(value)){
                                cout << "VALUE " << value << " ELIMINATED" << endl;
                            }
                            else{
                                cout << "VALUE " << value << " NOT FOUND" << endl;
                            }
                        }
                        else if(operation == 'R'){
                            if(intList->removeFirst(value)){
                                cout << "VALUE " << value << " REMOVED" << endl;
                            }
                            else{
                                cout << "VALUE " << value << " NOT FOUND" << endl;
                            }
                        }
                        else if(operation == 'G'){
                            if(intList->get(value)){
                                cout << "VALUE " << value << " FOUND" << endl;
                            }
                            else{
                                cout << "VALUE " << value << " NOT FOUND" << endl;
                            }
                            
                        }
                    }
                }
            }
    	}
    	if(intList != NULL){
    	    delete intList;
    	    intList = NULL;
    	}
    	
    	fileIn.close();
    }
    
	return 0;
}
Пример #4
0
void processFile(string filename) {

    ifstream inputstream;
    DLList *list = NULL;
    inputstream.open(filename.c_str());

    if (inputstream.fail()) {
        cout << "Unable to open " << filename << " for processing." << endl;
    } else {
        string nextline;
        while (! inputstream.eof()) {

            stringstream ss;
            int value = 0;
            getline(inputstream, nextline);
            char firstCharacter = nextline[0];

            switch (toupper(firstCharacter)) {
            case 'C':
                if (list != NULL) {
                    delete list;
                }
                list = new DLList;
                cout << "LIST CREATED" << endl;
                break;
            case 'T':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else if (list->getSize() > 0) {
                    list->popFront();
                    cout << "REMOVED HEAD" << endl;
                } else {
                    cout << "LIST EMPTY" << endl;
                }
                break;
            case 'I':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    ss.str(nextline.substr(2));
                    ss >> value;
                    list->insert(value);
                    cout << "VALUE " << value << " INSERTED" << endl;
                }
                break;
            case 'P':
                if (list ==  NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else if (list->getSize() > 0) {
                    cout << (*list) << endl;
                } else {
                    cout << "LIST EMPTY" << endl;
                }
                break;
            case 'X':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    list->clear();
                    cout << "LIST CLEARED" << endl;
                }
                break;
            case 'D':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    delete list;
                    list = NULL;
                    cout << "LIST DELETED" << endl;
                }
                break;
            case 'F':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    ss.str(nextline.substr(2));
                    ss >> value;
                    list->pushFront(value);
                    cout << "VALUE " << value << " ADDED TO HEAD" << endl;
                }
                break;
            case 'B':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    ss.str(nextline.substr(2));
                    ss >> value;
                    list->pushBack(value);
                    cout << "VALUE " << value << " ADDED TO TAIL" << endl;
                }
                break;
            case 'A':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    try {
                        cout << "VALUE " << list->getFront() << " AT HEAD" << endl;
                    } catch (string e) {
                        cout << e << endl;
                    }
                }
                break;
            case 'Z':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    try {
                        cout << "VALUE " << list->getBack() << " AT TAIL" << endl;
                    } catch (string e) {
                        cout << e << endl;
                    }
                }
                break;
            case 'K':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else if (list->getSize() > 0) {
                    list->popBack();
                    cout << "REMOVED TAIL" << endl;
                } else {
                    cout << "LIST EMPTY" << endl;
                }
                break;
            case 'E':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    ss.str(nextline.substr(2));
                    ss >> value;
                    if (list->removeAll(value) == true) {
                        cout << "VALUE " << value << " ELIMINATED" << endl;
                    } else {
                        cout << "VALUE " << value << " NOT FOUND" << endl;
                    }
                }
                break;
            case 'R':
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    ss.str(nextline.substr(2));
                    ss >> value;
                    if (list->removeFirst(value) == true) {
                        cout << "VALUE " << value << " REMOVED" << endl;
                    } else {
                        cout << "VALUE " << value << " NOT FOUND" << endl;
                    }
                }
                break;
            case 'G':
                ss.str(nextline.substr(2));
                ss >> value;
                if (list == NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else if (list->get(value) == true) {
                    cout << "VALUE " << value << " FOUND" << endl;
                } else {
                    cout << "VALUE " << value << " NOT FOUND" << endl;
                }
                break;
            case 'N':
                if (list ==  NULL) {
                    cout << "MUST CREATE LIST INSTANCE" << endl;
                } else {
                    cout << "LIST SIZE IS " << list->getSize() << endl;
                }
                break;
            default:
                break;

            }
        }
        inputstream.close();
    }
}
Пример #5
0
void run_project3(string filename){

	ifstream myfile(filename.c_str());
    
	if (myfile.is_open()){
		string line;
		string charLetter;
		bool doesListExist = false;
		
		DLList* list;
        
		while (getline(myfile,line)){
			
			charLetter = line.at(0);
			line.erase(0,1);
			int newNumber = 0;
			stringstream converter(line);
			converter >> newNumber;
			
			//creating a list.
			if(charLetter == "C"){
				if(doesListExist == false){
					list = new DLList;
				}
				else if (doesListExist == true){
					delete list;
					list = new DLList;
				}
					
				cout << "LIST CREATED" << endl;
				doesListExist = true;
			}
	
	        //clear the list if there is a list.
			if(charLetter == "X" && doesListExist == true){
				list->clear();
				cout << "LIST CLEARED" << endl;
			}
			
			//delete the list if there is a list.
			if(charLetter == "D" && doesListExist == true){
				delete list;
				list = NULL;
				cout << "LIST DELETED" << endl;
				doesListExist = false;
			}
			
			//insert a newNumber into the list if there is list.
			if(charLetter == "I" && doesListExist == true){
				list->insert(newNumber);
				cout << "VALUE " << newNumber << " INSERTED" << endl;
			}
			
			//add a newNumber into the front of the list if there is list.
			if(charLetter == "F" && doesListExist == true){
				list->pushFront(newNumber);
				cout << "VALUE " << newNumber << " ADDED TO HEAD" << endl;
			}
			
			//insert a newNumber into the back of the list if there is list.
			if(charLetter == "B" && doesListExist == true){
				list->pushBack(newNumber);
				cout << "VALUE " << newNumber << " ADDED TO TAIL" << endl;
			}
			
			//get the first element in the list if there is list.
			if(charLetter == "A" && doesListExist == true){
				try{
					btassert<bool>(list->getSize() != 0);
					cout << "VALUE " << list->getFront() << " AT HEAD" << endl;
				}
				catch (bool b){
					cout << "LIST EMPTY" << endl;
				}
			}
			
			//get the last element in the list if there is list.
			if(charLetter == "Z" && doesListExist == true){
				try{
					btassert<bool>(list->getSize() != 0);
					cout << "VALUE " << list->getBack() << " AT TAIL" << endl;
				}
				catch (bool b){
					cout << "LIST EMPTY" << endl;
				}
			}
	
	        //remove element from front of the list if there is a list.
			if(charLetter == "T" && doesListExist == true){
				if(list->getSize() == 0){
					cout << "LIST EMPTY" << endl;
				}
				else{
					list->popFront();
					cout << "REMOVED HEAD" << endl;
				}	
			}
			
			//remove element from back of the list if there is a list.
			if(charLetter == "K" && doesListExist == true){
				if(list->getSize() == 0){
					cout << "LIST EMPTY" << endl;
				}
				else{
					list->popBack();
					cout << "REMOVED TAIL" << endl;
				}
			}
			
			//get an element from the list if there is a list.
			if(charLetter == "G" && doesListExist == true){
				if(list->get(newNumber) == true){
					cout << "VALUE " << newNumber << " FOUND" << endl;
				}
				else{
					cout << "VALUE " << newNumber << " NOT FOUND" << endl;
				}
			}
			
			//eliminate all the same element, the element to be eliminate will be newNumber.
			if(charLetter == "E" && doesListExist == true){
				if(list->removeAll(newNumber) == true){
					cout <<"VALUE " << newNumber << " ELIMINATED" << endl;
				}
				else{
					cout << "VALUE " << newNumber << " NOT FOUND" << endl;
				}
			}

            //remove an element from the list, the element to be remove will be newNumber.
			if(charLetter == "R" && doesListExist == true){
				if(list->removeFirst(newNumber) == true){
					cout << "VALUE " << newNumber << " REMOVED" << endl;
				}
				else{
					cout << "VALUE " << newNumber << " NOT FOUND" << endl;
				}
			}

            //show number of element in the list.
			if(charLetter == "N" && doesListExist == true){
						cout << "LIST SIZE IS " << list->getSize() << endl;
			}

            //print the element(s) in the list.
			if(charLetter == "P" && doesListExist == true){
				if(list->getSize() == 0){
					cout << "LIST EMPTY" << endl;
				}
				else{
					cout << *list << endl;
				}
			}
			
			//display message if there is no list for an operation.
			if(doesListExist == false && charLetter != "#" && line != ""){
				cout << "MUST CREATE LIST INSTANCE" << endl;
			}
		}
		myfile.close();
		
		//delete the list if there is a list before exiting. NULL list pointer.
		if(doesListExist == true){
			delete list;
			list = NULL;
		}
	}
Пример #6
0
int main(int argc, char* argv[]) {

  DLList* list;
  stringstream ss;
  string strValue;
  int intValue;

  if (argc != 2) {
    cout << "USAGE: " << argv[0] << " FILENAME" << endl;

  } else {
    string currentLine;
    char input;
    cout << "PROCESSING FILE: " << argv[1] << endl;

    ifstream theFile(argv[1]);
    if (theFile.good()) {

      while (getline(theFile, currentLine)) {
        ss.clear();
        intValue = 0;
        strValue = "";
        bool activeList = false;

        input = currentLine.at(0);

          switch (input) {
            
            case 'C':
              if (activeList == false)
              list = new DLList;
              else if (activeList == true) {
              delete list; 
              list = new DLList; 
              }
              activeList = true;
              cout << "LIST CREATED" << endl;
              break;
              
            case 'X':
              list->clear();
              cout << "LIST CLEARED" << endl;
              break;

            case 'D':
              delete list;
              list = NULL;
              cout << "LIST DELETED" << endl;
              break;

             case 'I':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              cout << "VALUE " << intValue << " INSERTED" << endl;
              list->insert(intValue);
              break;

            case 'F':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              cout << "VALUE " << intValue << " ADDED TO HEAD" << endl;
              list->pushFront(intValue);
              break;

            case 'B':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              cout << "VALUE " << intValue << " ADDED TO TAIL" << endl;
              list->pushBack(intValue);
              break;

            case 'A':
              cout << "VALUE " << list->getFront() << " AT HEAD" << endl;
              break;

            case 'Z':
              cout << "VALUE " << list->getBack() << " AT TAIL" << endl;
              break;

            case 'T':
              list->popFront();
              cout << "REMOVED HEAD" << endl;
              break;

            case 'K':
              list->popBack();
              cout << "REMOVED TAIL" << endl;
              break;

            case 'E':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              if (list->removeAll(intValue))
                cout << "VALUE " << intValue << " ELIMINATED" << endl;
              else
                cout << "VALUE " << intValue << " NOT FOUND" << endl;
              break;

              case 'G':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              if (list->get(intValue))
                cout << "VALUE " << intValue << " FOUND" << endl;
              else
                cout << "VALUE " << intValue << " NOT FOUND" << endl;
              break;

             case 'P':
              cout << list->toString() << endl;
              break;

            case 'N':
              cout << "LIST SIZE IS " << list->getSize() << endl;
              break;

            case 'R':
              strValue = currentLine.substr(2);
              ss << strValue;
              ss >> intValue;
              if (list->removeFirst(intValue))
                cout << "VALUE " << intValue << " REMOVED" << endl;
              else
                cout << "VALUE " << intValue << " NOT FOUND" << endl;
              break;

            default:
              cout << "UNKNOWN COMMAND" << endl;
              break;

          }
        }
      }
     theFile.close();

  }

  return 0;
}
int main(int argc, char** argv)
{
	assert(true);

	// {{{ DLNode, int
	{
		//DLNode<int> n1;  // should fail, private
		DLNode<int> n1(10);

		assert(10 == n1.getData());
		assert(11 != n1.getData());
	}
	// }}}

	// {{{ BoolThing
	{
		BoolThing bl1;

		bl1.print();

		BoolThing bl2(false);
		BoolThing bl3(true);
		BoolThing bl4 = bl3;

		bl1.setValue(true);

		assert(bl1 != bl2);

		assert(bl1.getId() != bl2.getId());

		assert(bl1 == bl3);

		assert(bl1.getId() != bl3.getId());
		assert(bl4.getId() == bl3.getId());

		bl1 = bl2;  // assignment operator
		assert(bl1.getId() != bl2.getId());
		assert(bl1 == bl2);

		assert(bl2 < bl3);

		//cout << bl1 << endl;
		//cout << bl2 << endl;
		//cout << bl3 << endl;
	}
	// }}}

	// {{{ IntegerThing
	{
		IntegerThing t1(10);
		IntegerThing t2(20);

		assert(10 == t1.getValue());
		assert(20 == t2.getValue());

		t1.setValue(13);
		assert(13 == t1.getValue());

		assert(!(t1 == t2));
		assert((t1 < t2));
	}
	// }}}

	// {{{ CharThing
	{
		CharThing t1('A');
		CharThing t2('B');

		assert('A' == t1.getValue());
		assert('B' == t2.getValue());

		t1.setValue('C');
		assert('C' == t1.getValue());
		assert('A' != t1.getValue());

		assert(!(t1 == t2));
	}
	// }}}

	// {{{ StringThing
	{
		StringThing t1("abc");
		StringThing t2("DeF");

		assert("abc" == t1.getValue());
		assert("DeF" == t2.getValue());

		t1.setValue("ghi");
		assert("ghi" == t1.getValue());
		assert("abc" != t1.getValue());

		assert(!(t1 == t2));
	}
	// }}}

	// {{{ DoubleThing
	{
		DoubleThing t1(1.124);
		DoubleThing t2(-10.5);

		assert(1.124 == t1.getValue());
		assert(-10.5 == t2.getValue());

		t1.setValue(3.14159);
		assert(3.14159 == t1.getValue());

		assert(!(t1 == t2));
		assert((t1 > t2));
	}
	// }}}

	// {{{ DLNode, IntegerThing
	{
		DLNode<IntegerThing> a(1);
		DLNode<IntegerThing> b(3);

		assert(1 == a.getData().getValue());
		assert(3 == b.getData().getValue());

		assert(a.getData() < b.getData());
		assert(b.getData() > a.getData());
		assert(b.getData() >= a.getData());
		assert(a.getData() <= b.getData());
		assert(a.getData() != b.getData());
		assert(! (a.getData() >= b.getData()));
		assert(! (a.getData() > b.getData()));
	}
	// }}}

	// {{{ DLNode, BoolThing
	{
		DLNode<BoolThing> x(true);

		assert(true == x.getData().getValue());

		x.setData(false);

		assert(! x.getData().getValue());
	}
	// }}}

	// {{{ DLList, push pop
	{
		DLList<int> dl1;

		assert(0 == dl1.getNodeCount());

		for (int i = 0; i < 10; i++) {
			dl1.pushFront(i);
			dl1.pushBack(i+20);
		}
		assert(20 == dl1.getNodeCount());

		for (int i = 0; i < 10; i++) {
			dl1.popBack();
			dl1.popFront();
		}
		assert(0 == dl1.getNodeCount());

		// Nothing should happen when popping an empty list.
		for (int i = 0; i < 10; i++) {
			dl1.popBack();
			dl1.popFront();
		}
		assert(0 == dl1.getNodeCount());

	}
	// }}}

	// {{{ DLList, clear
	{
		DLList<int> dl1;

		assert(0 == dl1.getNodeCount());

		for (int i = 0; i < 10; i++) {
			dl1.pushFront(i);
			dl1.pushBack(i+20);
		}
		assert(20 == dl1.getNodeCount());

		//dl1.clearRecursive();
		dl1.clear();
		assert(0 == dl1.getNodeCount());
	}
	// }}}

	// {{{ DLList, clearRecursive
	{
		DLList<int> dl1;

		assert(0 == dl1.getNodeCount());

		for (int i = 0; i < 100; i++) {
			dl1.pushFront(i);
		}
		assert(100 == dl1.getNodeCount());

		dl1.clearRecursive();
		assert(0 == dl1.getNodeCount());

		for (int j = 0; j < 20; j++) {
			for (int i = 0; i < 10; i++) {
				dl1.pushFront(i);
			}
			dl1.popBack();
			dl1.popFront();
		}
		assert(0 < dl1.getNodeCount());
		dl1.clearRecursive();
		assert(0 == dl1.getNodeCount());
	}
	// }}}

	// {{{ DLList, find
	{
		DLList<int> dl1;

		for (int i = 0; i < 100; i++) {
			dl1.pushFront(i);
		}

		assert(dl1.find(99));
		assert(dl1.find(50));

		for (int i = 0; i < 5; i++) {
			dl1.popBack();
			dl1.popFront();
		}

		assert(dl1.find(65));
		assert(dl1.find(52));

		assert(!dl1.find(100));
		assert(!dl1.find(99));
		assert(!dl1.find(0));
		assert(!dl1.find(1));

	}
	// }}}

	// {{{ DLList, findRecursive
	{
		DLList<int> dl1;

		for (int i = 0; i < 100; i++) {
			dl1.pushFront(i);
		}

		assert(dl1.findRecursive(99));
		assert(dl1.findRecursive(50));

		for (int i = 0; i < 5; i++) {
			dl1.popBack();
			dl1.popFront();
		}

		assert(dl1.findRecursive(65));
		assert(dl1.findRecursive(52));

		assert(!dl1.findRecursive(100));
		assert(!dl1.findRecursive(99));
		assert(!dl1.findRecursive(0));
		assert(!dl1.findRecursive(1));

	}
	// }}}

	// {{{ DLList, insert [DISABLED]
	{
		DLList<int> dl1;

		for (int i = 99; i >= 0; i--) {
			dl1.insert(i);
		}

		assert(dl1.find(99));
		assert(dl1.find(50));
	}
	// }}}

	// {{{ DLList, remove()
	{
	DLList<int> dl1;

	for (int i = 0; i < 10; i++) {
		dl1.insert(i);
	}

	dl1.remove(0, true); // one_or_all=true

	dl1.remove(1);
	dl1.remove(2);

	assert(!dl1.find(0));
	assert(!dl1.find(1));
	assert(!dl1.find(2));
	assert(dl1.find(3));
	assert(dl1.find(4));
	}
	// }}}

	// {{{ DLList, remove()
	{
	DLList<int> dl1;

	// 0 1 2
	for (int i = 0; i < 3; i++) {
		dl1.insert(i);
	}

	assert(dl1.find(0));
	assert(dl1.find(1));
	assert(dl1.find(2));

	dl1.remove(2);
	assert(dl1.find(0));
	assert(dl1.find(1));
	assert(!dl1.find(2));

	dl1.remove(0);
	assert(!dl1.find(0));
	assert(dl1.find(1));
	assert(!dl1.find(2));
	}
	// }}}

	// {{{ DLList, remove() with duplicates
	{
	DLList<int> dl1;

	for (int i = 0; i < 5; i++) {
		dl1.insert(i);
		dl1.insert(i);
	}

	dl1.remove(0, false); // all_or_one=false
	dl1.remove(1, false);
	dl1.remove(2, false);

	assert(dl1.find(0));
	assert(dl1.find(1));
	assert(dl1.find(2));
	assert(dl1.find(3));
	assert(dl1.find(4));
	}
	// }}}

	// {{{ DLList, remove() with duplicates, all_or_one=true
	{
	DLList<int> dl1;

	for (int i = 0; i < 5; i++) {
		dl1.insert(i);
		dl1.insert(i);
	}

	dl1.remove(0, true); // all_or_one=true
	dl1.remove(1, true);
	dl1.remove(2, true);

	assert(!dl1.find(1));
	assert(!dl1.find(2));
	assert(!dl1.find(0));
	assert(dl1.find(3));
	assert(dl1.find(4));
	}
	// }}}

	// {{{ DLList, process
	{
		DLList<int> dl1;

		total = 0;

		dl1.insert(1);
		dl1.insert(2);
		dl1.insert(3);

		dl1.process(_sum);

		assert(6 == total);
	}
	// }}}

	// {{{ DLList, BoolThing
	{
		DLList<BoolThing> dl;
		assert(0 == dl.getNodeCount());

		dl.pushFront(true);

		assert(1 == dl.getNodeCount());
	}
	// }}}

	// {{{ DLList, IntegerThing comparison
	{
	DLNode<IntegerThing> a(1);
	DLNode<IntegerThing> b(3);

	assert(0 <= (a.getData()).getValue());
	assert(2 >= (a.getData()).getValue());

	//assert(1 == dl.getNodeCount());
	}
	// }}}

	// {{{ memory leak test [DISABLED]
	// 4/19/2011 tested OK
	/*
	{
		DLList<int> dl1;

		while (1) {
			DLList<int> dl2;

			assert(0 == dl1.getNodeCount());
			assert(0 == dl2.getNodeCount());

			for (int i = 0; i < 100; i++) {
				dl1.pushFront(i);
				dl1.pushBack(i);
			}

			for (int i = 0; i < 100; i++) {
				dl2.pushFront(i);
				dl2.pushBack(i);
			}

			//dl2.clearRecursive();
			dl1.clear();
		}
	}
	*/
	// }}}

	// {{{ memory leak test, IntegerThing [DISABLED]
	// 4/29/2011 tested OK
	/*
	{
		DLList<IntegerThing> dl1;

		while (1) {
			DLList<IntegerThing> dl2;

			assert(0 == dl1.getNodeCount());
			assert(0 == dl2.getNodeCount());

			for (int i = 0; i < 100; i++) {
				dl1.pushFront(i);
				dl1.pushBack(i);
			}

			for (int i = 0; i < 100; i++) {
				dl2.pushFront(i);
				dl2.pushBack(i);
			}

			//dl2.clearRecursive();
			dl1.clear();
		}
	}
	*/
	// }}}

	cout << "All tests passed.\n";
}