Click here to Skip to main content
16,009,185 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I have to make a project for school. My task is to choose a data structure for the addition of two polynomial expression, with high exponents and lots of null coefficients. I made my program using linked list data structure, but my teacher told my that linked list is not as efficient as the priority queue data structure in this case. Can somebody give me a clue how to implement a priority queue for the addition of two polynomial expression using c++ language? Are there more efficient data structure for my project?

What I have tried:

My program is based on the linked list data structure.
Posted
Updated 6-Jun-16 6:08am

 
Share this answer
 
Are you really sure you need to implement priority queue, not just use available one?

Priority queue is already implemented in a C++ standard library, std::priority_queue:
priority_queue — C++ Reference.

—SA
 
Share this answer
 
Comments
Member 12530538 7-Jun-16 6:18am    
Yes I can use std::priority_queue. This could be my solution. But why using priority queue is more efficient for the addition of two polynomial expression that using linked list?
Sergey Alexandrovich Kryukov 7-Jun-16 9:48am    
You did not ask about it, so I did not think about it. I'm not sure I understand the ultimate goals of your project.
It is CAS, expression parser, interpreter, anything else?
—SA
Member 12530538 7-Jun-16 14:27pm    
Sorry but i don't understand your question. The main goal of my project is to choose between a data structure and implement it for the addition of two polynomial expressions. First, I have choosen linked list data structure but my teacher told my that is not efficient(because the of the usage of too many 'while' and 'if' statements). She recently told me that I should use priority queue for my program, but I can't understand how priority queue could help me to complete my project. Could you give me a clue on how to use priority queue in my project ? Or maybe you know another data structure better...
Sergey Alexandrovich Kryukov 7-Jun-16 14:32pm    
Sorry, no, the purpose of the project could not be the choice between some structure. First of all, the project should be complete and meet certain functionality requirements. And those requirements are formulated to reach some ultimate goals. What goals? The answer to your question may heavily depend on that.

Yes, the explanation of your teacher is basically correct. What's not clear about it? Keep asking your teacher. It will give you a good habit of not doing anything blind-folded. Yes, you already demonstrate right attitude. You really need to understand the purpose of what you are doing. From the other hand, it's not good that you cannot really formulate the purpose and the goals of your project.

The idea is: priority queue already sorts the data in a ways suitable to the way you need to use this data. It is done according to the class Compare you should supply as a generic parameter. Let's say, you group polynomial factors according the the degrees of the variable; in this case, your Compare predicate less should return the result of comparison by degree. And so on... Look at the description of the template class std::priority_queue and you will see.

Does it give you the idea now? Will you accept my answer formally.

—SA
Member 12530538 7-Jun-16 15:22pm    
Thank you for the response. Yes it really help me. Now I understand why I should use a priority queue functionality.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900