2021年1月28日星期四

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;      }  }       
https://stackoverflow.com/questions/65948520/how-to-use-print-to-print-the-linked-list January 29, 2021 at 11:49AM

没有评论:

发表评论