Solve the classic problem with C ++ chained queue: Partner problem! (With comments)

Source: Internet
Author: User

Partner problems are described as follows:

That is to say, there is a party that needs to dance. Of course, it's a man's partner with a female. It is a male queue, a female queue, and then get out of the queue to match and dance. If the male queue is as many as the female queue, the output match ends perfectly, if the last man or female is left, output the name of the first person in the remaining queue, for example, I, then the output is still waiting! This is what it means!

The Code is as follows: (there is still no distinction between. H files. Everyone understands that there is no meaning in this case)

# Include <stdio. h> <br/> # include <iostream> <br/> # include <assert. h> <br/> template <class T> <br/> class linkqueuenode // node class definition <br/>{< br/> Public: <br/> T data; <br/> linkqueuenode <t> * link; <br/> linkqueuenode (T & Value): Data (value), Link (null) {}< br/> }; <br/> template <class T> <br/> class linkqueue <br/> {<br/> linkqueuenode <t> * front; <br/> linkqueuenode <t> * back; <br/> Public: <br/> linkqueue (): Front (null), back (Null) {}< br/> void enqueue (T & element); <br/> T delqueue (); <br/> T & getfront (); <br/> void makeempty (); <br/> bool isempty (); <br/> }; <br/> // implementation <br/> template <class T> <br/> void linkqueue <t>: enqueue (T & value) <br/>{< br/> linkqueuenode <t> * Add = new linkqueuenode <t> (value); <br/> If (Back = NULL) // Add the first node so that front points to this node <br/>{< br/> front = back = add; <br/>}< br/> else // If the queue already has nodes, you need to change the back pointer <br/>{< br/> back -> Link = add; <br/> back = back-> link; </P> <p >}< br/> template <class T> <br/> T linkqueue <t >:: delqueue () <br/>{< br/> // you must first determine whether the queue is empty. <br/> assert (! Isempty (); <br/> linkqueuenode <t> * Old = front; <br/> T data = old-> data; // retain the original competitor data <br/> front = front-> link; // move the competitor pointer <br/> If (Back = old) <br/> back = NULL; <br/> delete old; <br/> return data; </P> <p >}< br/> template <class T> <br/> T & linkqueue <t>: getfront () <br/>{< br/> assert (! Isempty (); // assertion, this is a good trick <br/> return front-> data; <br/>}< br/> template <class T> <br/> void linkqueue <t>: makeempty () <br/>{< br/> while (! Isempty () <br/>{< br/> This-> delqueue (); <br/>}</P> <p >}< br/> template <class T> <br/> bool linkqueue <t >:: isempty () <br/>{< br/> return front = NULL; <br/>}< br/> # include <string> <br/> using namespace STD; <br/> struct dancer // This struct type will be stored in the queue in a short time <br/>{< br/> string name; <br/> char sex; <br/>}; <br/> int main () <br/>{< br/> cout <"Enter the total number of partners:" <Endl; <br/> int num; <br/> CIN> num; <br/> linkqueue <dancer> mdancer; <br /> Linkqueue <dancer> fdancer; <br/> for (INT I = 0; I <num; I ++) <br/>{< br/> cout <"Enter the dancer's gender (f (female) or M (male) and name:" <Endl; <br/> char sex; <br/> CIN> sex; <br/> string name; <br/> CIN> name; <br/> dancer newdancer; <br/> newdancer. name = Name; <br/> newdancer. sex = sex; <br/> If (sex = 'F') // female <br/> {<br/> fdancer. enqueue (newdancer); <br/>}< br/> If (sex = 'M') // male <br/>{< br/> mdancer. enqueue (newdancer); <br/>}</P> <p >}< Br/> while ((! Mdancer. isempty ())&&(! Fdancer. isempty () // There are both men and women in the queue <br/>{< br/> cout <mdancer. delqueue (). name <"/T <--- pairing --->/t" <fdancer. delqueue (). name <Endl; <br/>}< br/> If (! Mdancer. isempty () // The remaining hot pot <br/>{< br/> cout <mdancer. getfront (). Name <"Mr is still waiting! "<Endl; <br/>}< br/> else if (! Fdancer. isempty () // all the other moles <br/>{< br/> cout <fdancer. getfront (). name <"What are you waiting! "<Endl; <br/>}< br/> else <br/> cout <" perfect end of pairing! "<Endl; <br/> return 0; <br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.