链表错误
linked list bug
我在C++开发一个程序,在那里我应该在刽子手上与计算机对战,我应该将特定的.cpp文件从使用向量转换为使用链表。虽然我似乎快完成了,但我不断遇到这个奇怪的错误,每次计算机猜错一个字母时,它都会导致我的程序崩溃。我通过调试器运行了程序,并在调用堆栈中弹出一条消息,内容类似于"程序收到 SIGSEGV,分段错误",以及此信息:
libstdc++6!_ZNSsC1ERKSs()
Guesser::guessACharacter(this=0x28feac) Line 56
main(argc=1, argv=0x7725a8) Line 30
崩溃似乎又回到了我的 Guesser::guessACharacter 函数的第 9 行的问题,我尝试将当前节点中的信息放入临时变量中。它看起来像这样:
string word = current->data;
以下是完整的代码:
#include "guesser.h"
#include "game.h"
#include <iostream>
#include <fstream>
using namespace std;
const std::string Guesser::alphabet = "abcdefghijklmnopqrstuvwxyz";
// Initialize the guesser for a game wit hthe indicated wordlength,
// using words from an indicated file.
Guesser::Guesser (int wordLength, const char* wordListFilename)
{
for (int i = 0; i < 26; ++i)
charactersTried[i] = false;
string word;
ifstream in (wordListFilename);
while (in >> word)
{
if (word.size() == wordLength)
{
// word is of desired length
if (word.find_first_not_of(alphabet) == string::npos) {
// word contains only lowercse alphabetics
//cerr<<"data before possible soln.:/n"<<possibleSolutions->data<<endl;
possibleSolutions->data = word;
//cerr<<"data after possible soln.:/n"<<possibleSolutions->data<<endl;
possibleSolutions = possibleSolutions -> next;
}
}
}
in.close();
}
/**
* Scan the words that are possible solutions so far, counting, for
* each letter not already tried, the number of words with that letter.
* Guess the letter that occurs in the most words.
*/
char Guesser::guessACharacter()
{
int counts[26];
for (int i = 0; i < 26; ++i)
counts[i] = 0;
// Count the number of words in which each letter can be found
for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
{
//cerr<<"data before possible soln.:/n"<<possibleSolutions->data<<endl;
string word = current->data;
//cerr<<"data in word:/n"<<word<<endl;
for (char c = 'a'; c <= 'z'; ++c)
{
if (!charactersTried[c- 'a'])
{
// Character c has not been tried yet
if (word.find(c) != string::npos)
// c is in this word
++counts[c - 'a'];
}
}
}
// Find the character that occurs in the most words
char guess = ' ';
int maxSoFar = -1;
for (char c = 'a'; c <= 'z'; ++c)
{
if (counts[c - 'a'] > maxSoFar)
{
guess = c;
maxSoFar = counts[c - 'a'];
}
}
if (maxSoFar <= 0)
{
guess = 'a';
while (charactersTried[guess-'a'])
++guess;
}
charactersTried[guess-'a'] = true;
return guess;
}
/**
* Following a successful guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that do not contain the
* guess character in the positions indicated in wordSoFar.
*/
void Guesser::characterIsInWord (char guess, const string& wordSoFar)
{
ListNode* remainingSolutions = 0;
for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
{
string wd = current-> data;
bool OK = true;
for (int k = 0; OK && k < wordSoFar.size(); ++k)
{
if (wordSoFar[k] == guess)
{
if (wd[k] != guess)
{
OK = false;
}
}
}
if (OK)
{
//cerr << "Keeping " << wd << endl;
remainingSolutions->data = wd;
//possibleSolutions->next = remainingSolutions;
}
}
possibleSolutions = remainingSolutions;
}
/**
* Following a mistaken guess of a letter in the word, make a pass through
* the possibleSolutions, removing all words that contain the
* guess character.
*/
void Guesser::characterIsNotInWord (char guess)
{
ListNode* remainingSolutions;
for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
{
string wd = current->data;
if (wd.find(guess) == string::npos)
{
remainingSolutions = new ListNode(wd, remainingSolutions);
}
}
possibleSolutions = remainingSolutions;
}
/**
* Guesser has lost the game. Look at the provider's actual word
* and gripe a bit about losing.
*/
void Guesser::admitToLoss (std::string actualWord, const string& wordSoFar)
{
bool match = actualWord.size() == wordSoFar.size();
for (int i = 0; match && i < actualWord.size(); ++i)
{
match = wordSoFar[i] == Game::FILL_CHARACTER ||
wordSoFar[i] == actualWord[i];
}
if (!match)
{
cout << "Ummm...your word '" << actualWord
<< "' does not match the patterh '"
<< wordSoFar <<"'.nDid you make a mistake somewhere?"
<< endl;
}
else
{
for (int i = 0; match && i < actualWord.size(); ++i)
{
if (wordSoFar[i] == Game::FILL_CHARACTER
&& charactersTried[actualWord[i]-'a'])
{
cout << "Did you forget to mention the '"
<< actualWord[i]
<< "' in position " << i+1 << "?"
<< endl;
return;
}
}
for (ListNode* current = possibleSolutions;(!match) && current !=
NULL; current = current-> next)
{
match = (actualWord == current->data);
current = current->next;
match = match && (current != 0);
}
//match = match && (current != 0);
if (match)
{
cout << "OK, I might have guessed that eventually." << endl;
}
else
{
cout << "Interesting, I don't know that word. Are you sure youn"
<< "spelled it correctly?." << endl;
}
}
}
我觉得这个问题的答案正盯着我的脸,但我就是想不通。我想知道其他人是否可以轻松发现我在这里做错了什么。
在下面的代码片段中
ListNode* remainingSolutions = 0;
for(ListNode* current = possibleSolutions; current != NULL; current = current-> next)
{
....
if (wordSoFar[k] == guess)
{
if (wd[k] != guess)
{
OK = false;
}
}
}
if (OK)
{
remainingSolutions->data = wd;
}
}
possibleSolutions = remainingSolutions;
如果访问,remainingSolutions
将为 0 或 NULL,这可能是原因之一。
同样在最后,它将 NULL 分配给 possibleSolutions
(我假设它是 linkedlist 的负责人),所以下次你访问它时,也许通过调用 guessACharacter()
,它会导致分段错误。
相关文章:
- 错误:需要类名(链表c++)
- 对单向链表进行排序时出现运行时错误
- 在链表中的第 n 位插入显示分割错误
- 链表错误的中间
- 为什么我在空指针错误(链表)中获取成员访问权限
- 查找显示链表时出现的错误
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 在 c++ 中实现链表时出现分段错误
- 双链表的擦除值函数,未知错误
- 删除链表中的节点 - 分段错误
- 分段错误通用双链表
- C++中链表的错误"Abort signal from abort(3) (sigabrt) "
- 奇怪的C++链表错误
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 为什么我的C++代码在以下打印链表的代码片段中显示分段错误?
- C++:按数字排序链表错误
- 为什么我在尝试添加两个链表时出现此错误?
- C++ largest_free节点函数段错误链表
- 析构函数分段错误链表
- c++分割错误链表元素添加