简体   繁体   English

如何使用 print() 打印链表

[英]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.我在代码中编写了 print() 并尝试使用它来打印列表中的数据。 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()?尝试打印,但它给了我错误:“undefined reference to `print(dbl_linked_list_t const*)'” 另外,你能检查我的 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;
    }
}   

The definition of print function is missing const. print function 的定义缺少 const。

print is declared as print 被声明为

void print(const dbl_linked_list_t*);

but defined as (missing const)但定义为(缺少常量)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM