我必须写一个“程序员友好”的链表。我有两个类,您的主类和您的节点类。我的节点类将是我的数据结构,并且必须包含下面列出的信息。所有节点信息都将存储在节点类中。我的主类应该只是调用节点函数的驱动程序。
主类应包含:
您的Node类应执行以下操作:
注意:
我的Node.cpp代码如下所示:引发此错误:引发异常:读取访问冲突。 这是nullptr。
#include "stdafx.h"
#include "Node.h"
#include "iomanip"
Node::Node()
{
}
Node::~Node()
{
}
void Node::AppendFIFO(int id, string userName, string password, string fName, string lName)
{
}
void Node::AppendLIFO(int id, string userName, string password, string fName, string lName)
{
//cout << "Yo" << endl;
Node *curNode;
curNode = head;
cout << "user name:" << userName.length();
cout << "Password:" << password.length();
cout << "f name:" << fName.length();
cout << "l name:" << lName.length();
while (curNode != nullptr)
{
curNode = curNode->next;
}
}
void Node:: display()
{
}
我的Node.h文件如下所示
#pragma once
#include "iostream"
#include "string"
#include "iomanip"
using namespace std;
class Node
{
public:
Node();
~Node();
void AppendFIFO(int, string, string, string, string);
void AppendLIFO(int, string, string, string, string);
void display();
private:
int ID;
string userName;
string password;
string firstName;
string lastName;
Node *next;
Node *head;
};
我的main.cpp文件代码如下所示
#include "stdafx.h"
#include "iostream"
#include "string"
#include "Node.h"
#include "iomanip"
using namespace std;
int main()
{
Node *LIFO = nullptr;
Node *FIFO = nullptr;
//The 10 users information for LIFO
LIFO->AppendLIFO(10, "postMalone", "Asdkc34D", "Austin", "Post");
LIFO->AppendLIFO(9, "h20", "akdjW78v", "Benny", "Washington");
LIFO->AppendLIFO(8, "testing", "aklc5kaS", "Timmy", "Trump");
LIFO->AppendLIFO(7, "Rob-by", "robby3939", "Robert", "Malone");
LIFO->AppendLIFO(6, "TracyLMoore", "Moore098", "Tracy", "Moore");
LIFO->AppendLIFO(5, "Billybill", "Bb234", "Bill", "Prescott");
LIFO->AppendLIFO(4, "beth-09", "09ASDN", "Beth", "Richards");
LIFO->AppendLIFO(3, "Gabe123", "123ilkkSW", "Gabriel", "Smith");
LIFO->AppendLIFO(2, "sthomas", "sthom56712", "Shannon", "Thomas");
LIFO->AppendLIFO(1, "bob", "LKJG840", "Bobby", "Steve");
LIFO->AppendLIFO(0, "zmoore00", "00SDJ", "Zackary", "Moore");
//The 10 users information for FIFO
FIFO->AppendFIFO(10, "postMalone", "Asdkc34D", "Austin", "Post");
FIFO->AppendFIFO(9, "h20", "akdjW78v", "Benny", "Washington");
FIFO->AppendFIFO(8, "testing", "aklc5kaS", "Timmy", "Trump");
FIFO->AppendFIFO(7, "Rob-by", "robby3939", "Robert", "Malone");
FIFO->AppendFIFO(6, "TracyLMoore", "Moore098", "Tracy", "Moore");
FIFO->AppendFIFO(5, "Billybill", "Bb234", "Bill", "Prescott");
FIFO->AppendFIFO(4, "beth-09", "09ASDN", "Beth", "Richards");
FIFO->AppendFIFO(3, "Gabe123", "123ilkkSW", "Gabriel", "Smith");
FIFO->AppendFIFO(2, "sthomas", "sthom56712", "Shannon", "Thomas");
FIFO->AppendFIFO(1, "bob", "LKJG840", "Bobby", "Steve");
FIFO->AppendFIFO(0, "zmoore00", "00SDJ", "Zackary", "Moore");
system("pause");
return 0;
}
我不知道错误出了什么问题。我也无法弄清楚如何确定一组字符串中最长的字符串的长度,然后将其存储以用于setw以便正确显示。
答案 0 :(得分:0)
它抛出此错误:抛出异常:读取访问冲突。这是nullptr。
您的程序首先要做
int main() { Node *LIFO = nullptr; ... LIFO->AppendLIFO(10, "postMalone", "Asdkc34D", "Austin", "Post");
,然后在 Node :: AppendLIFO 中完成
curNode = head;
因此您尝试在 this 为 nullptr 时访问 head ,然后稍后使用未定义的结果进行
curNode = curNode->next;
当前,您的程序永远不会创建 Node 的实例,它不能那样工作