I've been trying to teach myself programming for a few months now, I've bought about three books and watched a tonne of videos on YouTube which has been great so far. But the last week or so I've been stumped thanks to linked lists! I get the whole concept but the syntax is killing me! I've written a few very simple linked lists programs for exercise but now I'm trying to write a program where I load in some information from a file (information of people who have applied for a visa) onto a linked a list then just display the whole lot again(once that is done I plan on expanding it to where I only display certain nodes and remove certain nodes but I wont go into detail). Hopefully the code shown below is not too lengthy and if I could just get some pointers (no pun intended) or some snippets to help me understand this stuff that would be awesome!
p.s some of this code is from a similar example in a book.
here is the main(most of it)with the loop to load the information(it doesn't work)
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
}
Here is my class node header file
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;}
};
here is class list header file
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();
}
My class visa application header file
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;
};
and finally the visa application.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
}
I'm getting the error "no matching function for call to `List::appendNode(Node*&)'" among a few other things.Anyway I know this is long but it would be great if i could get some help, I do not have a tutor or teacher or someone like that to help me so any feedback would be much appreciated! Thanks!
**EDIT the error messages are:
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*)
while (!in.eof()){pNode = new Node;I think you are not deletingpNodedata here so your program is leaking memory...Not a solution but its good to know. Edit: same goes forvisapNode->nData = &visa;you set thenDatamember to pointer to a pointer to avisa_application, this is probably not what you want.pNode = NULL;delete pNode; //*free the memoryYou are nulling and then deleting no memory...You should have those other way around. To be honest, you have your program leaking memory all over the place. I know its not point of the question but I think you should go back over poiners and managing dynamically allocated memory. To summarize: nothing in the code (basically) gets deleted.node_data? There is no mention of that class/struct/typedef in your program.