本文整理汇总了C++中DLList::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ DLList::clear方法的具体用法?C++ DLList::clear怎么用?C++ DLList::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DLList
的用法示例。
在下文中一共展示了DLList::clear方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: processFile
void processFile (string filename) {
DLList list;
stringstream ss;
ifstream fin(filename.c_str());
if(!fin.fail()) {
string nextline;
while(!fin.eof()) {
getline(fin, nextline);
if(isalpha(nextline[0])) {
list.insert(nextline);
}
else {
list.cycle(atoi(nextline.c_str()));
cout << list.ToString() <<endl << "Won this round" << endl;
cout << list.getDeleted() << endl << "loses this round. " << endl << endl;
}
}
cout <<"Congratulations to: " << list.getFront() << endl << "you won!" << endl;
fin.close();
list.clear();
cout << "INPUT FINISHED" << endl;
}
else {
cout << "Unable to open " << filename << " for processing." << endl;
}
}
示例2: setFibNumber
/**********************************************************************
* Fib - setFibNumber
* Inputs: n (int)
* Calculates the n-th Fibonacci number
***********************************************************************/
void Fib::setFibNumber(int n)
{
if (n < 1)
{
cout << "Fibonacci subscript cannot be less than 1\n";
return;
}
/*need to clear fibNumber. setFibNumber() may be called several times on an object and you would have problems if you didn't erase all previous data*/
fibNumber.clear();
fibNumber.insert(1,0); // fibNumber will start as f1. f1 = 1;
fibSubscript = n;
if (n < 3) // can return here if n is 1 or 2.
return;
DLList<int> f2;
f2.insert(1,0); // fibNumber(aka f1) = 1 and f2 = 1. We are rdy to start.
Node<int>* p1;// traversal pointers for DLList objects fibNumber(f1) and f2
Node<int>* p2;
int temp = 0; // used as f1's node value placeholder.
int carry = 0;
for (int i = 0; i < n - 2; i++) //run n - 2 times. f1 and f2 done already
{
//reset pointers to respective beginnings of lists. reset carry to 0
p1 = fibNumber.getFirstNode();
p2 = f2.getFirstNode();
carry = 0;
//Add both lists. fibNumber becomes sum, f2 becomes what fibNumber was
while (p1 != NULL)
{
temp = p1->getData();
p1->setData((p1->getData() + p2->getData() + carry) % 1000000000);
carry = (temp + p2->getData()) / 1000000000;
p2->setData(temp);
p1 = p1->getNext();
p2 = p2->getNext();
}
if (carry == 1) // Account for carry on addition of final nodes.
{
fibNumber.insert(1, fibNumber.getNumItems());
f2.insert(0, f2.getNumItems());
}
}
}
示例3: unittest2
//.........这里部分代码省略.........
evaluate(list.getSize() == 6 && list.toString() == "5,10,20,30,50,55");
cout << "Passed TEST 8: insert(20)/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 8: insert(20)/getSize/toString #\n";
}
list.insert(40);
try {
evaluate(list.getSize() == 7 && list.toString() == "5,10,20,30,40,50,55");
cout << "Passed TEST 9: insert(40)/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 9: insert(40)/getSize/toString #\n";
}
list.insert(30);
try {
evaluate(list.getSize() == 8 && list.toString() == "5,10,20,30,30,40,50,55");
cout << "Passed TEST 10: insert(30)/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 10: insert(30)/getSize/toString #\n";
}
list.insert(5);
try {
evaluate(list.getSize() == 9 && list.toString() == "5,5,10,20,30,30,40,50,55");
cout << "Passed TEST 11: insert(5)/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 11: insert(5)/getSize/toString #\n";
}
try {
evaluate(list.removeFirst(1) == false);
cout << "Passed TEST 12: removeFirst(1) \n";
} catch (MyException e) {
cout << "# FAILED TEST 12: removeFirst(1) #\n";
}
try {
evaluate(list.removeFirst(5) == true && list.getSize() == 8 && list.toString() == "5,10,20,30,30,40,50,55");
cout << "Passed TEST 13: removeFirst(5)/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 13: removeFirst(5)/getSize/toString #\n";
}
try {
evaluate(list.removeFirst(30) == true && list.getSize() == 7 && list.toString() == "5,10,20,30,40,50,55");
cout << "Passed TEST 14: removeFirst(30)/getSize/toString \n";
} catch (MyException e){
cout << "# FAILED TEST 14: removeFirst(30)/getSize/toString #\n";
}
try {
evaluate(list.removeFirst(30) == true && list.getSize() == 6 && list.toString() == "5,10,20,40,50,55");
cout << "Passed TEST 15: removeFirst(30)/getSize/toString \n";
} catch (MyException e){
cout << "# FAILED TEST 15: removeFirst(30)/getSize/toString #\n";
}
try {
evaluate(list.removeFirst(55) == true && list.getSize() == 5 && list.toString() == "5,10,20,40,50");
cout << "Passed TEST 16: removeFirst(55)/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 16: removeFirst(55)/getSize/toString #\n";
}
try {
evaluate(list.removeFirst(10) == true && list.getSize() == 4 && list.toString() == "5,20,40,50");
cout << "Passed TEST 17: removeFirst(10)/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 17: removeFirst(10)/getSize/toString #\n";
}
list.popFront();
try {
evaluate(list.getSize() == 3 && list.toString() == "20,40,50");
cout << "Passed TEST 18: removeHead/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 18: removeHead/getSize/toString #\n";
}
list.popBack();
try {
evaluate(list.getSize() == 2 && list.toString() == "20,40");
cout << "Passed TEST 19: removeTail/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 19: removeTail/getSize/toString #\n";
}
list.clear();
try {
evaluate(list.getSize() == 0 && list.toString() == "");
cout << "Passed TEST 20: clear/getSize/toString \n";
} catch (MyException e) {
cout << "# FAILED TEST 20: clear/getSize/toString #\n";
}
cout << "\nUNIT TEST COMPLETE\n\n";
}
示例4: processFile
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
//.........这里部分代码省略.........
示例5: main
int main()
{
using namespace std;
DLList<int> myIntDLL;
cout << "Prepending: 5" << endl;
myIntDLL.prepend(5);
myIntDLL.printList();
cout << "Prepending: 4" << endl;
myIntDLL.prepend(4);
myIntDLL.printList();
cout << "Prepending: 3" << endl;
myIntDLL.prepend(3);
myIntDLL.printList();
cout << "Appending: 9" << endl;
myIntDLL.append(9);
myIntDLL.printList();
cout << "Appending: 8" << endl;
myIntDLL.append(8);
myIntDLL.printList();
cout << "Appeding: 7" << endl;
myIntDLL.append(7);
myIntDLL.printList();
cout << "Current data: " << *myIntDLL.getValue() << endl;
cout << "Moving to end" << endl;
myIntDLL.moveToEnd();
cout << "Current data: " << *myIntDLL.getValue() << endl;
cout << "Moving to start" << endl;
myIntDLL.moveToStart();
cout << "Current data: " << *myIntDLL.getValue() << endl;
cout << "Iterating through list with next function" << endl;
for (int i = 0; i < 10; i++)
{
cout << "Current data: " << *myIntDLL.getValue() << endl;
cout << "Next: " << myIntDLL.next() << endl;
}
cout << "Iterating through list with prev function" << endl;
for (int i = 0; i < 10; i++)
{
cout << "Current data: " << *myIntDLL.getValue() << endl;
cout << "Prev: " << myIntDLL.prev() << endl;
}
cout << "Number of links active: " << myIntDLL.numActive() << endl;
cout << "Number of links free: " << myIntDLL.numFree() << endl;
cout << "Clearing list" << endl;
myIntDLL.clear();
myIntDLL.printList();
cout << "Number of links active: " << myIntDLL.numActive() << endl;
cout << "Number of links free: " << myIntDLL.numFree() << endl;
return 0;
}
示例6: main
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;
//.........这里部分代码省略.........
示例7: processFile
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;
}
//.........这里部分代码省略.........
示例8: run_project3
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){
//.........这里部分代码省略.........
示例9: main
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;
//.........这里部分代码省略.........
示例10: processFile
void processFile (string filename) {
stringstream ss;
int value = 0;
DLList* list = NULL;
char firstCharacter;
ifstream fin(filename.c_str());
if(!fin.fail()) {
string nextline;
while(!fin.eof()) {
getline(fin, nextline);
firstCharacter = nextline[0];
switch(firstCharacter) {
case '#':
break;
case 'C':
if(list != NULL) { //if there was a previous list delete it
delete list;
}
else {
//creates a new list of type DLList
list = new DLList();
cout << "LIST CREATED" << endl;
}
break;
case 'X':
//if no list was created yet create list first
if(list == NULL) {
cout << "MUST CREAT LIST INSTANCE" << endl;
} //if there was a list clear it with the clear function
else {
list->clear();
cout << "LIST CLEARED" << endl;
}
break;
case 'D':
//if there is a list deleted it
if(list!= NULL) {
delete list;
cout << "LIST DELETED" << endl;
}//if there isn't show warning
else
cout << "MUST CREAT LIST INSTANCE" << endl;
break;
case 'I':
if(list == NULL) {
cout << "MUST CREAT LIST INSTANCE" << endl;
}
else {
ss.str(nextline.substr(2));//gets the next whole value after the first value on a line
ss >> value;//puts the from input into an int
list->insert(value);//inserts the value into a node on the list
cout << "VALUE " << value << " INSERTED" << endl;
ss.clear();
ss.str("");
}
break;
case 'F':
ss.str(nextline.substr(2));
ss >> value;
list->insertHead(value);//inserts the value from input into the first node
cout << "VALUE " << value << " ADDED TO HEAD" << endl;
ss.clear();
ss.str("");
break;
case 'B':
ss.str(nextline.substr(2));
ss >> value;
list->insertTail(value);
cout << "VALUE " << value << " ADDED TO TAIL" << endl;
ss.clear();
ss.str("");
break;
case 'A':
if(list == NULL) {
cout << "VALUE NULL AT HEAD" << endl;
}
else {
list->getHead();
cout << "VALUE " << list->getHead() << " AT HEAD" << endl;
}
break;
case 'Z':
list->getTail();
cout << "VALUE " << list->getTail() << " AT TAIL" << endl;
break;
case 'T':
if(list == NULL) {
cout << "MUST CREATE LIST INSTANCE" << endl;
}
else if (list->getSize() > 0) {
list->removeHead();
cout << "REMOVED HEAD" << endl;
}
else {
cout << "LIST EMPTY" << endl;
}
break;
case 'K':
if(list == NULL) {
//.........这里部分代码省略.........
示例11: processIntFile
bool processIntFile(string filename)
{
ifstream infile(filename.c_str());
if (infile.is_open()){
string str;
DLList<int>* list = NULL;
bool listAlive = false;
while (! infile.eof()){
getline(infile, str);
int data = 0;
if (str[0] == '#'){
}
else if (str[0] == 'C'){
if(listAlive){
delete list;
list = new DLList<int>;
}
else{
list = new DLList<int>;
listAlive = true;
}
cout << "LIST CREATED" << endl;
}
else if (str[0] == 'X'){
if(listAlive){
list->clear();
cout << "LIST CLEARED" << endl;
}
else if(!listAlive){
cout << "MUST CREATE LIST INSTANCE\n";
}
else{
cout << "LIST EMPTY\n";
}
}
else if (str[0] == 'D'){
if(listAlive){
delete list;
list = NULL;
listAlive = false;
cout << "LIST DELETED" << endl;
}
else{
cout << "MUST CREATE LIST INSTANCE\n";
}
}
else if (str[0] == 'I'){
if(listAlive){
stringstream ss(str.substr(2));
ss >> data;
list->insert(data);
cout << "VALUE " << data << " INSERTED" << endl;
}
else if(!listAlive){
cout << "MUST CREATE LIST INSTANCE\n";
}
else{
cout << "LIST EMPTY\n";
}
}
else if (str[0] == 'F'){