1

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*)   
5
  • while (!in.eof()){pNode = new Node; I think you are not deleting pNode data here so your program is leaking memory...Not a solution but its good to know. Edit: same goes for visa Commented Oct 21, 2013 at 8:58
  • When you do pNode->nData = &visa; you set the nData member to pointer to a pointer to a visa_application, this is probably not what you want. Commented Oct 21, 2013 at 8:59
  • Also, please post the complete and unedited error message log. And point out where in the posted source the errors are. Commented Oct 21, 2013 at 9:00
  • pNode = NULL;delete pNode; //*free the memory You 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. Commented Oct 21, 2013 at 9:04
  • And what on earth is node_data? There is no mention of that class/struct/typedef in your program. Commented Oct 21, 2013 at 9:07

6 Answers 6

1

As far as I see it, your List has only the method:

void List::appendNode(node_data * value)

but in your main, you call the methode with a object of class Node. Try

void List ::appendNode(Node * pNode)
{

instead of:

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

Also, as already suggested in the comments. Write always the whole errocode in your question, including the position. Best practice is to include the compiler and os too.

Sign up to request clarification or add additional context in comments.

Comments

1

According to your own declaration of the function you can't call

myList.appendNode(pNode); //add node to list

with a pointer to Node, you'll need to pass a pointer of type node_data* there.

As others mentioned in comments, there are a lot of other flaws and errors in your code.

Comments

0

You declared and defined appendNode as void appendNode(node_data * value); but you tried to calling it using myList.appendNode(pNode); //add node to list, pNode here is an object of Node type, so you got your error.

Comments

0

Assuming node_data* is some kind of typedef of void* in other words void pointer, you are passing Node as your data instead of your visa_application. To solve this simply change

myList.appendNode(pNode); //add node to list

to

myList.appendNode((node_data*)visa); //add node to list

and you should be fine. You can also get rid of allocation of memory for node in your main function as that is what your List::createNode is doing AFAIK.

OT: the program is full of leaks, read my comments on your question

Comments

0

Doubly Linked List are very majorly used in computer applications . I found a very good and efficient program for Doubly Linked list in a site

Only difference bertween single linkedlist and doubly linked list is the extra pointer which helps to traverse the Linked list from left to right or right->left .

Program for Doubly Linked list C++ : http://www.techfinite.net/2013/11/double-linked-list-program-and-algorithm.html

Comments

0

I try to define the zero parameter Node constructor in the Node class and set pNext = this and pPrev = this. I haven't run your code but that error could be because you are calling a zero parameter Node constructor but you haven't defined one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.