简单链表核心转储
Simple Linked List Core Dumps
C++ 新手尝试 grasp/implement 通过用户可以浏览的 maze/game 形式的第一个链表。我下面的代码可以编译,但在我输入字符串后崩溃得很厉害。
#include <iostream>
#include <string>
//main node, label is current room user is in
struct RoomNode
{
std::string label;
RoomNode* north;
RoomNode* south;
RoomNode* east;
RoomNode* west;
//constructor, all locations set to NULL by default
RoomNode(std::string name)
{
label = name;
north = NULL;
south = NULL;
east = NULL;
west = NULL;
}
};
int main()
{
bool exitFound = false;
std::string userDirection;
std::string userExit = "EXIT";
//the maze has 8 rooms
//8th room has exit
//create necessary nodes
RoomNode *head;
head = new RoomNode("Beginning (Room 1)");
RoomNode *secondRoom = new RoomNode("Room 2");
RoomNode *thirdRoom = new RoomNode("Room 3");
RoomNode *fourthRoom = new RoomNode("Room 4");
RoomNode *fifthRoom = new RoomNode("Room 5");
RoomNode *sixthRoom = new RoomNode("Room 6");
RoomNode *seventhRoom = new RoomNode("Room 7");
RoomNode *eighthRoom = new RoomNode("EXIT");
//build linked list
//RoomNode contructor defaults all directions to NULL
head->east = secondRoom;
secondRoom->south = thirdRoom;
secondRoom->west = head;
thirdRoom->west = fifthRoom;
thirdRoom->east = fourthRoom; //fourth room is dead end
thirdRoom->north = secondRoom;
fifthRoom->north = head;
fifthRoom->south = sixthRoom;
sixthRoom->west = seventhRoom;
seventhRoom->north = fourthRoom;
seventhRoom->south = eighthRoom;
std::cout << "You must navigate your way out of the maze." << std::endl;
std::cout << "Do so by specifying the direction you would" << std::endl;
std::cout << "like to travel (East, West, North, or South)." << std::endl;
RoomNode *traverse = head; //point to beginning of list
do
{
std::cout << "You are currently in room " << traverse-> label << std::endl;
std::cout << "Enter the direction you wish to travel in (lowercase)." << std::endl;
getline(std::cin, userDirection);
if (userDirection != "east" || userDirection != "north" ||
userDirection != "west" || userDirection != "south")
{
std::cout << "You did not enter a valid direction." << std::endl;
std::cout << "Please try again, this time entering" << std::endl;
std::cout << "north, east, west, or south." << std::endl;
break;
}
else
{
if (userDirection == "west")
{
if (traverse->west == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->west; //update current position
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
else if (userDirection == "north")
{
if (traverse->north == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->north;
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
else if (userDirection == "east")
{
if (traverse->south == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->east;
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
else if (userDirection == "south")
{
if (traverse->south == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->south;
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
}
} while (exitFound);
delete head;
delete traverse;
delete secondRoom;
delete thirdRoom;
delete fourthRoom;
delete fifthRoom;
delete sixthRoom;
delete seventhRoom;
delete eighthRoom;
return 0;
}
我知道它可以缩短很多,但我的主要重点是让链表正常工作,有人可以帮我吗?谢谢
我发现遇到"delete traverse"会出现这种情况。
traverse 指向别的东西,而不是它自己的对象,在本例中是 head,它已经被删除了。
这就是它在输入后跳出循环的原因:
if (userDirection != "east" || userDirection != "north" ||
userDirection != "west" || userDirection != "south")
请考虑使用 && 而不是 ||,这样 userDirection 的有效值将导致表达式为 false 并且流程会落入 else 块以进行方向处理。
另外,exitFound为false,所以它可能会在第一次迭代后退出循环。也许 while 条件需要是 while (!exitFound).
C++ 新手尝试 grasp/implement 通过用户可以浏览的 maze/game 形式的第一个链表。我下面的代码可以编译,但在我输入字符串后崩溃得很厉害。
#include <iostream>
#include <string>
//main node, label is current room user is in
struct RoomNode
{
std::string label;
RoomNode* north;
RoomNode* south;
RoomNode* east;
RoomNode* west;
//constructor, all locations set to NULL by default
RoomNode(std::string name)
{
label = name;
north = NULL;
south = NULL;
east = NULL;
west = NULL;
}
};
int main()
{
bool exitFound = false;
std::string userDirection;
std::string userExit = "EXIT";
//the maze has 8 rooms
//8th room has exit
//create necessary nodes
RoomNode *head;
head = new RoomNode("Beginning (Room 1)");
RoomNode *secondRoom = new RoomNode("Room 2");
RoomNode *thirdRoom = new RoomNode("Room 3");
RoomNode *fourthRoom = new RoomNode("Room 4");
RoomNode *fifthRoom = new RoomNode("Room 5");
RoomNode *sixthRoom = new RoomNode("Room 6");
RoomNode *seventhRoom = new RoomNode("Room 7");
RoomNode *eighthRoom = new RoomNode("EXIT");
//build linked list
//RoomNode contructor defaults all directions to NULL
head->east = secondRoom;
secondRoom->south = thirdRoom;
secondRoom->west = head;
thirdRoom->west = fifthRoom;
thirdRoom->east = fourthRoom; //fourth room is dead end
thirdRoom->north = secondRoom;
fifthRoom->north = head;
fifthRoom->south = sixthRoom;
sixthRoom->west = seventhRoom;
seventhRoom->north = fourthRoom;
seventhRoom->south = eighthRoom;
std::cout << "You must navigate your way out of the maze." << std::endl;
std::cout << "Do so by specifying the direction you would" << std::endl;
std::cout << "like to travel (East, West, North, or South)." << std::endl;
RoomNode *traverse = head; //point to beginning of list
do
{
std::cout << "You are currently in room " << traverse-> label << std::endl;
std::cout << "Enter the direction you wish to travel in (lowercase)." << std::endl;
getline(std::cin, userDirection);
if (userDirection != "east" || userDirection != "north" ||
userDirection != "west" || userDirection != "south")
{
std::cout << "You did not enter a valid direction." << std::endl;
std::cout << "Please try again, this time entering" << std::endl;
std::cout << "north, east, west, or south." << std::endl;
break;
}
else
{
if (userDirection == "west")
{
if (traverse->west == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->west; //update current position
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
else if (userDirection == "north")
{
if (traverse->north == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->north;
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
else if (userDirection == "east")
{
if (traverse->south == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->east;
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
else if (userDirection == "south")
{
if (traverse->south == NULL)
{
std::cout << "Sorry, the door in that direction is boarded shut!" << std::endl;
break;
}
else
{
std::cout << "You moved " << userDirection << "!" << std::endl;
traverse = traverse->south;
}
if (traverse->label == userExit)
{
std::cout << "You found the exit! Congratulations!" << std::endl;
std::cout << "You were able to escape the serpent!" << std::endl;
exitFound = true;
break;
}
}
}
} while (exitFound);
delete head;
delete traverse;
delete secondRoom;
delete thirdRoom;
delete fourthRoom;
delete fifthRoom;
delete sixthRoom;
delete seventhRoom;
delete eighthRoom;
return 0;
}
我知道它可以缩短很多,但我的主要重点是让链表正常工作,有人可以帮我吗?谢谢
我发现遇到"delete traverse"会出现这种情况。 traverse 指向别的东西,而不是它自己的对象,在本例中是 head,它已经被删除了。
这就是它在输入后跳出循环的原因:
if (userDirection != "east" || userDirection != "north" ||
userDirection != "west" || userDirection != "south")
请考虑使用 && 而不是 ||,这样 userDirection 的有效值将导致表达式为 false 并且流程会落入 else 块以进行方向处理。
另外,exitFound为false,所以它可能会在第一次迭代后退出循环。也许 while 条件需要是 while (!exitFound).