Queue ----- Array Storage Structure and Its Operation Algorithm Implementation ----- partner Problem

Source: Internet
Author: User

[Cpp] // file: queue. h # ifndef _ Queue_H_INCLUDE _ # define N 10002 # include <cstdio> template <class T> class Queue {public: Queue (): frount (1 ), rear (1) {} void push (T t); void pop (); T front (); bool empty (); private: int frount, rear; t q [N] ;}; template <class T> bool Queue <T >:: empty () {if (frount = rear) return true; else return false ;} template <class T> T Queue <T>: front (){ If (! Empty () return Q [frount]; else {printf ("Empty! \ N ") ;}}template <class T> void Queue <T >:: pop () {if (! Empty () frount ++; else printf ("Empty! \ N ");} template <class T> void Queue <T>: push (T t) {Q [rear] = t; rear ++ ;} # endif/_ STACK_H_INCLUDE _ [cpp] // ******â # include <iostream> # include "queue. h "using namespace std; struct node {char name [20]; char sex ;}; int main () {Queue <node> m, w; int n; struct node x; cin> n; while (n --) {cin> x. name; cin> x. sex; if (x. sex = 'W') w. push (x); else if (x. sex = 'M') m. push (x); else;} while (! W. empty ()&&! M. empty () {cout <w. front (). name <"--" <m. front (). name <endl; w. pop (); m. pop ();} if (w. empty () {printf ("There are men still waiting! \ N ");} else if (m. empty () {printf (" There are women still waiting! \ N ");} else printf (" There is no person waiting! \ N "); return 0 ;}

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.