An Phan
An Phan

Reputation: 35

How to use print() to print the linked list

I wrote the print() in my code and try to use it to print out the data in the list. Also the text file below. Thank you for any suggestions. Try to print but it gave me the error :"undefined reference to `print(dbl_linked_list_t const*)'" Also, can you check my read()? I didn't know if I was read in the data into my program right or not

struct fruit_t {
    char fruit_name[256];                       // name of fruit
    float quantity;                         // in lbs
    float price;                            // price tag of the fruit
    float new_quantity;

};
struct node_t{
    node_t* next;
    node_t* prev;   //constructor
    fruit_t f;      //data declarations
    
};
struct dbl_linked_list_t{
    node_t* head;
    node_t* tail;
};

//-----------Function--------------------------------------
node_t* initNode (fruit_t);         // create new node
void createList (dbl_linked_list_t*);   // create a list
void insertNode (dbl_linked_list_t*, node_t*);
void print(const dbl_linked_list_t*);
//---------------------------------------------------------

int main(int argc, char *argv[])
{
                    //prog -inorder|sortall|sortone file.txt
                    //set program mode from command line arg
    ifstream inFile;  
    dbl_linked_list_t list;             //declare linked_list<fruit>
    node_t* nodePtr;
    fruit_t fruit;
    createList(&list);
    inFile.open("list1.txt");
    fruit_t f;
    while (inFile.read ( (char*)&fruit, sizeof(fruit_t))){ //while (reading more data)
        nodePtr = initNode(fruit);
        insertNode (&list, nodePtr);            
    }
    inFile.close();
    //store data INORDER | SORTALL | SORTONE 
    cout << "geting "<< endl;
    print(&list); //RIGHT HERE IS WHEN THE ERROR APPEAR
}
node_t* initNode (fruit_t fruit){       // create new node
    node_t* newNode = (node_t*) malloc (sizeof (node_t) );
    newNode -> f = fruit;
    newNode -> next = NULL;
    newNode -> prev = NULL;
    return newNode;
}
void createList (dbl_linked_list_t* lPtr){  // create a list
    lPtr -> head = NULL;
    lPtr -> tail = NULL;
}
void insertNode (dbl_linked_list_t* lPtr, node_t* nPtr){
    node_t* curPtr;
    if (lPtr != NULL){
        if (lPtr -> head == NULL){
            lPtr -> head = nPtr;
            lPtr -> tail = nPtr;
        }
        curPtr = curPtr -> next;
    }

}
void print(dbl_linked_list_t* lPtr){
    node_t* curPtr;
    curPtr = lPtr -> head;
    while (curPtr != NULL){
        cout << setfill('.') << setw(20) << left << curPtr -> f.fruit_name;
        cout << setfill (' ') << setw (5) << right << curPtr -> f.quantity
                     << setw (9) << right << curPtr -> f.price << endl;
        curPtr = curPtr -> next;
    }
}   

Upvotes: 1

Views: 35

Answers (1)

srki
srki

Reputation: 36

The definition of print function is missing const.

print is declared as

void print(const dbl_linked_list_t*);

but defined as (missing const)

void print(dbl_linked_list_t* lPtr){ ... }

Upvotes: 1

Related Questions