gpt4 book ai didi

C++双向链表程序

转载 作者:行者123 更新时间:2023-11-30 04:13:12 25 4
gpt4 key购买 nike

几个月来,我一直在尝试自学编程,我已经买了大约三本书,并在 YouTube 上观看了大量视频,这些视频到目前为止都很棒。但是在上周左右,我被链表难住了!我明白了整个概念,但语法让我受不了!我已经写了一些非常简单的链表程序来练习,但现在我正在尝试编写一个程序,我将文件中的一些信息(申请签证的人的信息)加载到链表中,然后就可以了再次显示全部(一旦完成,我计划将其扩展到只显示某些节点​​并删除某些节点的位置,但我不会详细介绍)。希望下面显示的代码不会太长,如果我能得到一些指示(没有双关语意)或一些片段来帮助我理解这些东西,那就太棒了!

p.s 部分代码来自一本书中的类似示例。

这是主要的(大部分)循环来加载信息(它不起作用)

 List myList;
visa_application *visa;

Node * pNode;

string visa_type;//these are all the variables that i want to load from the file
int invoice_no;
string surname;
string firstname;
double contact;
string status;
string result;

ifstream in;
in.open("applications.txt",ios::in);



while (!in.eof()){
pNode = new Node;
visa = new visa_application; //allocate memory for nodes

in >> visa-> visa_type >> visa->invoice_no >> visa-> surname;
in >> visa-> firstname >> visa-> contact >> visa-> status >> visa-> result ;

pNode->nData = &visa; //put some data in the node
myList.appendNode(pNode); //add node to list

}

这是我的类节点头文件

 class Node
{
friend class List;
private:
node_data * nData;
Node *pNext;
Node *pPrev;
public:
Node (node_data * data){nData = data;
pNext = NULL;
pPrev = NULL;}
node_data * getData(){return nData;}
};

这是类列表头文件

class List
{
private:
Node *pHead;
Node *pTail;
Node *createNode(node_data * data);
public:
List ();
~List();
Node *getpHead (){ return pHead;}
Node *getpTail (){return pTail;}
Node *previousNode(Node *pNode){return pNode->pPrev;}
Node *nextNode (Node *pNode){return pNode->pNext;}
void appendNode(node_data * value);
void insertNode(node_data * value, Node *pAfter);
void removeNode(Node *pNode);
bool isEmpty();
void printList();
};

List ::List() {
pHead=NULL;
pTail=NULL;
}

List ::~List(){
while (!isEmpty()) //keep on removing until the
//head points to NULL
removeNode(pHead);
cout << "List deleted\n";
}

Node * List::createNode(node_data * data){
Node * pNode = new Node (data); //allocate memory for new node and
//intialize value to data
return pNode;
}

bool List ::isEmpty(){
return pHead == NULL;
}

void List ::appendNode(node_data * value)
{
Node * pNode = createNode(value);

if (isEmpty()) { //if list is empty
pHead = pNode; //make head point to pNode
pNode->pPrev = NULL;
}
else { //otherwise
pTail->pNext = pNode; //make tail point to pNode
pNode->pPrev = pTail;
}
pTail = pNode; //tail is now pNode
pNode->pNext = NULL; //pNode next now points to NULL
}


void List ::insertNode(node_data * value, Node *pAfter)
{
Node *pNode = createNode(value);
pNode->pNext = pAfter->pNext;
pNode->pPrev = pAfter;

if (pAfter->pNext != NULL)
pAfter->pNext->pPrev = pNode;
else
pTail = pNode;
pAfter->pNext = pNode;
}

void List ::removeNode(Node *pNode)
{
if (pNode->pPrev == NULL) //if removing the head
pHead = pNode->pNext;

else
pNode->pPrev->pNext = pNode->pNext; //if removing a middle node

if (pNode->pNext == NULL) //if removing the tail
pTail = pNode->pPrev;

else
pNode->pNext->pPrev = pNode->pPrev;
pNode = NULL;
delete pNode; //*free the memory
}

void List ::printList()
{
Node *pNode=pHead;
if (isEmpty())
cout << "The list is empty\n";
else
for (pNode = pHead; pNode != NULL; pNode = pNode->pNext)
pNode->nData->print();
}

我类签证申请头文件

class visa_application
{
public:
// class constructor
visa_application();
// class destructor
~visa_application();



private:
string visa_type;
int invoice_no;
string surname;
string firstname;
double contact;
string status;
string result;
};

最后是签证申请.cpp

     visa_application::visa_application()
{
string visa_type = none;
int invoice_no = 0;
string surname = none;
string firstname = none;
double contact = 00;
string status = none;
string result = none;
}

class destructor
visa_application::~visa_application()
{
// insert your code here
}

我收到错误“没有匹配函数调用 `List::appendNode(Node*&)'”以及其他一些事情。无论如何我知道这很长但是如果我能得到一些就更好了帮助,我没有导师或老师或类似的人来帮助我,所以任何反馈将不胜感激!谢谢!

**编辑错误信息是:

 no matching function for call to `Node::Node()' 
candidates are: Node::Node(const Node&)
Node::Node(node_data*)
no matching function for call to `List::appendNode(Node*&)'
candidates are: void List::appendNode(node_data*)

最佳答案

据我所知,您的 List 只有方法:

void List::appendNode(node_data * value)

但在您的 main 中,您使用类 Node 的对象调用方法。尝试

void List ::appendNode(Node * pNode)
{

代替:

void List ::appendNode(node_data * value)
{
Node * pNode = createNode(value);

此外,正如评论中已经建议的那样。始终在您的问题中写下整个错误代码,包括位置。最佳做法是也包括编译器和操作系统。

关于C++双向链表程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19490023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com