这是一个家庭作业!
我的任务是创建两个双向链表,一个用于偶数,另一个用于奇数。这些列表将通过从整数文件中读取来填充。我必须搜索正确的列表(偶数或奇数,取决于文件中的编号),以查看从文件中读取的编号是否已在列表中。如果是,我必须忽略这个数字,继续前进。我们不允许使用OOP,但我们正在研究ADT的概念。请不要在您的答案中包含任何OOP,因为我不被允许使用它,我还不知道如何阅读它。
当我运行我的代码时,它编译没有错误,但它返回一个-1。在做了一些调试之后,我发现它正在读入并为每个列表插入第一个数字,但随后在插入第二个数字时挂起。但是,我似乎找不出是什么原因导致它以-1退出。如果有人能给我指出问题所在的正确方向,我将不胜感激。
哦,另外,这个程序把你想要读取整数的文件的文件名和另一个稍后会在程序中使用的文件(我还没有读到那个部分)作为命令行参数。
#include <iostream>
#include <fstream>
#include <cstddef>
#include <iomanip>
using namespace std;
struct doublyLinkList{
int numberRead;
doublyLinkList* prev;
doublyLinkList* next;
};
struct topAndCounter{
int counter;
doublyLinkList* head;
};
void initializeList(topAndCounter& aLinkList);
bool searchOrderedList(topAndCounter aLinkList, int tempIntHold);
void orderedListInsertion(topAndCounter& aLinkList, bool& success, int tempIntHold);
bool isEmptyList(topAndCounter& aLinkList);
int main(int argC, char* argV[]) // allows usage of command line arguments
{
ifstream intsForList;
ifstream intsToSearchFor;
topAndCounter evenList; // even list struct
topAndCounter oddList; // odd list struct
int tempIntHold; // holds integer read in from file
string storeIntsFile; // holds name of file
string intsToFindFile; // holds name of file
bool success = true; // whether memory was successfully allocated
cout << endl << endl;
cout << "Program to read in and store a list of unique integers from one ";
cout << "file and match numbers to it from a second file." << endl << endl;
switch(argC) // takes the argument count
{
case 1:
cout << "You didn't enter any filenames on the command line." << endl;
cout << "Please enter them now" << endl;
cout << "Filename for list of integers to store: ";
cin >> storeIntsFile;
cout << "Filename for integers to search list for: ";
cin >> intsToFindFile;
break;
case 2:
cout << "You didn't enter the file name of integers to search the list";
cout << " for" << endl;
cout << "Please enter it now: ";
cin >> intsToFindFile;
storeIntsFile = argV[1];
break;
case 3:
storeIntsFile = argV[1];
intsToFindFile = argV[2];
break;
}
intsForList.open(storeIntsFile.c_str()); // trys to open first file
intsToSearchFor.open(intsToFindFile.c_str()); // tries to open second file
// if files are not opened successful
if ((!intsForList) || (!intsToSearchFor))
{
while(!intsForList) // while the first file is not opened successfully
{
cout << "File for integers to populate list not found. " << endl;
cout << "Please enter another filename: ";
cin >> storeIntsFile;
intsForList.open(storeIntsFile); // try to open file
} // end while
while(!intsToSearchFor) // while the second file is not opened successfully
{
cout << "File containing integers to search list for not found.";
cout << endl;
cout << "Please enter another filename: ";
cin >> intsToFindFile;
intsToSearchFor.open(intsToFindFile); // try to open file
} // end while
} // end if
initializeList(evenList); // function call to initialize list to NULL
initializeList(oddList); // function call to initialize list to NULL
intsForList >> tempIntHold; // read in the first integer from the file
while((intsForList) && (success)) // while there is still integers in the file and memory
// allocated successfully
{
if (tempIntHold % 2 == 0) // if the number read from the file is even
{
orderedListInsertion(evenList, success, tempIntHold); // call the even sort funct
} // end if
else // if the number is odd
{
orderedListInsertion(oddList, success, tempIntHold); // odd num sort funct
} // end else
intsForList >> tempIntHold; // try to read next integer in file
} // end while
return 0;
}
void initializeList(topAndCounter& aLinkList)
{
aLinkList.counter = 0;
aLinkList.head = NULL; // initialize head pointer to null
return;
}
// function to search the list to see if the number read in from file is already in list
bool searchOrderedList(topAndCounter aLinkList, int tempIntHold)
{
bool found = false;
doublyLinkList* searchNode; // pointer for searching list
searchNode = aLinkList.head; // set pointer to top of list
while ((searchNode != NULL) && (!found)) // there are nodes in list and the number is
// not found
{
if (searchNode -> numberRead >= tempIntHold)
{
found = true;
}
else
{
searchNode = searchNode -> next; // move pointer to next node
}
}
if (found)
{
found = (searchNode -> numberRead == tempIntHold);
}
return found;
} // end function
void orderedListInsertion(topAndCounter& aLinkList, bool& success, int tempIntHold)
{
doublyLinkList* newNode;
doublyLinkList* searchNode;
bool intInUse;
bool found;
newNode = new (nothrow) doublyLinkList;
if (newNode != NULL)
{
intInUse = searchOrderedList(aLinkList, tempIntHold);
if (intInUse)
{
cout << "The current interger is already in the list. It will now ";
cout << "be ignored" << endl;
} // end if
else
{
newNode -> numberRead = tempIntHold;
if (isEmptyList(aLinkList))
{
newNode -> prev = NULL;
newNode -> next = NULL;
aLinkList.head = newNode;
} // end if
else
{
searchNode = aLinkList.head;
found = false;
while((searchNode != NULL) && (!found))
{
if (searchNode -> numberRead > newNode -> numberRead)
{
found = true;
} // end if
else
{
searchNode = searchNode -> next;
} // end else
} // end while
if (searchNode == aLinkList.head)
{
newNode -> next = searchNode;
newNode -> prev = NULL;
searchNode -> prev = newNode;
aLinkList.head = newNode;
} // end if
else
{
if(searchNode == NULL)
{
newNode -> prev = searchNode -> prev;
newNode -> next = searchNode;
} // end if
else
{
newNode -> prev = searchNode -> prev;
newNode -> next = searchNode;
searchNode -> prev = newNode;
} // end else
} // end else
newNode -> next = searchNode;
newNode -> prev = searchNode -> prev;
searchNode -> prev = newNode;
} // end else
aLinkList.counter++;
success = true;
} // end else
} // end if
else
{
cout << "No more heap memory. No more integers from the list will be";
cout << " stored." << endl;
success = false;
} // end else
return;
} // end function
bool isEmptyList(topAndCounter& aLinkList)
{
if (aLinkList.head == NULL)
{
return true;
}
else
{
return false;
}
} // end function发布于 2014-07-11 23:04:32
忽略“这是作业,所以我不能把它做好”这一方面,结果可能是这样的:
#include <set>
#include <vector>
#include <string>
#include <iostream>
int main() {
std::vector<std::set<int>> sets(2);
std::vector<std::string> names {"Even numbers", "Odd numbers"};
int num;
while (std::cin >> num)
sets[num % 2].insert(num);
for (int i=0; i<sets.size(); i++) {
std::cout << "\n" << names[i];
for (int j : sets[i])
std::cout << "\t" << j;
}
}不,不是面向对象的。事实上,标准库的这一部分的主要设计者(Alexander Stepanov)非常清楚他不是特别喜欢面向对象的事实,并且真的没有时间去做它。
至于其余的需求,很抱歉,但它们太愚蠢了,无法考虑,更不用说实现了。与所有太多教师的明显信念相反,将集合实现为链表是一个极其糟糕的想法。
发布于 2014-07-11 23:56:27
我不会在这里给你完整的答案,但我确实有一些评论,希望能帮助你自己解决这个问题。
if(searchNode == NULL) { newNode -> prev = searchNode -> prev;// HERE,searchNode == NULL newNode -> next = searchNode;} // end if else { newNode -> prev = searchNode -> prev;newNode -> next = searchNode;searchNode prev =;} // end else我也觉得你有很多代码应该分解到更小的函数中。
我给你的建议是将你的代码重写成小得多的函数,每个函数都有一个非常具体的任务。
例如:
//插入一个元素,并返回指向新插入的元素的指针。doublyLinkList * insert_at_end( topAndCounter &head,int val );doublyLinkList * insert_before( topAndCounter &head,doublyLinkList *existing,int val );
这样说来,如果您有我上面提到的两个函数,那么您在orderedListInsertion中的所有代码都可以简化为:
orderedListInsertion(topAndCounter& aLinkList,bool& success,int tempIntHold) { success = false;doublyLinkList *current = aLinkList->head;while (current != NULL) { if (current->numberRead == tempIntHold ) //重复返回;else if ( tempIntHold < current->numberRead ){ insert_before(aLinkList,current,tempIntHold);success = true;return;}} insert_at_end(aLinkList,tempIntHold);success = true;}
更好的是,事先测试这些小函数要容易得多,这样你就可以确定你的程序的哪些部分工作了。
https://stackoverflow.com/questions/24700284
复制相似问题