Use a Circular Single-chain table (two template classes: node classes and circular linked lists) to solve the problem of magician licensing.

Source: Internet
Author: User

Problem description: The magician uses 13 black cards in a deck to arrange them in advance and stack them together, with the cards facing down. Said to the audience: "I don't watch a card. I can guess what each card is. I can count it out loud. Do you believe it? On-site demonstration ." The magician turned the card number at the top to 1, turned it over to black peach A, put black peach A on the table, and then sorted the remaining cards from top to bottom, the second number 1, 2, place the first card under these cards, turn the second card over, exactly the peach 2, also put it on the table, the third times 1, 2, 3, place the first two cards under these cards in sequence, and then turn the third chapter card to exactly taotao 3. In this way, all 13 cards are displayed in sequence, which is accurate.

Q: How is the starting order of cards arranged?

 

I have not divided the. h file again. It is simple and put together.

The Code is as follows:

// Cirlistnode class <br/> # include <stdio. h> <br/> template <class T> class cirlistnode <br/>{< br/> T data; <br/> cirlistnode <t> * link; <br/> Public: <br/> cirlistnode (): Link (null) {}< br/> cirlistnode (T value): Link (null), data (value) {}< br/> ~ Cirlistnode () {}< br/> void setlink (cirlistnode <t> * Next); <br/> cirlistnode <t> * getlink (); <br/> T & getdata (); <br/> void setdata (T value); <br/> }; <br/> template <class T> <br/> void cirlistnode <t>: setdata (T value) <br/>{< br/> DATA = value; <br/>}</P> <p> template <class T> <br/> void cirlistnode <t>: setlink (cirlistnode <t> * Next) <br/>{< br/> link = next; <br/>}< br/> template <class T> <br/> cirlistnode <t> * CI Rlistnode <t >:: getlink () <br/>{< br/> return link; <br/>}< br/> template <class T> <br/> T & cirlistnode <t>: getdata () <br/>{< br/> return data; <br/>}</P> <p> // cirlist class <br/> template <class T> class cirlist <br/>{< br/> cirlistnode <t> * Head; <br/> cirlistnode <t> * tail; <br/> cirlistnode <t> * cur; <br/> Public: <br/> cirlist (); <br/> ~ Cirlist () {}; <br/> bool addtail (T value); <br/> void removethis (); <br/> void removeall (); <br/> void setbegin (); <br/> int getcount (); <br/> cirlistnode <t> * getcur (); <br/> bool isempty (); <br/> T getnext (); <br/> cirlistnode <t> * getnextnode (); <br/> void setdata (T value); <br/> }; <br/> // cirlist member function implementation <br/> template <class T> <br/> void cirlist <t >:: setdata (T value) <br/>{< br/> cur-> setdata (value); <br/ >}</P> <p> template <class T> <br/> cirlistnode <t> * cirlist <t>: getnextnode () <br/>{< br/> cur = cur-> getlink (); <br/> return cur; <br/>}</P> <p> template <class T> <br/> cirlist <t>: cirlist () <br/>{< br/> head = tail = new cirlistnode <t>; <br/> cur = NULL; <br/> head-> setlink (head ); <br/>}< br/> template <class T> <br/> bool cirlist <t>: addtail (T value) <br/>{< br/> cirlistnode <t> * Add = new cirlistnode <t> (value); <br /> Tail-> setlink (ADD); <br/> tail = tail-> getlink (); <br/> tail-> setlink (head ); <br/> If (tail! = NULL) <br/>{< br/> return true; <br/>}< br/> else <br/> return false; </P> <p >}< br/> template <class T> <br/> void cirlist <t>: removethis () <br/>{< br/> If (cur = head) <br/>{< br/> cur = cur-> getlink (); <br/>}< br/> cirlistnode <t> * precur = cur; <br/> for (INT I = 0; I <this-> getcount (); I ++) <br/>{< br/> precur = precur-> getlink (); <br/>}< br/> precur-> setlink (cur-> getlink (); <br/> cur = precur-> getlink (); <br/> P Recur = NULL; <br/>}< br/> template <class T> <br/> void cirlist <t >:: removeall () <br/>{< br/> setbegin (); <br/> int length = getcount (); <br/> for (INT I = 0; I <length; I ++) <br/>{< br/> removethis (); <br/>}< br/> cur = head; <br/>}< br/> template <class T> <br/> void cirlist <t>: setbegin () <br/>{< br/> cur = head; <br/>}< br/> template <class T> <br/> int cirlist <t> :: getcount () <br/>{< br/> int num = 0; <br/> cirlistn Ode <t> * Here = cur; <br/> while (cur-> getlink ()! = Here) <br/>{< br/> cur = cur-> getlink (); <br/> ++ num; <br/>}< br/> cur = cur-> getlink (); <br/> return num; <br/>}< br/> template <class T> <br/> cirlistnode <t> * cirlist <t>: getcur () <br/>{< br/> return cur; <br/>}< br/> template <class T> <br/> bool cirlist <t >:: isempty () <br/>{< br/> return head-> getlink () = head; <br/>}< br/> template <class T> <br/> T cirlist <t>: getnext () <br/>{< br/> If (cur = head) <br/> {<Br/> cur = cur-> getlink (); <br/>}< br/> T num = cur-> getdata (); <br/> cur = cur-> getlink (); <br/> return num; <br/>}</P> <p> # include <iostream> <br/> using namespace STD; <br/> int main () <br/>{< br/> cirlist <int> poker; <br/> for (INT I1 = 0; I1 <13; I1 ++) <br/> poker. addtail (0); <br/> poker. setbegin (); <br/> poker. getnextnode (); <br/> for (INT I = 1; I <14; I ++) <br/>{< br/> poker. setdata (I); <br/> for (Int J = 0; j <= I; j ++) <Br/>{< br/> poker. getnextnode (); <br/> If (poker. getcur ()-> getdata ()! = 0) <br/>{< br/> j --; <br/>}< br/> if (I = 13) <br/> break; <br/>}< br/> poker. setbegin (); <br/> poker. getnextnode (); <br/> for (int K = 0; k <13; k ++) <br/>{< br/> cout <poker. getcur ()-> getdata () <"*"; <br/> poker. getnextnode (); <br/>}< br/> cout <Endl; <br/> return 0; </P> <p>}

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.