algorithmuscanorj

First draft for the seqNpad project

Dec 14th, 2012
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 83.79 KB | None | 0 0
  1.  
  2. /* (Reviewed Dec, 14 2012)
  3.  *
  4.  * seqNpad - Script interpreter and Notepad for Integer Sequences.
  5.  * Version 3.29 by: R. J. Cano, <aallggoorriitthhmmuuss@gmail.com>
  6.  *  
  7.  * This program was devised and written for documentation purposes and is
  8.  * subject to the terms of use of the On Line Encyclopedia Of Integer Sequences (TM).
  9.  * (Visit: http://www.oeis.org/a217626)
  10.  *
  11.  *      << If people do not believe that mathematics is simple, it is only because
  12.  *          they do not realize how complicated life is. >>
  13.  *
  14.  *                  --John von Neumann
  15.  *
  16.  *
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <time.h>
  23.  
  24. typedef struct numLIFO   { long value;         struct numLIFO*   belowLink;  } numLIFO;
  25.  
  26. typedef struct numBook   { long value;         struct numBook*   prevLink;
  27.                                                struct numBook*   nextLink;   } numBook;
  28.  
  29. typedef struct digitsString { long integer_value; struct digitsString* nextString; } digitsString;
  30.  
  31.  
  32. digitsString *__theRing_HEAD, *__theRing_TAIL;
  33.  
  34. /*
  35.  * (Inherited from A207324 sequencer)
  36.  *
  37.  * Note about the ring: After calling close__the_ring() a first time, don't call it again before a call to
  38.  * open_the_ring() of course for the same parameter list (else it would have possibly harmful and
  39.  * unpredictible consequences).
  40.  *
  41.  */
  42.  
  43. int _BatchInputMode=        1; /*
  44.                                    This should be 0 if this program will be used as an user-ineractive interpreter.
  45.                                 */
  46.                                
  47. int _Which_main=            2; /*
  48.                                -----------------------------------------
  49.                                    #   function call:  In order to run:
  50.                                -----------------------------------------
  51.                                    0   main()          seqNotepad,
  52.                                    1   Old_main_01()   A207324 sequencer,
  53.                                    2   Old_main_02()   A217626 sequwncer.
  54.                                -----------------------------------------
  55.                                Note: If proper intructions are given to
  56.                                      seqNotepad, it replaces the mentioned
  57.                                      sequencer for A217656 and may replace
  58.                                      a sequencer for A215940.
  59.                                 */
  60.                                
  61. int _a217626_fixTextGlitch= 1; /*
  62.                                    Set it to zero if a modification made to runSeqAn()
  63.                                    doesn't have anything to do about a217626.
  64.                                 */
  65.                                
  66. int _ScriptMem01=           0; /* Similar in some way to "ditto" operator in Maple. */
  67.  
  68. int _rec=                   0; /* Internal use only. Don't touch it. */
  69.  
  70. /*
  71.  * The "Demo loop": As its name tell us, a demonstrative script.
  72.  *
  73.  * ( These numbers are internal instructions for being executed in sequence )
  74.  *
  75.  */
  76.  
  77. int _Script[]= {-9, -12, -211, -7, 1, -1011, 9, 1, -3, 1, -2, -911, -666};
  78.  
  79. /*
  80.  * Important:
  81.  *
  82.  *  Due the finiteness of the preceding array, every script must ends in ..."-2, 1}"
  83.  *  for return the control to the OS or well in ..."-2,-911,-666}" in order to close
  84.  *  an execution loop. Else, the interpreter will crash with a segmentation fault caused
  85.  *  by an array index out of bounds.
  86.  *
  87.  *  None explanation about the use of this program as Script interpreter would be better than
  88.  *  a detailed study of the "main menu" and the functionality implemented in "requestUserInput()".
  89.  *
  90.  */
  91.  
  92. char*       _CtrlStr=                       "%li";
  93. char*       seqAnDescriptorString=          "A217626: First differences among the terms in A215940.\nA symmetric set of generators for permutations in the natural numbers";
  94. int         _ScriptCursor=                  0;
  95. int         __savingCallsToMallocMode=      0;
  96.  
  97. /* Should be set to 1 when compiling this source. (Unconditional) */
  98. int         _firstCleanUp=                  1;
  99.  
  100. int         _lastScriptCommand=             0;
  101. int         allowChangeTheSign=             0;
  102.  
  103. int         _Is_SJT_Notepad=                0; /* Experimental feature not used yet. */
  104. int         _QsortFirstRun=                 1; /* Experimental. Only have sense to show "tau factor" the first time Qsort() is run. */
  105.  
  106. /* Use it to generate additional information about the offsets in the "old main" functions. */
  107. int         b_file_mode=                    1;
  108.  
  109. int         notepadHasChanged=              0;
  110. numLIFO*  __lastDecomposition=            NULL;
  111.  
  112. /* An acceptable definition for negative infinity inside an integer (computer) context. */
  113. long        _NegInfty=                     -999999999;
  114.  
  115. long        _kronometer00=                  0;
  116. long        __SizeFor_BendingTheWireDemo=   5;
  117. long        __OnPrintingViewModeCommutator= 0;
  118. long        bigSigma=                       0;
  119. long        entries=                        0;
  120. long        justDeltaMode=                  0;
  121. long        offsetShifting=                 0;
  122. long        old_justDeltaMode=              0;
  123. long        old_offsetShifting=             0;
  124. long        old_valueToDivide=              1;
  125. long        old_valueTosubtract=            0;
  126. long        precedingEntry=                 0;
  127. long        valueToDivide=                  1;
  128. long        valueTosubtract=                0;
  129. numBook*    lastEntry=                      NULL;
  130. numBook*    auxBookTail=                    NULL;
  131. numBook*    swapBookHead=                   NULL;
  132. numBook*    swapBookTail=                   NULL;
  133.  
  134. /*
  135.  *  Begin: Globals inherited from A207324 sequencer.
  136.  */
  137.  
  138. const long __mode_ZERO=              0;
  139. const long __mode_EQUAL=             1;
  140. const long __mode_DETERMINANT=       2;
  141. const long __TOKEN_TEMPLATE=         0;
  142. const long __UNIT=                   1;
  143. long       __config_generator[3][2];
  144. long       generator_settings=       0;
  145.  
  146. /*
  147.  *  End: Globals inherited from A207324 sequencer.
  148.  */
  149.  
  150. int      Old_main_01(void);
  151. int      Old_main_02(void);
  152. int      addressHackingDemo(void);
  153. int      appendNode(numBook**, numBook**, numBook**);
  154. int      applyQsortTo(numBook**);
  155. int      deleteBook(numBook**);
  156. int      deleteNode(numBook**, numBook**);
  157. int      discardLifoStack(numLIFO**);
  158. int      extractNode(numBook**, numBook**);
  159. int      isolated(numBook**);
  160. int      loadNumBookFromDisk(numBook**);
  161. int      main(void);
  162. int      printNumBook(numBook**);
  163. int      saveNumBookToDisk(numBook**);
  164. long     KthDigitOf(long, int, int, int);
  165. long     bookSize(numBook**);
  166. long     factorial(long);
  167. long     greatestPowerOfBaseIn(long,int);
  168. long     howManyDigitsIn(long, int);
  169. long     innerRecursion(long, long);
  170. long     numLIFONumWriter(numLIFO*);
  171. long     refPermutOfFirst_iter(int, int);
  172. long     refPermutOfFirst_recu(int, int);
  173. numBook* Qsort(numBook**);
  174. numBook* createNode(long);
  175. void     triConcat(numBook**,numBook**,numBook**,numBook**);
  176. void     clearBookAndOptions(numBook**);
  177. void     discardNumBook(numBook**);
  178. void     extractFromBook(numBook**);
  179. void     goodBye(void);
  180. void     insertToBook(numBook**, numBook**, numBook**);
  181. void     old_printNumBook(numBook**, long);
  182. void     print_Seq_Notepad_Main_Menu_(void);
  183. void     requestUserInput(long*);
  184. void     retrieveOptions(void);
  185. void     runBendingA217626Demo(numBook**, long);
  186. void     runSeqAn(numBook**, long* n);
  187. void     runRainDance(numBook**, long);
  188. void     run_seqNpad(void);
  189. void     saveOptions(void);
  190. void     startupGreetings(void);
  191. void     slow_Sort(numBook**);
  192. void     transferNode(numBook**, numBook**, numBook**);
  193. void     versusSort(numBook**);
  194.  
  195. /*
  196.  *  Begin: Function declarations inherited from A207324 sequencer.
  197.  */
  198.  
  199. /*---------> non-standards called by Old_main_01(); <-----------------------------------------------------*/
  200. void configure_modes(void);
  201. void run_my_SJT_implementation(int, long);
  202. /*---------> non-standards called by others; <-----------------------------------------------------*/
  203. void finalizer_matrix(digitsString **, digitsString**);
  204. void print_matrix(digitsString **);
  205. void apply_SteinhausJohnsonTrotterAlgorithm(digitsString**, digitsString**, long);
  206. void mode_matrix_waterfall(void);
  207. digitsString* generate_square_matrix(digitsString**, digitsString**, digitsString**, digitsString**, long);
  208. void initializer_matrix(digitsString **, digitsString**);
  209. digitsString* extract_first_nodes(digitsString**, digitsString**, long);
  210. void concat_matrices_node_by_node(digitsString**, digitsString**, digitsString**);
  211. /*---------> additional non-standards (some of them called by more than one function); <-----------------------------------------------------*/
  212. long only_initialized(digitsString**);
  213. long absolute_value(long);
  214. digitsString* insert_new_node(long, digitsString**);
  215. void close__the_ring(digitsString**, digitsString**);
  216. void open_the_ring(digitsString**, digitsString**);
  217. long LeibnitzLaplaceSignature(long);
  218. /*------------------------------------------------------------------------------------------------------------------------------*/
  219.  
  220. /*
  221.  *  End: Function declarations inherited from A207324 sequencer.
  222.  */
  223.  
  224. /*
  225.  * ---------------------------------------------------------------------------------------------------------
  226.  *  Formerly the main() function for the A207324 sequencer.
  227.  * ---------------------------------------------------------------------------------------------------------
  228.  */
  229.  
  230. /*
  231.  *  Begin: Declarations inherited from "deception.c";
  232.  */
  233.  
  234. numBook  nullBookBase;
  235. numBook* nullBook= &nullBookBase;
  236.  
  237. long howmany= 100000;
  238.  
  239. float _lastRecursionScore= -0.33e0;
  240. float _maxXRecursionScore= -0.33e0;
  241.  
  242. int      main(void);
  243. long     insertValue(long, numBook**, numBook**);
  244. long     numBookDiscard(numBook**, numBook**);
  245. int      numBookPrint(int, numBook*, numBook*, int, int);
  246. int  printNodes(numBook*, numBook*);
  247. numBook* concat33(numBook**, numBook**, numBook**, numBook**, numBook**, numBook**, numBook**);
  248. void     moveNode(numBook**, numBook**, numBook**, numBook**, numBook**, long);
  249. void     aBadQuicksort01(numBook**, numBook**, numBook**, numBook**);
  250. void     aBadQuicksort02(numBook**, numBook**, numBook**, numBook**);
  251.  
  252. long insertValue(long newValue, numBook** whereHead, numBook** whereTail) {
  253.  
  254.   long ans= 0;
  255.   numBook* newEntry= NULL;
  256.  
  257.   if ((whereHead != NULL) && (whereTail != NULL)) {
  258.    
  259.     if (*whereHead == NULL) {
  260.      
  261.       (*whereHead)= (numBook*) malloc(sizeof(numBook));
  262.       (*whereHead)->value= newValue;
  263.       (*whereHead)->nextLink= NULL;
  264.       (*whereHead)->prevLink= NULL;
  265.       (*whereTail)= (*whereHead);
  266.       ans++;
  267.    
  268.     } else {
  269.      
  270.       newEntry= (numBook*) malloc(sizeof(numBook));
  271.       newEntry->value=     newValue;
  272.       newEntry->nextLink=  NULL;
  273.       newEntry->prevLink=  (*whereTail);
  274.       (*whereTail)->nextLink= newEntry;
  275.       (*whereTail)= newEntry;
  276.       newEntry= NULL;
  277.       ans++;
  278.      
  279.     }
  280.    
  281.   } else {
  282.     ;
  283.   }
  284.  
  285.   return ans;
  286. }
  287.  
  288. long numBookDiscard(numBook** hheeaadd, numBook** ttaaiill) {
  289.   numBook* leftSlayer=  NULL;
  290.   numBook* rightSlayer= NULL;
  291.   long ans= 0;
  292.   if (hheeaadd != NULL) {
  293.     if (*hheeaadd != NULL) {
  294.       if (ttaaiill != NULL) {
  295.     if (*ttaaiill != NULL) {
  296.       leftSlayer=  *hheeaadd;
  297.       rightSlayer= *ttaaiill;
  298.       while (
  299.               ( ((*hheeaadd)->nextLink) != (*ttaaiill) )
  300.           &&
  301.           ( (*hheeaadd) != (*ttaaiill) )
  302.         )
  303.       {
  304.         leftSlayer=  *hheeaadd;
  305.         rightSlayer= *ttaaiill;
  306.         (*hheeaadd)= (*hheeaadd)->nextLink;
  307.         (*ttaaiill)= (*ttaaiill)->prevLink;
  308.         (*hheeaadd)->prevLink= NULL;
  309.         (*ttaaiill)->nextLink= NULL;
  310.         free(leftSlayer);
  311.         free(rightSlayer);
  312.         ans+= 2;
  313.        
  314.       }
  315.       leftSlayer=  NULL;
  316.       rightSlayer= NULL;
  317.       if ( (*hheeaadd) == (*ttaaiill) ) {
  318.         *ttaaiill= NULL;
  319.         free(*hheeaadd);
  320.         *hheeaadd= NULL;
  321.         ans+= 1;
  322.       } else {
  323.         (*hheeaadd)->nextLink=  NULL;
  324.         (*ttaaiill)->prevLink= NULL;
  325.         free((*hheeaadd));
  326.         free((*ttaaiill));
  327.         (*hheeaadd)=  NULL;
  328.         (*ttaaiill)= NULL;
  329.         ans+= 2;  
  330.       }
  331.     }
  332.       }
  333.     }
  334.   }
  335.   return ans;
  336. }
  337.  
  338. int printNodes(numBook* ref1, numBook* ref2) {
  339.     int ans= 0;
  340.     printf(" (");
  341.     if (ref1 != NULL) {
  342.       if (ref1->prevLink != NULL) {
  343.     printf("<");
  344.       } else {
  345.     printf("!");
  346.       }
  347.       printf("%li", ref1->value);
  348.       if (ref1->nextLink != NULL) {
  349.     printf(">");
  350.       } else {
  351.     printf("!");
  352.       }
  353.     } else {
  354.       printf("void");
  355.     }
  356.     printf(",");
  357.     if (ref2 != NULL) {
  358.       if (ref2->prevLink != NULL) {
  359.     printf("<");
  360.       } else {
  361.     printf("!");
  362.       }
  363.       printf("%li", ref2->value);
  364.       if (ref2->nextLink != NULL) {
  365.     printf(">");
  366.       } else {
  367.     printf("!");
  368.       }
  369.     } else {
  370.       printf("void");
  371.     }
  372.     printf(")");
  373.     return ans;
  374. }
  375.  
  376. int numBookPrint(int orientation, numBook* aBookHead, numBook* aBookTail, int columns, int tabDash) {
  377.  
  378.   int ans=     0;
  379.   int rowFill= 0;
  380.   numBook* cursor= NULL;
  381.  
  382.   if (orientation) {
  383.    
  384.     cursor= aBookHead;
  385.    
  386.     if (cursor != NULL) {
  387.    
  388.       printf("\n\n");
  389.    
  390.       while (cursor != NULL) {
  391.      
  392.     printf("%c%11li%c", ( (tabDash) ? '\t': ' ' ), cursor->value, ( (++rowFill%columns==0) ? '\n' : ' ' ) );
  393.      
  394.     cursor= cursor->nextLink;
  395.       }
  396.      
  397.       printf("\n%li Nodes.", bookSize(&aBookHead));
  398.      
  399.     }
  400.     else {
  401.    
  402.       printf("\n\n\t(Empty)");
  403.    
  404.     }
  405.    
  406.   } else {
  407.    
  408.     cursor= aBookTail;
  409.    
  410.     if (cursor != NULL) {
  411.    
  412.       printf("\n\n");
  413.    
  414.       while (cursor != NULL) {
  415.      
  416.     printf("%c%11li%c", ( (tabDash) ? '\t': ' ' ), cursor->value, ( (++rowFill%columns==0) ? '\n' : ' ' ) );
  417.      
  418.     cursor= cursor->prevLink;
  419.       }
  420.      
  421.       printf("\n%li Nodes.", bookSize(&aBookHead));
  422.      
  423.     }
  424.     else {
  425.    
  426.       printf("\n\n\t(Empty)");
  427.    
  428.     }    
  429.    
  430.   }
  431.   return  ans;
  432. }
  433.  
  434. numBook* concat33(numBook** copyOperand00Tail,
  435.               numBook** copyOperand01Head,
  436.               numBook** copyOperand01Tail,
  437.               numBook** copyOperand02Head,
  438.               numBook** copyOperand02Tail,
  439.               numBook** copyOperand03Head,
  440.               numBook** copyOperand03Tail) {
  441.  
  442.   numBook* operand00Head= NULL;
  443.   numBook* operand00Tail= (copyOperand00Tail != NULL) ? *copyOperand00Tail : NULL;
  444.   numBook* operand01Head= (copyOperand01Head != NULL) ? *copyOperand01Head : NULL;
  445.   numBook* operand01Tail= (copyOperand01Tail != NULL) ? *copyOperand01Tail : NULL;
  446.   numBook* operand02Head= (copyOperand02Head != NULL) ? *copyOperand02Head : NULL;
  447.   numBook* operand02Tail= (copyOperand02Tail != NULL) ? *copyOperand02Tail : NULL;
  448.   numBook* operand03Head= (copyOperand03Head != NULL) ? *copyOperand03Head : NULL;
  449.   numBook* operand03Tail= (copyOperand03Tail != NULL) ? *copyOperand03Tail : NULL;
  450.  
  451.   if (copyOperand01Head != NULL) { *copyOperand01Head= NULL; }
  452.   if (copyOperand01Tail != NULL) { *copyOperand01Tail= NULL; }
  453.   if (copyOperand02Head != NULL) { *copyOperand02Head= NULL; }
  454.   if (copyOperand02Tail != NULL) { *copyOperand02Tail= NULL; }
  455.   if (copyOperand03Head != NULL) { *copyOperand03Head= NULL; }
  456.   if (copyOperand03Tail != NULL) { *copyOperand03Tail= NULL; }
  457.  
  458.   if (operand00Tail != NULL) {
  459.    
  460.     if ( (operand01Head != NULL) || (operand02Head != NULL) || (operand03Head != NULL) ) {
  461.  
  462.       if ( (operand01Head != NULL) && (operand02Head != NULL) && (operand03Head != NULL) ) {
  463.    
  464.     if ( (operand01Tail != NULL) && (operand02Tail != NULL) && (operand03Tail != NULL) ) {
  465.  
  466.       operand00Head= operand01Head;
  467.       operand01Tail->nextLink= operand02Head;
  468.       operand02Head->prevLink= operand01Tail;
  469.       operand02Tail->nextLink= operand03Head;
  470.       operand03Head->prevLink= operand02Tail;
  471.       operand00Tail= operand03Tail;
  472.      
  473.     }
  474.    
  475.       }      
  476.      
  477.       if ( (operand01Head != NULL) && (operand02Head == NULL) && (operand03Head == NULL) ) {
  478.    
  479.     if (operand01Tail != NULL) {
  480.      
  481.       operand00Head= operand01Head;
  482.       operand00Tail= operand01Tail;
  483.  
  484.     }
  485.    
  486.       }
  487.      
  488.       if ( (operand01Head == NULL) && (operand02Head != NULL) && (operand03Head == NULL) ) {
  489.  
  490.     if (operand02Tail != NULL) {
  491.      
  492.       operand00Head= operand02Head;
  493.       operand00Tail= operand02Tail;
  494.  
  495.     }
  496.    
  497.       }
  498.      
  499.       if ( (operand01Head == NULL) && (operand02Head == NULL) && (operand03Head != NULL) ) {
  500.    
  501.     if (operand03Tail != NULL) {
  502.      
  503.       operand00Head= operand03Head;
  504.       operand00Tail= operand03Tail;
  505.      
  506.     }
  507.    
  508.       }
  509.      
  510.       if ( (operand01Head != NULL) && (operand02Head != NULL) && (operand03Head == NULL) ) {
  511.    
  512.     if ( (operand01Tail != NULL) && (operand02Tail != NULL) ) {
  513.      
  514.       operand00Head= operand01Head;
  515.       operand01Tail->nextLink= operand02Head;
  516.       operand02Head->prevLink= operand01Tail;
  517.       operand00Tail= operand02Tail;
  518.  
  519.     }
  520.    
  521.       }
  522.  
  523.       if ( (operand01Head == NULL) && (operand02Head != NULL) && (operand03Head != NULL) ) {
  524.    
  525.     if ( (operand02Tail != NULL) && (operand03Tail != NULL) ) {
  526.      
  527.       operand00Head= operand02Head;
  528.       operand02Tail->nextLink= operand03Head;
  529.       operand03Head->prevLink= operand02Tail;
  530.       operand00Tail= operand03Tail;
  531.  
  532.     }
  533.    
  534.       }
  535.      
  536.       if ( (operand01Head != NULL) && (operand02Head == NULL) && (operand03Head != NULL) ) {
  537.    
  538.     if ( (operand01Tail != NULL) && (operand03Tail != NULL) ) {
  539.      
  540.       operand00Head= operand01Head;
  541.       operand01Tail->nextLink= operand03Head;
  542.       operand03Head->prevLink= operand01Tail;
  543.       operand00Tail= operand03Tail;
  544.  
  545.     }
  546.    
  547.       }
  548.    
  549.       (*copyOperand00Tail)= operand00Tail;
  550.    
  551.       operand01Head= NULL;
  552.       operand01Tail= NULL;
  553.       operand02Head= NULL;
  554.       operand02Tail= NULL;
  555.       operand03Head= NULL;
  556.       operand03Tail= NULL;
  557.            
  558.     }
  559.    
  560.   }
  561.  
  562.   return operand00Head;
  563. }
  564.  
  565. void moveNode(numBook** whichNode, numBook** fromHead, numBook** fromTail, numBook** toHead, numBook** toTail, long copy_whatPos) {
  566.  
  567.   numBook* tmp01=   NULL;
  568.   numBook* tmp02=   NULL;
  569.   numBook* tmp03=   NULL;
  570.   numBook* tmp04=   NULL;
  571.   long     bound=   0;
  572.   long     counter= 0;
  573.   long     whatPos= copy_whatPos;
  574.  
  575.   if (whichNode != NULL) {
  576.     if (*whichNode != NULL) {
  577.       if (fromHead != NULL) {
  578.     if (*fromHead != NULL) {
  579.       if (fromTail != NULL) {
  580.         if (*fromTail != NULL) {
  581.           if ( ((*whichNode) == (*fromHead)) && ((*whichNode) == (*fromTail)) ) {
  582.         (*fromHead)= NULL;
  583.         (*fromTail)= NULL;
  584.           } else if ( ((*whichNode) == (*fromHead)) && ((*whichNode) != (*fromTail)) ) {
  585.         tmp01= (*fromHead)->nextLink;
  586.         tmp01->prevLink= NULL;
  587.         (*whichNode)->nextLink= NULL;
  588.         (*fromHead)= tmp01;
  589.           } else if ( ((*whichNode) != (*fromHead)) && ((*whichNode) == (*fromTail)) ) {
  590.         tmp02= (*fromTail)->prevLink;
  591.         tmp02->nextLink= NULL;
  592.         (*whichNode)->prevLink= NULL;
  593.         (*fromTail)= tmp02;
  594.           } else if ( ((*whichNode) != (*fromHead)) && ((*whichNode) != (*fromTail)) ) {
  595.         tmp01= (*whichNode)->prevLink;
  596.         tmp02= (*whichNode)->nextLink;
  597.         (*whichNode)->nextLink= NULL;
  598.         (*whichNode)->prevLink= NULL;
  599.         tmp01->nextLink= tmp02;
  600.         tmp02->prevLink= tmp01;
  601.         tmp01= NULL;
  602.         tmp02= NULL;
  603.           }
  604.           if (toHead != NULL) {
  605.         if (toTail != NULL) {
  606.           if ( ((*toHead) == NULL) && ((*toTail) == NULL) ) {
  607.             (*toHead)= (*whichNode);
  608.             (*toTail)= (*whichNode);
  609.           }
  610.           else {
  611.             bound= bookSize(toHead);
  612.             if (whatPos == bound) {
  613.               whatPos= 0;
  614.             }
  615.             switch (whatPos) {
  616.               case -1:
  617.             if (bound >= 1) {
  618.               (*whichNode)->nextLink= (*toHead);
  619.               (*toHead)->prevLink= (*whichNode);
  620.               (*toHead)= (*whichNode);
  621.             }
  622.             break;
  623.               case  0:
  624.             if (bound >= 1) {
  625.               (*toTail)->nextLink= (*whichNode);
  626.               (*whichNode)->prevLink= (*toTail);
  627.               (*toTail)= (*whichNode);
  628.             }
  629.             break;
  630.               default:
  631.             if ((1<=whatPos) && (whatPos<bound)) {
  632.               if ( (bound-whatPos) >= whatPos ) {
  633.                 tmp03= (*toHead);
  634.                 counter= 1;
  635.                 while (counter != whatPos) {
  636.                   counter++;  
  637.                   tmp03= tmp03->nextLink;
  638.                 }
  639.               }
  640.               else {
  641.                 tmp03= (*toTail)->prevLink;
  642.                 counter= (bound-1);
  643.                 while (counter != whatPos) {
  644.                   counter--;  
  645.                   tmp03= tmp03->prevLink;
  646.                 }
  647.               }
  648.               tmp04= tmp03->nextLink;
  649.               tmp04->prevLink= (*whichNode);
  650.               (*whichNode)->nextLink= tmp04;
  651.               (*whichNode)->prevLink= tmp03;
  652.               tmp03->nextLink= (*whichNode);
  653.               tmp03= NULL;
  654.               tmp04= NULL;
  655.             }
  656.             break;
  657.             }
  658.           }
  659.         }
  660.           }
  661.         }
  662.       }
  663.     }
  664.       }
  665.     }
  666.   }
  667. }
  668.  
  669.  
  670. void aBadQuicksort01(numBook** solutionHead, numBook** solutionTail, numBook** problemHead, numBook** problemTail) {
  671.  
  672.   numBook* leftCursor=        NULL;
  673.   numBook* rightCursor=       NULL;
  674.   numBook* equaArrayHead=     NULL;
  675.   numBook* equaArrayTail=     NULL;
  676.   numBook* lessArrayHead=     NULL;
  677.   numBook* lessArrayTail=     NULL;
  678.   numBook* greaArrayHead=     NULL;
  679.   numBook* greaArrayTail=     NULL;
  680.   numBook* lessArrayHeadSort= NULL;
  681.   numBook* lessArrayTailSort= NULL;
  682.   numBook* greaArrayHeadSort= NULL;
  683.   numBook* greaArrayTailSort= NULL;
  684.   numBook* pivotNode=         NULL;
  685.   long     counter=           1;  
  686.   long     bound=             bookSize(problemHead);
  687.  
  688.   if (bound <= 1) {
  689.    
  690.   if (problemHead != NULL) {
  691.     if (*problemHead != NULL) {
  692.       if (problemTail != NULL) {
  693.     if (*problemTail != NULL) {
  694.       if (solutionHead != NULL) {
  695.         if (solutionTail != NULL) {
  696.          
  697.           (*solutionHead)= (*problemHead);
  698.           (*solutionTail)= (*problemTail);
  699.           (*problemHead)=  NULL;
  700.           (*problemTail)=  NULL;
  701.          
  702.         }
  703.       }
  704.     }
  705.       }
  706.     }
  707.   }
  708.    
  709.    
  710.   } else {
  711.    
  712.   if (problemHead != NULL) {
  713.     if (*problemHead != NULL) {
  714.       if (problemTail != NULL) {
  715.     if (*problemTail != NULL) {
  716.       if (solutionHead != NULL) {
  717.         if (solutionTail != NULL) {
  718.          
  719.       pivotNode= *problemHead;
  720.       bound= ( (bound/2) + (bound%2) );
  721.       while (counter < bound) {
  722.         pivotNode= pivotNode->nextLink;
  723.         counter++;
  724.       }
  725.      
  726.       moveNode(&pivotNode,  problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
  727.      
  728.       leftCursor=  *problemHead;
  729.       rightCursor= *problemTail;
  730.       while (
  731.               ( ((*problemHead)->nextLink) != (*problemTail) )
  732.           &&
  733.           ( (*problemHead) != (*problemTail) )
  734.         )
  735.       {
  736.         leftCursor=  *problemHead;
  737.         rightCursor= *problemTail;
  738.        
  739.  
  740.         if ( (leftCursor->value) == (pivotNode->value) ) {
  741.           moveNode(&leftCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
  742.         } else if ( (leftCursor->value) < (pivotNode->value) ) {
  743.           moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  744.         } else { /* > */
  745.           moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  746.         }
  747.        
  748.  
  749.         if ( (rightCursor->value) == (pivotNode->value) ) {
  750.           moveNode(&rightCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
  751.         } else if ( (rightCursor->value) < (pivotNode->value) ) {
  752.           moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  753.         } else { /* > */
  754.           moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  755.         }
  756.        
  757.       }
  758.       leftCursor=  NULL;
  759.       rightCursor= NULL;
  760.       if ( (*problemHead) == (*problemTail) ) {
  761.         leftCursor=  *problemHead;
  762.        
  763.         if ( (leftCursor->value) == (pivotNode->value) ) {
  764.           moveNode(&leftCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
  765.         } else if ( (leftCursor->value) < (pivotNode->value) ) {
  766.           moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  767.         } else { /* > */
  768.           moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  769.         }
  770.        
  771.         *problemHead= NULL;
  772.       } else {
  773.         leftCursor=  *problemHead;
  774.         rightCursor= *problemTail;
  775.        
  776.         if ( (leftCursor->value) == (pivotNode->value) ) {
  777.           moveNode(&leftCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
  778.         } else if ( (leftCursor->value) < (pivotNode->value) ) {
  779.           moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  780.         } else { /* > */
  781.           moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  782.         }
  783.        
  784.  
  785.         if ( (rightCursor->value) == (pivotNode->value) ) {
  786.           moveNode(&rightCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
  787.         } else if ( (rightCursor->value) < (pivotNode->value) ) {
  788.           moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  789.         } else { /* > */
  790.           moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  791.         }
  792.        
  793.         (*problemHead)=  NULL;
  794.         (*problemTail)= NULL;
  795.       }
  796.      
  797.       *solutionHead= nullBook;
  798.       *solutionTail= nullBook;
  799.       pivotNode=    NULL;
  800.       leftCursor=   NULL;
  801.       rightCursor=  NULL;
  802.      
  803.       aBadQuicksort01(&lessArrayHeadSort, &lessArrayTailSort, &lessArrayHead, &lessArrayTail);
  804.       aBadQuicksort01(&greaArrayHeadSort, &greaArrayTailSort, &greaArrayHead, &greaArrayTail);
  805.      
  806.       (*solutionHead)= concat33(solutionTail, &lessArrayHeadSort, &lessArrayTailSort, &equaArrayHead, &equaArrayTail, &greaArrayHeadSort, &greaArrayTailSort);
  807.      
  808.           }
  809.         }
  810.       }
  811.     }
  812.       }
  813.     }
  814.   }
  815. }
  816.  
  817. /*
  818.  *   End: Declarations inherited from "deception.c";
  819.  */
  820.  
  821. void aBadQuicksort02(numBook** solutionHead, numBook** solutionTail, numBook** problemHead, numBook** problemTail) {
  822.  
  823.   numBook* auxCursor=         NULL;
  824.   numBook* leftCursor=        NULL;
  825.   numBook* rightCursor=       NULL;
  826.   numBook* lessArrayHead=     NULL;
  827.   numBook* lessArrayTail=     NULL;
  828.   numBook* greaArrayHead=     NULL;
  829.   numBook* greaArrayTail=     NULL;
  830.   numBook* lessArrayHeadSort= NULL;
  831.   numBook* lessArrayTailSort= NULL;
  832.   numBook* greaArrayHeadSort= NULL;
  833.   numBook* greaArrayTailSort= NULL;
  834.   numBook* pivotHead=         NULL;
  835.   numBook* pivotTail=         NULL;
  836.   long     counter=           1;  
  837.   long     bound=             bookSize(problemHead);
  838.  
  839.   if (bound <= 1) {
  840.    
  841.   if (problemHead != NULL) {
  842.     if (*problemHead != NULL) {
  843.       if (problemTail != NULL) {
  844.     if (*problemTail != NULL) {
  845.       if (solutionHead != NULL) {
  846.         if (solutionTail != NULL) {
  847.          
  848.           (*solutionHead)= (*problemHead);
  849.           (*solutionTail)= (*problemTail);
  850.           (*problemHead)=  NULL;
  851.           (*problemTail)=  NULL;
  852.          
  853.         }
  854.       }
  855.     }
  856.       }
  857.     }
  858.   }
  859.        
  860.   } else {
  861.    
  862.   if (problemHead != NULL) {
  863.     if (*problemHead != NULL) {
  864.       if (problemTail != NULL) {
  865.     if (*problemTail != NULL) {
  866.       if (solutionHead != NULL) {
  867.         if (solutionTail != NULL) {
  868.          
  869.       auxCursor= *problemHead;
  870.       bound= ( (bound/2) + (bound%2) );
  871.       while (counter < bound) {
  872.         auxCursor= auxCursor->nextLink;
  873.         counter++;
  874.       }
  875.  
  876.       moveNode(&auxCursor, problemHead, problemTail, &pivotHead, &pivotTail, 0);
  877.      
  878.       leftCursor=  *problemHead;
  879.       rightCursor= *problemTail;
  880.       while (
  881.               ( ((*problemHead)->nextLink) != (*problemTail) )
  882.           &&
  883.           ( (*problemHead) != (*problemTail) )
  884.         )
  885.       {
  886.         leftCursor=  *problemHead;
  887.         rightCursor= *problemTail;
  888.        
  889.         if ( (leftCursor->value) <= (pivotHead->value) ) {
  890.           moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  891.         } else { /* > */
  892.           moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  893.         }
  894.  
  895.         if ( (rightCursor->value) <= (pivotHead->value) ) {
  896.           moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  897.         } else { /* > */
  898.           moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  899.         }
  900.        
  901.       }
  902.       leftCursor=  NULL;
  903.       rightCursor= NULL;
  904.       if ( (*problemHead) == (*problemTail) ) {
  905.         leftCursor=  *problemHead;
  906.        
  907.         if ( (leftCursor->value) <= (pivotHead->value) ) {
  908.           moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  909.         } else { /* > */
  910.           moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  911.         }
  912.        
  913.         *problemHead= NULL;
  914.        
  915.       } else {
  916.         leftCursor=  *problemHead;
  917.         rightCursor= *problemTail;
  918.        
  919.  
  920.         if ( (leftCursor->value) <= (pivotHead->value) ) {
  921.           moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  922.         } else { /* > */
  923.           moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  924.         }
  925.        
  926.  
  927.         if ( (rightCursor->value) <= (pivotHead->value) ) {
  928.           moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
  929.         } else { /* > */
  930.           moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
  931.         }
  932.        
  933.         (*problemHead)=  NULL;
  934.         (*problemTail)= NULL;
  935.       }
  936.      
  937.       *solutionHead= nullBook;
  938.       *solutionTail= nullBook;
  939.       leftCursor=   NULL;
  940.       rightCursor=  NULL;
  941.      
  942.       aBadQuicksort02(&lessArrayHeadSort, &lessArrayTailSort, &lessArrayHead, &lessArrayTail);
  943.       aBadQuicksort02(&greaArrayHeadSort, &greaArrayTailSort, &greaArrayHead, &greaArrayTail);
  944.      
  945.       (*solutionHead)= concat33(solutionTail, &lessArrayHeadSort, &lessArrayTailSort, &pivotHead, &pivotTail, &greaArrayHeadSort, &greaArrayTailSort);
  946.      
  947.           }
  948.         }
  949.       }
  950.     }
  951.       }
  952.     }
  953.   }
  954. }
  955.  
  956. /*
  957.  *
  958.  */
  959. int Old_main_01(void) {
  960.  
  961.   int j, _OScurrentExecutionFeedbackCode = EXIT_SUCCESS;
  962.   long N;
  963.  
  964.   configure_modes(); /* Don't change this, it is for initialization. */
  965.  
  966.   printf("%s%s","\n\nThis is the oeis.org/A207324 sequencer program. Welcome!", "\n\n\tShould be run the sequencer only for a particular SJT-array? (1=yes, 0=No) ");  
  967.   scanf("%i", &j);
  968.   getchar();  
  969.   printf("%s","\n\n\tWhich is the upper size? (try first 1-10 on 32-bit machines): ");
  970.   scanf("%li", &N);
  971.   getchar();
  972.  
  973.   run_my_SJT_implementation(j, N);
  974.  
  975.   return _OScurrentExecutionFeedbackCode;
  976.  
  977. }
  978.  
  979. /*
  980.  * ---------------------------------------------------------------------------------------------------------
  981.  *  Formerly the main() function for the A217626 sequencer.
  982.  * ---------------------------------------------------------------------------------------------------------
  983.  */
  984.  
  985. int Old_main_02(void) {
  986.  
  987.   /* In the further it should be greater than the unit. */
  988.   int upperBound= 1;
  989.  
  990.   numBook* beta=  NULL;
  991.  
  992.   printf("\n\nThis is the oeis.org/A217626 sequencer program. Welcome!");
  993.  
  994.   do
  995.   {
  996.     printf("\nPlease enter N in order to generate the first (N!-1) terms of this sequence: ");
  997.     scanf("%i",&upperBound);
  998.   }
  999.   while ( (0 >= upperBound) || (upperBound > 9) );
  1000.  
  1001.   printf("\nPlease wait... depending on the input you made this may take long.\n");
  1002.  
  1003.   runRainDance(&beta, upperBound);
  1004.  
  1005.   versusSort(&beta);
  1006.   old_printNumBook(&beta, upperBound);
  1007.   discardNumBook(&beta);  
  1008.  
  1009.   return EXIT_SUCCESS;
  1010. }
  1011.  
  1012. int addressHackingDemo(void) {
  1013.  
  1014.   /*
  1015.    *    This should print something like: "44, non-NULL, 134513984, nU"
  1016.    *    (Where 134513984 is a memory address)
  1017.    */
  1018.  
  1019.   long array[3]= {0, 0, 0};
  1020.   char* dummy= "non-NULL";
  1021.   array[1]= (long) dummy;
  1022.   printf("\n%i%i, %s, %li, %c%c \n", sizeof(void*), sizeof(long), (char*)array[1], array[1], *((char*)(array[1]+2)), *((char*)(array[1]+5)) );
  1023.   return EXIT_SUCCESS;
  1024.  
  1025. }
  1026.  
  1027. int appendNode(numBook** WhereHead, numBook** WhereTail, numBook** addThis) {
  1028.  
  1029.   numBook* auxTail= NULL;
  1030.   int ans= ( isolated(addThis) && (WhereHead != NULL) );
  1031.  
  1032.   if (ans) {
  1033.  
  1034.     if (*WhereHead != NULL) {
  1035.    
  1036.       if (WhereTail != NULL) {
  1037.     auxTail= *WhereTail;
  1038.       } else {
  1039.     auxTail= *WhereHead;
  1040.       }
  1041.       while (auxTail->nextLink != NULL) { auxTail= auxTail->nextLink; }
  1042.       auxTail->nextLink= *addThis;
  1043.       (*addThis)->prevLink= auxTail;
  1044.      
  1045.     } else {
  1046.      
  1047.       *WhereHead= *addThis;
  1048.       if (WhereTail != NULL) {
  1049.     *WhereTail= *WhereHead;
  1050.       }
  1051.      
  1052.     }
  1053.    
  1054.     *addThis= NULL;
  1055.   }
  1056.  
  1057.   return ans;
  1058. }
  1059.  
  1060. int applyQsortTo(numBook** TheBook) {
  1061.  
  1062.   numBook* temp= NULL;
  1063.  
  1064.   int ans= (TheBook != NULL);
  1065.  
  1066.   if (ans) {
  1067.  
  1068.     temp= *TheBook;
  1069.     *TheBook= NULL;
  1070.     *TheBook= Qsort(&temp);
  1071.    
  1072.   }
  1073.  
  1074.   return ans;
  1075. }
  1076.  
  1077. int deleteBook(numBook** theBook) {
  1078.   int ans= (theBook != NULL);
  1079.   numBook* cursor= NULL;
  1080.   if (ans) {
  1081.     while (*theBook != NULL) {
  1082.       cursor= *theBook;
  1083.       *theBook= (*theBook)->nextLink;
  1084.       deleteNode(&cursor, theBook);
  1085.     }
  1086.   }
  1087.   return ans;
  1088. }
  1089.  
  1090. int deleteNode(numBook** nodE, numBook** origin) {
  1091.     int ans= extractNode(nodE, origin);
  1092.     if (ans) {
  1093.     free(*nodE);
  1094.     }
  1095.     return ans;
  1096. }
  1097.  
  1098. int discardLifoStack(numLIFO** lifoDecomposition) {
  1099.   int ans= (lifoDecomposition==NULL) ? -1 : 0;
  1100.   numLIFO* focus;
  1101.   if (ans != -1) while(*lifoDecomposition != NULL) {
  1102.     focus= *lifoDecomposition;
  1103.     *lifoDecomposition= (*lifoDecomposition)->belowLink;
  1104.     free(focus);
  1105.     ans++;
  1106.   }
  1107.   return ans;
  1108. }
  1109.  
  1110. int extractNode(numBook** nodE, numBook** origin) {
  1111.  
  1112.     int ans= (nodE != NULL);
  1113.    
  1114.     numBook* prevEnd= NULL;
  1115.     numBook* nextEnd= NULL;
  1116.    
  1117.     if (ans) {
  1118.       ans= 0;
  1119.      
  1120.       if ((*nodE)->prevLink != NULL) { prevEnd= (*nodE)->prevLink; }
  1121.       if ((*nodE)->nextLink != NULL) { nextEnd= (*nodE)->nextLink; }
  1122.      
  1123.       if ((prevEnd == NULL) && (nextEnd == NULL)) {
  1124.     ans= 1;
  1125.       }
  1126.      
  1127.       if ((prevEnd != NULL) && (nextEnd != NULL)) {
  1128.     (*nodE)->prevLink= NULL;
  1129.     (*nodE)->nextLink= NULL;
  1130.     prevEnd->nextLink= nextEnd;
  1131.     nextEnd->prevLink= prevEnd;
  1132.     ans= 1;
  1133.       }
  1134.      
  1135.       if ((prevEnd == NULL) && (nextEnd != NULL)) {
  1136.     (*nodE)->prevLink= NULL;
  1137.     (*nodE)->nextLink= NULL;
  1138.     nextEnd->prevLink= NULL;
  1139.     *origin= nextEnd;
  1140.     ans= 1;
  1141.       }
  1142.      
  1143.       if ((prevEnd != NULL) && (nextEnd == NULL)) {
  1144.     (*nodE)->prevLink= NULL;
  1145.     (*nodE)->nextLink= NULL;
  1146.     prevEnd->nextLink= NULL;
  1147.     ans= 1;
  1148.       }
  1149.      
  1150.     }
  1151.    
  1152.     return ans;
  1153. }
  1154.  
  1155. int isolated(numBook** Subject) {
  1156.  
  1157.   return ( ((*Subject)->prevLink==NULL) && ((*Subject)->nextLink==NULL) );
  1158.  
  1159. }
  1160.  
  1161. int loadNumBookFromDisk(numBook** theBook) {
  1162.  
  1163.   int ans= ( (theBook != NULL) ? 1 : 0 );
  1164.  
  1165.   if (ans) {
  1166.     /*
  1167.      * WhatToDo:
  1168.      * The code placed here should load a numBook previously saved in another
  1169.      * execution. Or better, allow the user select a file from a File dialog
  1170.      * box. may be it would be implemented on X for Unix/Linux easily inclusive
  1171.      * via external plugin-like components for example KDE's kdialog (it would
  1172.      * be as nice as it sounds). More time is required for make experimentation
  1173.      * on it.
  1174.      *
  1175.      */
  1176.   }
  1177.  
  1178.   return ans;
  1179. }
  1180.  
  1181. int main(void) {
  1182.  
  1183.   int (*runOld)(void);
  1184.   int ans;
  1185.  
  1186.   long portal[]= {(long)NULL, (long)&Old_main_01, (long)&Old_main_02};
  1187.   runOld= (int (*)(void)) portal[_Which_main];
  1188.  
  1189.   if (runOld == NULL) {
  1190.    
  1191.     ans= EXIT_SUCCESS;
  1192.     startupGreetings();
  1193.     run_seqNpad();
  1194.     goodBye();
  1195.    
  1196.   } else {
  1197.    
  1198.     ans= runOld();
  1199.    
  1200.   }
  1201.  
  1202.   return ans;
  1203. }
  1204.  
  1205. int printNumBook(numBook** TheBook) {
  1206.  
  1207.   int ans= ((TheBook != NULL) && (valueToDivide != 0));
  1208.   numBook* cursor= NULL;
  1209.   long lineIndex= 1;
  1210.  
  1211.   if (ans) {
  1212.     if (justDeltaMode) {
  1213.       cursor= *TheBook;
  1214.       precedingEntry= valueTosubtract;
  1215.       printf("\n-----------------------------------------------------");
  1216.       while(cursor != NULL) {
  1217.     printf("\n%10li\t%10li", (lineIndex + offsetShifting), ( (cursor->value - precedingEntry) / valueToDivide) );
  1218.     precedingEntry= (cursor->value);
  1219.     cursor= cursor->nextLink;
  1220.     lineIndex++;
  1221.       }
  1222.       printf("\n-----------------------------------------------------\n");      
  1223.     } else {
  1224.       cursor= *TheBook;
  1225.       printf("\n-----------------------------------------------------");
  1226.       while(cursor != NULL) {
  1227.     printf("\n%10li\t%10li", (lineIndex + offsetShifting), ( (cursor->value - valueTosubtract) / valueToDivide) );
  1228.     cursor= cursor->nextLink;
  1229.     lineIndex++;
  1230.       }
  1231.       printf("\n-----------------------------------------------------\n");
  1232.     }
  1233.   }
  1234.  
  1235.   return ans;
  1236. }
  1237.  
  1238. int saveNumBookToDisk(numBook** TheBook) {
  1239.  
  1240.   int ans= (TheBook != NULL);
  1241.   if (ans) {
  1242.  
  1243.     /*
  1244.      *  WhatToDo: The code placed here should flush the data on the notepad in a single disk file.
  1245.      */
  1246.  
  1247.     ans*= deleteBook(TheBook);
  1248.   }
  1249.   return ans;
  1250. }
  1251.  
  1252. long KthDigitOf(long copy_whichNum, int copy_whatBase, int copy_atPosition, int copy_withOrientation) {
  1253.   numLIFO* lifoDecomposition= NULL;
  1254.   numLIFO* focus;
  1255.   long whichNum= (copy_whichNum >= 0) ? copy_whichNum : -1*copy_whichNum;
  1256.   int whatBase= 10;
  1257.   int  atPosition= copy_atPosition;
  1258.   int  withOrientation= copy_withOrientation;
  1259.   int  displacement;
  1260.   int  deltaDisplacement= 1;
  1261.   long ans=-1;
  1262.   long numSize= howManyDigitsIn(whichNum,whatBase);
  1263.   long test;
  1264.   if ((whichNum==0) && (atPosition==1)) {
  1265.     ans=0;
  1266.   } else if ((atPosition >= 1) && (atPosition <= numSize) ) {
  1267.     while (whichNum != 0) {
  1268.       focus= NULL;
  1269.       test= 0;
  1270.       while (((whichNum-test) % whatBase) != 0) { test++; }
  1271.       focus= (numLIFO*) malloc(sizeof(numLIFO));
  1272.       focus->value= test;
  1273.       focus->belowLink= lifoDecomposition;
  1274.       lifoDecomposition=focus;
  1275.       whichNum-=test;
  1276.       whichNum/=whatBase;
  1277.     }
  1278.   }
  1279.   focus= lifoDecomposition;
  1280.   if (withOrientation == -1) {
  1281.     atPosition*=-1;
  1282.     atPosition+=numSize+1;
  1283.   }
  1284.   displacement=1;
  1285.   while(focus != NULL) {
  1286.     if (displacement==atPosition) ans= focus->value;
  1287.     focus= focus->belowLink;
  1288.     displacement+=deltaDisplacement;
  1289.   }
  1290.  
  1291.   if (!__savingCallsToMallocMode) {
  1292.     discardLifoStack(&lifoDecomposition);
  1293.   } else {
  1294.     __lastDecomposition= lifoDecomposition;
  1295.   }
  1296.   return ans;
  1297. }
  1298.  
  1299. long bookSize(numBook** Array) {
  1300.  
  1301.   numBook* cursor= (Array != NULL) ? *Array : NULL;
  1302.   long count= 0;
  1303.   while (cursor != NULL) {
  1304.     count++;
  1305.     cursor= cursor->nextLink;
  1306.   }
  1307.  
  1308.   return count;
  1309. }
  1310.  
  1311. long factorial(long copy_arg) {
  1312.  
  1313.   long arg, ans;
  1314.   ans= 1;
  1315.   arg= (copy_arg >= 0) ? copy_arg : -1*copy_arg;
  1316.   while (arg > 0) { ans*= arg--; }
  1317.  
  1318.   return (copy_arg >= 0) ? ans : -1*ans;
  1319. }
  1320.  
  1321. long greatestPowerOfBaseIn(long whichNum, int whatBase) {
  1322.   long ans= -1;
  1323.   if (whatBase > 1) ans+= (0 == whichNum) ? (1+_NegInfty) : (1+floor(fabs(log(whichNum))/log(whatBase)));
  1324.   return ans;
  1325. }
  1326.  
  1327. long howManyDigitsIn(long whichNum, int whatBase) {
  1328.   long ans= -1;
  1329.   if (whatBase > 1) {
  1330.     switch (whichNum) {
  1331.       case 0:
  1332.       case 1:
  1333.       ans= 1;
  1334.       break;
  1335.       default:
  1336.       if (whichNum == whatBase) {
  1337.     ans= 2;
  1338.       } else {
  1339.     ans= (1 + greatestPowerOfBaseIn(whichNum, whatBase));
  1340.       }
  1341.     }
  1342.   }
  1343.   return ans;
  1344. }
  1345.  
  1346. long innerRecursion(long arg1, long arg2) {
  1347.   return ((arg1==arg2) ? arg1 : arg2 + 10*innerRecursion(arg1, arg2-1));
  1348. }
  1349.  
  1350. long numLIFONumWriter(numLIFO* Q) {
  1351.   return (((Q->belowLink)==NULL) ? Q->value : Q->value + 10*numLIFONumWriter(Q->belowLink));
  1352. }
  1353.  
  1354. long refPermutOfFirst_iter(int arg, int base) {
  1355.   long ans=0;
  1356.   int nu;
  1357.   for (nu=0; nu < arg; nu++) {
  1358.     ans+=(arg-nu)*floor(pow(base,nu));
  1359.   }
  1360.   return ans;
  1361. }
  1362.  
  1363. long refPermutOfFirst_recu(int arg, int base) {
  1364.   return innerRecursion(1,arg);
  1365. }
  1366.  
  1367. numBook* Qsort(numBook** array) {
  1368.  
  1369.   numBook* cursor01=    NULL;
  1370.   numBook* cursor02=    NULL;
  1371.   numBook* equa_head=   NULL;
  1372.   /*
  1373.   numBook* equa_tail=   NULL;
  1374.   */
  1375.   long addr_head[]= {0,0,0};
  1376.   int  addrSize=          3;
  1377.   int  index=             0;
  1378.   numBook** arg00a=      NULL;
  1379.   numBook** arg01a=      NULL;
  1380.   numBook** arg02a=      NULL;
  1381.   numBook** arg03a=      NULL;
  1382.   numBook*  arg00b=      NULL;
  1383.   numBook*  less_head=   NULL;
  1384.   numBook*  less_tail=   NULL;
  1385.   numBook*  grea_head=   NULL;
  1386.   numBook*  grea_tail=   NULL;
  1387.   numBook*  answ_head=   (array != NULL) ? *array : NULL;
  1388.   long      arrayLength= bookSize(array);
  1389.   long      pivot=       (arrayLength/2)+(arrayLength%2);
  1390.   long      counter=     0;
  1391.  
  1392.   if (arrayLength <= 1) {  
  1393.    
  1394.     array= NULL;
  1395.  
  1396.   } else {
  1397.    
  1398.     cursor01= *array;
  1399.     for (counter=0; (counter < pivot); counter++) { cursor01= cursor01->nextLink; }
  1400.     extractNode(&cursor01, array);
  1401.     equa_head= cursor01;
  1402.     /*
  1403.     equa_tail= cursor01;
  1404.     */
  1405.     cursor01= NULL;
  1406.     cursor02= *array;
  1407.     while (cursor02 != NULL) {
  1408.       cursor01= cursor02;
  1409.       cursor02= cursor02->nextLink;
  1410.       extractNode(&cursor01, array);
  1411.       if ( (cursor01->value) <= (equa_head->value) ) {
  1412.     transferNode(&cursor01, &less_head, &less_tail);
  1413.       } /*else if ( (cursor01->value) == (equa_head->value) ) {
  1414.     transferNode(&cursor01, &equa_head, &equa_tail);
  1415.       }*/ else {
  1416.     transferNode(&cursor01, &grea_head, &grea_tail);
  1417.       }
  1418.     }
  1419.    
  1420.     applyQsortTo(&less_head);
  1421.     applyQsortTo(&grea_head);
  1422.     /*
  1423.     triConcat(&answ_head, &less_head, &equa_head, &grea_head);
  1424.     */
  1425.    
  1426.     /* Inline substitution for the preceding call... */
  1427.  
  1428.   arg00a= &answ_head;
  1429.   arg01a= &less_head;
  1430.   arg02a= &equa_head;
  1431.   arg03a= &grea_head;
  1432.  
  1433.   addr_head[0]= (long)arg01a; addr_head[1]= (long)arg02a; addr_head[2]= (long)arg03a;
  1434.  
  1435.   arg01a= NULL; arg02a= NULL; arg03a= NULL;
  1436.  
  1437.   while ( bookSize( (numBook**) addr_head[index]) == 0 )  {index++;}
  1438.  
  1439.   if ( index < addrSize ) {
  1440.    
  1441.     (*arg00a)= (*((numBook**)(addr_head[index])));
  1442.     arg00b= (*arg00a);
  1443.     while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
  1444.    
  1445.     index++;
  1446.   }
  1447.  
  1448.   while ( index < addrSize ) {
  1449.    
  1450.     if ( bookSize( (numBook**) addr_head[index]) != 0 ) {
  1451.      
  1452.       arg00b->nextLink= (*((numBook**)(addr_head[index])));
  1453.       (*((numBook**)(addr_head[index])))->prevLink= arg00b;
  1454.       while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
  1455.      
  1456.     }
  1457.    
  1458.     index++;
  1459.   }
  1460.  
  1461.   }
  1462.  
  1463.   return answ_head;
  1464. }
  1465.  
  1466. numBook* createNode(long firstElement) {
  1467.  
  1468.   numBook* genesis= (numBook*) malloc(sizeof(numBook));
  1469.  
  1470.   genesis->value= firstElement;
  1471.   genesis->prevLink= NULL;
  1472.   genesis->nextLink= NULL;
  1473.  
  1474.   return genesis;
  1475. }
  1476.  
  1477. void triConcat(numBook** arg00a, numBook** arg01a, numBook** arg02a, numBook** arg03a) {
  1478.  
  1479.   long addr_head[]= {0,0,0};
  1480.   int  addrSize=          3;
  1481.   int  index=             0;
  1482.   numBook* arg00b=     NULL;
  1483.    
  1484.   addr_head[0]= (long)arg01a; addr_head[1]= (long)arg02a; addr_head[2]= (long)arg03a;
  1485.  
  1486.   arg01a= NULL; arg02a= NULL; arg03a= NULL;
  1487.  
  1488.   while ( bookSize( (numBook**) addr_head[index]) == 0 )  {index++;}
  1489.  
  1490.   if ( index < addrSize ) {
  1491.    
  1492.     (*arg00a)= (*((numBook**)(addr_head[index])));
  1493.     arg00b= (*arg00a);
  1494.     while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
  1495.    
  1496.     index++;
  1497.   }
  1498.  
  1499.   while ( index < addrSize ) {
  1500.    
  1501.     if ( bookSize( (numBook**) addr_head[index]) != 0 ) {
  1502.      
  1503.       arg00b->nextLink= (*((numBook**)(addr_head[index])));
  1504.       (*((numBook**)(addr_head[index])))->prevLink= arg00b;
  1505.       while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
  1506.      
  1507.     }
  1508.    
  1509.     index++;
  1510.   }
  1511.  
  1512. }
  1513.  
  1514. void clearBookAndOptions(numBook** theBook) {
  1515.  
  1516. auxBookTail=  NULL;
  1517. swapBookHead= NULL;
  1518. swapBookTail= NULL;
  1519.  
  1520. deleteBook(theBook);
  1521.  
  1522. entries=              0;
  1523. _firstCleanUp=        0;
  1524. valueTosubtract=      0;
  1525. valueToDivide=        1;
  1526. offsetShifting=       0;
  1527. justDeltaMode=        0;
  1528. old_justDeltaMode=    0;
  1529. old_offsetShifting=   0;
  1530. old_valueToDivide=    1;
  1531. old_valueTosubtract=  0;
  1532. _Is_SJT_Notepad=      0;
  1533. notepadHasChanged=    0;
  1534.  
  1535. }
  1536.  
  1537. void discardNumBook(numBook** bookOfPermutations) {
  1538.   deleteBook(bookOfPermutations);
  1539. }
  1540.  
  1541. void extractFromBook(numBook** catch) {
  1542.  
  1543.   numBook* prev= (*catch)->prevLink;
  1544.   numBook* next= (*catch)->nextLink;
  1545.  
  1546.   (*catch)->prevLink= NULL;
  1547.   (*catch)->nextLink= NULL;
  1548.  
  1549.   if ( (prev != NULL) && (next != NULL) ) {
  1550.     prev->nextLink= next;
  1551.     next->prevLink= prev;
  1552.   }
  1553.  
  1554.   if ( (prev == NULL) && (next != NULL) ) {
  1555.     next->prevLink= NULL;
  1556.   }
  1557.  
  1558.   if ( (prev != NULL) && (next == NULL) ) {
  1559.     prev->nextLink= NULL;
  1560.   }
  1561.  
  1562.   if ( (prev == NULL) && (next == NULL) ) {
  1563.     ; /* Nothing! :-) */
  1564.   }
  1565.  
  1566. }
  1567.  
  1568. void goodBye(void) {
  1569.  if ( !(_BatchInputMode) ) {
  1570.   printf("\n\nThat's all for now. (Exiting to the OS)\n\n");
  1571.  }
  1572. }
  1573.  
  1574. void insertToBook(numBook** ElementToInsert, numBook** BookHead, numBook** BookTail) {
  1575.  
  1576.   if ( (ElementToInsert != NULL) && ( (BookHead != NULL) && (BookTail != NULL) ) ) {
  1577.    
  1578.     if (*BookHead == NULL) {
  1579.      
  1580.       *BookHead= *ElementToInsert;
  1581.       *BookTail= *BookHead;
  1582.      
  1583.     } else {
  1584.      
  1585.       if (*BookTail == NULL) {
  1586.     *BookTail= *BookHead;
  1587.     while ((*BookTail)->nextLink != NULL) { *BookTail= (*BookTail)->nextLink; }
  1588.       }
  1589.      
  1590.       (*BookTail)->nextLink= *ElementToInsert;
  1591.       (*BookTail)= *ElementToInsert;
  1592.     }
  1593.    
  1594.     *ElementToInsert= NULL;    
  1595.    
  1596.   }
  1597. }  
  1598.  
  1599. void old_printNumBook(numBook** theBook, long forFirst) {
  1600.  
  1601.   long ground= refPermutOfFirst_iter(forFirst, 10);
  1602.  
  1603.   /*
  1604.    *    By default it should be initialized as zero!!
  1605.    */
  1606.   long offsetCounter= 0;
  1607.  
  1608.   numBook* cursor;
  1609.   cursor= *theBook;
  1610.  
  1611.   if (!b_file_mode) {
  1612.     printf("\n------------------");
  1613.     while (cursor != NULL) {
  1614.       if ((cursor->value - ground)/(10-1) != 0) {
  1615.     printf("\n%li\t%li", ++offsetCounter, ((cursor->value - ground)/(10-1)) );
  1616.       }
  1617.       ground=cursor->value;
  1618.       cursor= cursor->nextLink;
  1619.     }
  1620.     printf("\n------------------\n");  
  1621.   } else {
  1622.     printf("\n------------------");
  1623.     while (cursor != NULL) {
  1624.       if ((cursor->value - ground)/(10-1) != 0) {
  1625.     printf("\n%li\t%li", ++offsetCounter, ((cursor->value - ground)/(10-1)) );
  1626.       }
  1627.       ground=cursor->value;
  1628.       cursor= cursor->nextLink;
  1629.     }
  1630.     printf("\n------------------\n");  
  1631.   }
  1632. }
  1633.  
  1634. void print_Seq_Notepad_Main_Menu_(void) {
  1635.  
  1636.  if ( !(_BatchInputMode) ) {
  1637.  
  1638.   printf("\n");
  1639.   printf("\n\t===========================================================================");
  1640.   printf("\n\t seqNpad ver:3.29");
  1641.   printf("\n\t===========================================================[ Main menu ]===");
  1642.   printf("\n\t  -1 Multiply by -1 the last entry.");
  1643.   printf("\n\t  -2 Exit to the OS.");
  1644.   printf("\n\t  -3 Print the notepad.");
  1645.   printf("\n\t  -4 For delete a number.");
  1646.   printf("\n\t  -5 For move a number.");
  1647.   printf("\n\t  -6 Sort the notepad (applying Qsort).");
  1648.   printf("\n\t  -7 Set the output modifiers.");
  1649.   printf("\n\t  -8 Set a shifting for the offsets.");
  1650.   printf("\n\t  -9 Reset to a new and empty notepad.");
  1651.   printf("\n\t -10 Sum/multiply each entry in the notebook with/by a given value.");
  1652.   printf("\n\t -11 Insert a number.");
  1653.   printf("\n\t -12 Run sequencer for a(n) and store its first terms in a NEW notepad.");
  1654.   printf("\n\t===========================================================================");
  1655.   printf("\n\t Every non-negative number will be stored directly in the current notepad.");
  1656.  
  1657.  }
  1658.  
  1659. }
  1660.  
  1661. void requestUserInput(long* reader) {
  1662.  
  1663. if (_BatchInputMode) {
  1664.   *reader= _Script[_ScriptCursor++];
  1665.   switch (*reader) {
  1666.     case -112:
  1667.       *reader=_ScriptMem01;
  1668.       break;  
  1669.     case -211:
  1670.       _rec= 1;
  1671.       *reader= -911;
  1672.     case -911: if (_lastScriptCommand != -6) {
  1673.       _lastScriptCommand= -911;
  1674.       printf("(Awaiting for user input) ");
  1675.       scanf(_CtrlStr, reader);
  1676.       getchar();
  1677.       if (_rec) {
  1678.     _ScriptMem01= *reader;
  1679.     _rec= 0;
  1680.       }
  1681.     } break;
  1682.     case -1011:
  1683.       if (_ScriptMem01 != 0) {
  1684.     *reader= refPermutOfFirst_iter(_ScriptMem01, 10);
  1685.       } else {
  1686.     *reader= -2;
  1687.       }
  1688.       break;
  1689.     case -666: if (_lastScriptCommand != -6) {
  1690.       _ScriptCursor= 0;
  1691.       _firstCleanUp= 1;
  1692.       printf("%li\n", *reader);
  1693.     } break;  
  1694.     case -321: if (_lastScriptCommand != -6 ) {
  1695.       /*
  1696.        * Any code placed here should make a pause (devised for demonstrations).
  1697.        */
  1698.       _lastScriptCommand= -321;
  1699.       printf("%li\n", *reader);
  1700.     } break;    
  1701.     default:
  1702.       printf("%li, ", *reader);
  1703.       _lastScriptCommand= *reader;
  1704.       break;
  1705.   }
  1706.   } else {
  1707.     scanf(_CtrlStr, reader);
  1708.     getchar();
  1709.   }
  1710. }
  1711.  
  1712. void retrieveOptions(void) {
  1713.  
  1714.    valueTosubtract=     old_valueTosubtract;
  1715.    valueToDivide=       old_valueToDivide;
  1716.    justDeltaMode=       old_justDeltaMode;
  1717.    offsetShifting=      old_offsetShifting;
  1718.    if (notepadHasChanged) { _Is_SJT_Notepad= 0; }
  1719.  
  1720. }
  1721.  
  1722. void runBendingA217626Demo(numBook** theBook, long wireSize) {
  1723.  
  1724.   printNumBook(theBook);
  1725.  
  1726. }
  1727.  
  1728. void runSeqAn(numBook** onThisBook, long* n) {
  1729.  
  1730.   /*
  1731.    *
  1732.    * This is a demo. Your a(n) generating function must be
  1733.    * implemented in C and properly placed here.
  1734.    *
  1735.    */
  1736.  
  1737.   /* */
  1738.   runRainDance(onThisBook, *n);
  1739.   applyQsortTo(onThisBook);
  1740.   /* */
  1741.   /* * /
  1742.   runRainDance(&swapBookHead, *n);
  1743.   swapBookTail= auxBookTail;
  1744.   auxBookTail= NULL;
  1745.   aBadQuicksort01(onThisBook, &auxBookTail, &swapBookHead, &swapBookTail);
  1746.   / * */
  1747.  
  1748.   *n= factorial(*n);
  1749.  
  1750.   /*
  1751.    * Due the inconvenience of not having a better way
  1752.    * of inverse function for the integer factorial,
  1753.    * the author decided use "n" as shown above.
  1754.    * However, let the user remember in this
  1755.    * case that "n" points to "entries".
  1756.    *
  1757.    * (Of course this comment is about the clear
  1758.    * difference existing in a general context
  1759.    * between the numbers n, and n!)
  1760.    *
  1761.    */
  1762.  
  1763. }
  1764.  
  1765.  
  1766. void runRainDance(numBook** Output, long forFirst) {
  1767.  
  1768.   numBook*   emul=       NULL;
  1769.   numBook**  Input=      &emul;
  1770.   numBook*   qurzor=     NULL;
  1771.   numBook*   newPerm=    NULL;
  1772.   numBook*   OutputTail= NULL;
  1773.   numLIFO* add=        NULL;
  1774.   numLIFO* swapper=    NULL;
  1775.   long       tmp=        0;
  1776.   long       lambda=     0;
  1777.   int        signature=  1;
  1778.  
  1779.   auxBookTail= NULL;
  1780.  
  1781.   __savingCallsToMallocMode= 1;
  1782.  
  1783.   if (__lastDecomposition != NULL) {
  1784.     discardLifoStack(&__lastDecomposition);
  1785.   }
  1786.   if ((Output != NULL) && (forFirst >= 1)) {
  1787.     (*Output)= (numBook*) malloc(sizeof(numBook));
  1788.     (*Output)->value= 1;
  1789.     (*Output)->prevLink= NULL;
  1790.     (*Output)->nextLink= NULL;
  1791.     for (lambda= 2; lambda<= forFirst; lambda++) {
  1792.       if (*Input != NULL) {
  1793.     discardNumBook(Input);
  1794.       }
  1795.       (*Input)= (*Output);
  1796.       *Output= NULL;
  1797.       OutputTail= NULL;
  1798.       qurzor= *Input;
  1799.       while (qurzor != NULL) {
  1800.     KthDigitOf(qurzor->value,10,1,1);
  1801.     add= (numLIFO*) malloc(sizeof(numLIFO));
  1802.     add->value= lambda;
  1803.     add->belowLink= __lastDecomposition;
  1804.     __lastDecomposition= add;
  1805.     newPerm= (numBook*) malloc(sizeof(numBook));
  1806.     newPerm->value= numLIFONumWriter(__lastDecomposition);
  1807.     newPerm->nextLink= NULL;
  1808.     if (*Output == NULL) {
  1809.       newPerm->prevLink= NULL;
  1810.       *Output= newPerm;
  1811.       OutputTail= *Output;
  1812.     } else {
  1813.       newPerm->prevLink= OutputTail;
  1814.       OutputTail->nextLink= newPerm;
  1815.       OutputTail= newPerm;
  1816.     }
  1817.     for (swapper=__lastDecomposition; swapper->belowLink != NULL; ) {
  1818.       tmp= swapper->value;
  1819.       add= swapper->belowLink;
  1820.       (swapper->value)=(add->value);
  1821.       (add->value)= tmp;
  1822.       swapper=swapper->belowLink;
  1823.       newPerm= (numBook*) malloc(sizeof(numBook));
  1824.       newPerm->value= numLIFONumWriter(__lastDecomposition);
  1825.       newPerm->nextLink= NULL;
  1826.       if (*Output == NULL) {
  1827.         (*Output)=newPerm;
  1828.         (*Output)->prevLink= NULL;
  1829.         OutputTail= *Output;
  1830.       }
  1831.       else {
  1832.         newPerm->prevLink= OutputTail;
  1833.         OutputTail->nextLink= newPerm;
  1834.         OutputTail=newPerm;
  1835.       }
  1836.     }
  1837.       signature*= -1;
  1838.       qurzor= qurzor->nextLink;
  1839.       add= NULL;
  1840.       discardLifoStack(&__lastDecomposition);
  1841.       }
  1842.     }
  1843.   }
  1844.   __savingCallsToMallocMode= 0;
  1845.   if (*Input != NULL) {
  1846.     discardNumBook(Input);
  1847.   }
  1848.  
  1849.   auxBookTail= OutputTail;
  1850.  
  1851. }
  1852.  
  1853. void run_seqNpad(void) {
  1854.  
  1855.   numBook* seqNotepad= NULL;
  1856.  
  1857.   numBook* pencil01=   NULL;
  1858.   numBook* pencil02=   NULL;
  1859.   numBook* pencil03=   NULL;
  1860.  
  1861.   numBook* aThinWire=  NULL;
  1862.   /*
  1863.   long gamma= 0;
  1864.   */
  1865.  
  1866.   long userInput01= 0;
  1867.   long userInput02= 0;
  1868.   long userInput03= 0;
  1869.   long userInput04= 0;
  1870.  
  1871.   long auxCounter01;
  1872.   long auxCounter02;
  1873.  
  1874.   long temp;
  1875.  
  1876.   do {
  1877.  
  1878.     loadNumBookFromDisk(&seqNotepad);
  1879.    
  1880.     print_Seq_Notepad_Main_Menu_();
  1881.     if ( !(_BatchInputMode) ) { printf("\n\n\t(Enter a seqNpad command) >> "); }
  1882.     requestUserInput(&userInput01);
  1883.    
  1884.       if (userInput01 >= 0) {
  1885.    
  1886.     pencil01= createNode(userInput01);
  1887.     lastEntry= pencil01;
  1888.     allowChangeTheSign= 1;
  1889.     appendNode(&seqNotepad, NULL, &pencil01);
  1890.     entries++;
  1891.     notepadHasChanged= 1;
  1892.    
  1893.       } else {
  1894.    
  1895.     __OnPrintingViewModeCommutator= userInput01;
  1896.    
  1897.     if (userInput01 != -1) {
  1898.       lastEntry= NULL;
  1899.       allowChangeTheSign= 0;
  1900.     }
  1901.  
  1902.     switch (__OnPrintingViewModeCommutator) {
  1903.       case  -4:
  1904.       case  -5:
  1905.       case  -6:
  1906.       case -11:
  1907.         saveOptions();
  1908.         break;
  1909.       default:
  1910.         break;
  1911.     }
  1912.  
  1913.     switch (userInput01) {
  1914.       case -1:
  1915.         if (allowChangeTheSign) {
  1916.           lastEntry->value*= -1;
  1917.           lastEntry= NULL;
  1918.           allowChangeTheSign= 0;
  1919.         }
  1920.         break;
  1921.       case -2:
  1922.         printf("\nYou are about to exit the interpreter. \nAre you sure? (1=yes, 0=NO): ");
  1923.         do requestUserInput(&userInput02); while ( !( (userInput02 == 0) || (userInput02 == 1) ) );
  1924.         userInput01*= userInput02;
  1925.         printf("\n");
  1926.         break;
  1927.       case -3:
  1928.           printf("\n\nThe following information is part of the notepad:");
  1929.           printNumBook(&seqNotepad);
  1930.           printf("%li Element(s).\n", entries);
  1931.           if (entries > 0) {
  1932.           bigSigma= 0;
  1933.           pencil03= seqNotepad;
  1934.           precedingEntry= valueTosubtract;
  1935.           while (pencil03 != NULL) {
  1936.         bigSigma+= (long)( (pencil03->value - precedingEntry)/valueToDivide );
  1937.         if (justDeltaMode) { precedingEntry= pencil03->value; }
  1938.         pencil03= pencil03->nextLink;
  1939.           }
  1940.           printf("\nThe sum of every entry in the notepad is: %10li;\nAnd the average among all the entries is: %10.5f;\n", bigSigma, (float)(bigSigma/entries) );
  1941.           }
  1942.           do {
  1943.           printf("\nEnter \"1\" to continue when ready: ");
  1944.           requestUserInput(&userInput03);
  1945.           } while (userInput03 != 1);
  1946.         break;
  1947.       case -4: if (entries > 0) {
  1948.           do {
  1949.         printNumBook(&seqNotepad);
  1950.         printf("%li Element(s).\n", entries);
  1951.         printf("\nEnter the offset of the number you want to delete \n(See the left column, 0 to abort): ");
  1952.         if (entries == 1) { userInput01= 1; printf("1\n"); }
  1953.         else if (entries == 0) { userInput01= 0; printf("0\n"); }
  1954.         else { requestUserInput(&userInput01); }
  1955.           } while ( !((0 <= userInput01 ) || ( userInput01 <= entries )) );
  1956.           if (userInput01 != 0) {
  1957.         pencil01= seqNotepad;
  1958.         auxCounter01= 0;
  1959.         while ((pencil01 != NULL) && (++auxCounter01 != userInput01)) {
  1960.           pencil01= pencil01->nextLink;
  1961.         }
  1962.         if (auxCounter01 == userInput01) {
  1963.           if (entries > 1) { deleteNode(&pencil01, &seqNotepad); }
  1964.           else { deleteBook(&seqNotepad); }
  1965.           if (entries >= 1) { entries--; }
  1966.           printNumBook(&seqNotepad);
  1967.           printf("%li Element(s).\n", entries);
  1968.         }
  1969.         notepadHasChanged= 1;
  1970.           }
  1971.         } else {
  1972.           printf("\nThe notepad is empty. There is nothing to delete.\n");
  1973.         }
  1974.         break;
  1975.       case -5: if (entries > 0) {
  1976.         printNumBook(&seqNotepad);
  1977.         printf("%li Element(s).\n", entries);
  1978.         do {
  1979.         printf("\nEnter the offset of the number you want to move \n(See the left column, 0 to abort): ");
  1980.         requestUserInput(&userInput01);
  1981.         } while ( !( (userInput01 >= 0) && (userInput01 <= entries ) ) );
  1982.  
  1983.         do {
  1984.         printf("\nEnter the offset of the new position: \n(See the left column, 0 to abort): ");
  1985.         if (userInput01 != 0) { requestUserInput(&userInput02); } else { userInput02= 0; printf("0\n"); }
  1986.         } while ( !( (userInput02 >= 0) && (userInput02 <= entries ) ) );
  1987.        
  1988.         if (( userInput01 != 0) && (userInput02 != 0) && (userInput01 != userInput02) ) {
  1989.           pencil01= seqNotepad;
  1990.           auxCounter01= 0;
  1991.           while ((pencil01 != NULL) && (++auxCounter01 != userInput01)) {
  1992.             pencil01= pencil01->nextLink;
  1993.           }
  1994.           pencil02= seqNotepad;
  1995.           auxCounter02= 0;
  1996.           while ((pencil02 != NULL) && (++auxCounter02 != userInput02)) {
  1997.             pencil02= pencil02->nextLink;
  1998.           }
  1999.           temp=pencil01->value;
  2000.           pencil01->value= pencil02->value;
  2001.           pencil02->value= temp;
  2002.         }
  2003.         printNumBook(&seqNotepad);
  2004.         printf("%li Element(s).\n", entries);
  2005.         notepadHasChanged= 1;
  2006.           } else {
  2007.         printf("\nThe notepad is empty. There is nothing to move.\n");
  2008.           }
  2009.         break;
  2010.       case -6: if (entries > 1) {
  2011.           _kronometer00= 0;
  2012.           printf("\n\tApplying Qsort. Please wait...");
  2013.           _kronometer00-= clock();
  2014.           applyQsortTo(&seqNotepad);
  2015.           _kronometer00+= clock();
  2016.           if (_QsortFirstRun) {
  2017.            printf(" Ready. (\"tau\" factor: %.4f)", ((float)_kronometer00/entries));
  2018.            _QsortFirstRun= 0;
  2019.           } else {
  2020.            printf(" Ready.");
  2021.           }
  2022.           /*
  2023.           if (!_BatchInputMode) {
  2024.         printNumBook(&seqNotepad);
  2025.         printf("%li Element(s).\n", entries);
  2026.        
  2027.         do {
  2028.           printf("\nEnter \"1\" to continue when ready: ");
  2029.           requestUserInput(&userInput03);
  2030.         } while (userInput03 != 1);
  2031.        
  2032.         printf("\n");
  2033.           }
  2034.           */
  2035.           notepadHasChanged= 1;
  2036.         } else if (entries == 0) {
  2037.         printf("\nThe notepad is empty. There is nothing to sort.\n");
  2038.         }
  2039.         break;
  2040.       case -7:
  2041.           printf("\n Output modifiers settings:\n============================\n");
  2042.           printf("\nAll the entries of a notepad may be processed before printing them to stdout,");
  2043.           printf("\nthis is done subtracting and dividing each entry with certain values you may specify.");
  2044.           printf("\n\nBy default when printing, zero is subtracted to each entry and then it is divided by 1.");
  2045.           printf("\nDo you want to change these values? (1=yes, 0=NO): ");
  2046.           do requestUserInput(&userInput01); while ( !( (userInput01 == 0) || (userInput01 == 1) ) );
  2047.           if (userInput01) {
  2048.         printf("The following value will be subtracted to each entry in the notepad... \n(please enter an integer value): ");
  2049.         requestUserInput(&userInput02);
  2050.         do {
  2051.         printf("All the differences described before will be divided by... \n(please enter an integer value. Zero is not allowed): ");
  2052.         requestUserInput(&userInput03);
  2053.         } while (userInput03 == 0);
  2054.         valueTosubtract= userInput02;
  2055.         valueToDivide= userInput03;
  2056.         printf("Enable the \"justDelta\" feature? (1=yes, 0=NO): ");
  2057.         do {
  2058.           requestUserInput(&justDeltaMode);
  2059.         } while ( !( (justDeltaMode == 0) || (justDeltaMode == 1) ) );
  2060.           }
  2061.         break;
  2062.       case -8:
  2063.           printf("\nBy default, when displaying the entries of a notepad, the offset column starts in 1.");
  2064.           printf("\nYou may change this behavior by defining a \"shifting\" value. It will added to the inner");
  2065.           printf("\ncounter, fixing the offset in the way you want.");
  2066.           printf("\nEnter a new value for shifting (the current is %li): ",offsetShifting);
  2067.           requestUserInput(&userInput03);
  2068.           do {
  2069.         printf("\nYou are about to change the offset shifting from %li to %li; \n Is this correct? (1=yes, 0=NO): ", offsetShifting, userInput03);
  2070.         requestUserInput(&userInput02);
  2071.           } while ( !( (userInput02 != 0) || (userInput02 != 1) ) );
  2072.           if (userInput02) {
  2073.         offsetShifting= userInput03;
  2074.         printf("\nThe new starting offset will be: %li\n", (1+offsetShifting));
  2075.           }
  2076.         break;
  2077.       case -9:
  2078.         if ( (_firstCleanUp) && (_BatchInputMode) ) { clearBookAndOptions(&seqNotepad); } else { if (entries > 0) {
  2079.         printf("\nThis will delete the numbers stored in the notepad.\n Proceed? (1=yes, 0=NO): ");
  2080.         requestUserInput(&userInput02);
  2081.         if (userInput02) {
  2082.           printf("\nWARNING: You cannot undo this action.\n Are you sure? (1=yes, 0=NO): ");
  2083.           requestUserInput(&userInput03);
  2084.           if (userInput03) {
  2085.        
  2086.         clearBookAndOptions(&seqNotepad);
  2087.         printNumBook(&seqNotepad);
  2088.         printf("%li Element(s).\n", entries);
  2089.  
  2090.           }
  2091.         } } else {
  2092.           printf("\nThe notepad is already empty!\n");
  2093.         }
  2094.         }
  2095.         break;
  2096.       case -10:
  2097.         if (entries > 0) {
  2098.           do {
  2099.         printf("\nYou're about to: \n\t\tsum/multiply each entry in the notebook \n\t\twith/by a given value.\n\nProceed? (1=yes, 0=NO): ");
  2100.         requestUserInput(&userInput03);
  2101.           } while (!( (userInput03 >= 0) && (userInput03 <= 1) ));
  2102.           if (userInput03 == 1) {
  2103.         do {
  2104.           printf("\nSelect the operation (1=Sum, 0=Multiply): ");
  2105.           requestUserInput(&userInput02);
  2106.         } while ((userInput02 != 0) && (userInput02 != 1));
  2107.         printf("\nEnter the value (0 to abort): ");
  2108.         requestUserInput(&userInput04);
  2109.         if (userInput04 != 0) {
  2110.           if (userInput02 == 1) {
  2111.             pencil03= seqNotepad;
  2112.             while (pencil03 != NULL) {
  2113.               pencil03->value+= userInput04;
  2114.               pencil03= pencil03->nextLink;
  2115.             }
  2116.           }
  2117.           if (userInput02 == 0) {
  2118.             pencil03= seqNotepad;
  2119.             while (pencil03 != NULL) {
  2120.               pencil03->value*= userInput04;
  2121.               pencil03= pencil03->nextLink;
  2122.             }
  2123.           }
  2124.         }
  2125.           }
  2126.         } else {
  2127.           printf("\n\n\tBefore doing this, you should enter some numbers to the notebook.\n");
  2128.         }
  2129.         break;
  2130.       case -11:
  2131.         if (entries > 0) {
  2132.         printf("\nInserting a new number saves moving/deleting operations.");
  2133.         printNumBook(&seqNotepad);
  2134.         printf("%li Element(s).\n", entries);
  2135.         do {
  2136.           printf("\nPlease enter the offset for the element that would be placed below");
  2137.           printf("\nthe number that you want to insert (See the left column, 0 to abort): ");
  2138.           requestUserInput(&userInput02);
  2139.         } while ( !( (userInput02 >= 0) && (userInput02 <= entries ) ) );
  2140.         if (userInput02 != 0) {
  2141.           printf("\nEnter the new value to insert: ");
  2142.           requestUserInput(&userInput03);
  2143.           pencil03= createNode(userInput03);
  2144.           lastEntry= pencil03;
  2145.           userInput01= 1;
  2146.           pencil01= seqNotepad;
  2147.           while (userInput01 != userInput02) {
  2148.         pencil02=pencil01;
  2149.         pencil01=pencil01->nextLink;
  2150.         userInput01++;
  2151.           }
  2152.           if (userInput02 == 1) {
  2153.         pencil01->prevLink= pencil03;
  2154.         pencil03->nextLink= pencil01;
  2155.         seqNotepad= pencil03;
  2156.           } else {
  2157.         pencil02->nextLink= pencil03;
  2158.         pencil03->prevLink= pencil02;
  2159.         pencil01->prevLink= pencil03;
  2160.         pencil03->nextLink= pencil01;
  2161.           }      
  2162.           allowChangeTheSign= 1;
  2163.           entries++;
  2164.           notepadHasChanged= 1;
  2165.         }
  2166.         } else {
  2167.         printf("\nThis action is not available while the notepad is empty. \n");
  2168.         }
  2169.         break;
  2170.       case -12:
  2171.        
  2172.         /*
  2173.          *
  2174.          * Since this step generates a SJT notepad, it should not mess with "notepadHasChanged" beyond
  2175.          * setting its value to zero. Why?: "notepadHasChanged" is a security measure to prevent wrong
  2176.          * assumptions when using Quicksort (in order to apply always the proper variation).
  2177.          *
  2178.          * Any other step modifying a notepad should take among its duties setting "notepadHasChanged" to "1"
  2179.          * ONLY if a real change in the notepad was made upon its execution (or leaving it unchanged if aborted).
  2180.          *
  2181.          * (The behavior described here is imperative!)
  2182.          *
  2183.          * A colateral effect: Every SJT notepads loaded MANUALLY by the user or via internal script won't be
  2184.          * treated as SJT when sorting.
  2185.          *
  2186.          * Then an additional measure to keep Qb taking advantage of knowing whenever a notepad is SJT or not:
  2187.          *
  2188.          *      Check before applying Qsort the following:
  2189.          *
  2190.          *         1) Counting the number of entries for the given notepad, must result a factorial. [ O(n!) fixed ]
  2191.          *         2) Every entry must be a number compound of n digits.
  2192.          *         3) Every entry must be unique. [ O(n^2) in the worst case, precisely in a true SJT Nbook ]
  2193.          *         4) Multiplying all the digits in each entry, n! must arise as such product.
  2194.          *
  2195.          *      Then select the proper variation according the case.
  2196.          *
  2197.          * Check this each time Qsort is applied would be certainly a waste. For such reason the "notepadHasChanged" flag
  2198.          * mechanism was implemented.
  2199.          *
  2200.          */
  2201.        
  2202.         if (entries > 0) {
  2203.         printf("\nThis will delete the numbers stored in the notepad.\n Proceed? (1=yes, 0=NO): ");
  2204.           requestUserInput(&userInput02);
  2205.           if (userInput02) {
  2206.         printf("\nWARNING: You cannot undo this action.\n Are you sure? (1=yes, 0=NO): ");
  2207.             requestUserInput(&userInput03);
  2208.           if (userInput03) {    
  2209.             clearBookAndOptions(&seqNotepad);
  2210.         do {
  2211.              printf("\n%s.\n\nEnter \"m\" for generating the a(n) terms for n= 1..%s; m= ", seqAnDescriptorString, (_a217626_fixTextGlitch) ? "m!" : "m");
  2212.              requestUserInput(&userInput04);
  2213.         } while ( !( (userInput04 >= 1) && (userInput04 <= 11) ) );
  2214.             entries= userInput04;
  2215.             runSeqAn(&seqNotepad, &entries);
  2216.         _Is_SJT_Notepad=   1;
  2217.         notepadHasChanged= 0;
  2218.             }
  2219.           }
  2220.         }
  2221.         else {
  2222.           clearBookAndOptions(&seqNotepad);
  2223.           do {
  2224.             printf("\n%s.\n\nEnter \"m\" for generating the a(n) terms for n= 1..%s; m= ", seqAnDescriptorString, (_a217626_fixTextGlitch) ? "m!" : "m");
  2225.             requestUserInput(&userInput04);
  2226.           } while ( !( (userInput04 >= 1) && (userInput04 <= 11) ) );
  2227.           entries= userInput04;
  2228.           runSeqAn(&seqNotepad, &entries);
  2229.           _Is_SJT_Notepad=   1;
  2230.           notepadHasChanged= 0;
  2231.         }
  2232.         break;
  2233.       /*  
  2234.       case -13: / * Experimental: Bending a piece of A217626 * /
  2235.           gamma= __SizeFor_BendingTheWireDemo;
  2236.           if (aThinWire == NULL) { runSeqAn(&aThinWire, &gamma); }
  2237.           runBendingA217626Demo(&aThinWire,gamma);
  2238.         break;
  2239.       */
  2240.       default:
  2241.         printf("\nUnknown command. Try \"-2\".\n");
  2242.         break;
  2243.     }
  2244.    
  2245.     switch (__OnPrintingViewModeCommutator) {
  2246.       case  -4:
  2247.       case  -5:
  2248.       case  -6:
  2249.       case -11:
  2250.         retrieveOptions();
  2251.         break;
  2252.       default:
  2253.         break;
  2254.     }
  2255.    
  2256.       }
  2257.      
  2258.   } while (userInput01 != -2);
  2259.  
  2260.   if (aThinWire != NULL) { deleteBook(&aThinWire); }
  2261.   saveNumBookToDisk(&seqNotepad);
  2262.  
  2263. }
  2264.  
  2265. /*  This is a friendly "keep out!... brave dog" banner at the LAB's closed door. */
  2266.  
  2267. /* * /
  2268.  
  2269. void run_seqNpad(void) {
  2270.  
  2271.   long pencil= 0;
  2272.   long sum= 0;
  2273.   long count= 0;
  2274.  
  2275.   _BatchInputMode= 0;
  2276.  
  2277.   printf("\n\n:-) \n\t\tHi OEIS!, \n\t\t\tHello seqFan!. \n\n\tThis is an Interpreter and Notepad for Integer Sequences.\n\t(Possibly yet under construction if you are reading this message)");
  2278.   do {
  2279.     printf("\n\n(There's nothing implemented yet. Please excuse me... \nEnter -2 to exit, or a nonn to sum, 0 to restart the sum) >> ");
  2280.     requestUserInput(&pencil);
  2281.     if (pencil == 0) {
  2282.       sum=   0;
  2283.       count= 0;
  2284.     } else if (pencil > 0) {
  2285.       sum+= pencil;
  2286.       count++;
  2287.     }
  2288.   } while (pencil != -2);
  2289.   if (count > 1) {
  2290.     printf("\n\n\tSum: %li; Average[%li number(s)]:%9.3f", sum, count, (float)sum/count);
  2291.   }
  2292.   printf("\n\nGood bye. Come back soon.\n\n");
  2293.  
  2294. }
  2295.  
  2296. / * */
  2297.  
  2298. void saveOptions(void) {
  2299.  
  2300.     old_valueTosubtract=    valueTosubtract;
  2301.     old_valueToDivide=      valueToDivide;
  2302.     old_justDeltaMode=      justDeltaMode;
  2303.     old_offsetShifting=     offsetShifting;
  2304.     valueTosubtract=        0;
  2305.     valueToDivide=          1;
  2306.     justDeltaMode=          0;
  2307.     offsetShifting=         0;
  2308.    
  2309. }
  2310.  
  2311. void startupGreetings(void) {
  2312.  
  2313.  if ( !(_BatchInputMode) ) {  
  2314.    
  2315.  printf("\n\tseqNpad - Script interpreter and Notepad for Integer Sequences ver:3.29 \n\t\tR. J. Cano, <aallggoorriitthhmmuuss@gmail.com>\n\n\tWelcome!");
  2316.  
  2317.  } else {
  2318.  
  2319.    printf("\nseqNpad^SI ver:3.29 - Script Interpreter (Batch mode On):\n\n");
  2320.    
  2321.  }
  2322.  
  2323. }
  2324.  
  2325.  
  2326. void slow_Sort(numBook** BookToSort) {
  2327.  
  2328.   numBook* fromWhere= *BookToSort;
  2329.   numBook* searchCursor;
  2330.   numBook* compare;
  2331.   long tmp;
  2332.   while (fromWhere != NULL) {
  2333.     compare=fromWhere;
  2334.     searchCursor= fromWhere;
  2335.     while (searchCursor != NULL) {
  2336.       if (compare->value > searchCursor->value) {
  2337.     tmp=searchCursor->value;
  2338.     searchCursor->value= compare->value;
  2339.     compare->value= tmp;
  2340.       }
  2341.       searchCursor= searchCursor->nextLink;
  2342.     }
  2343.     fromWhere= fromWhere->nextLink;    
  2344.   }
  2345.  
  2346. }
  2347.  
  2348. void transferNode(numBook** BB, numBook** AA_head, numBook** AA_tail) {
  2349.  
  2350.   if (BB != NULL) {
  2351.     if (AA_head != NULL) {
  2352.       if (AA_tail != NULL) {
  2353.     if (*AA_head == NULL) {
  2354.      
  2355.       *AA_head= *BB;
  2356.       *AA_tail= *BB;
  2357.       *BB= NULL;
  2358.      
  2359.     } else {
  2360.      
  2361.       while ((*AA_tail)->nextLink != NULL) {
  2362.         (*AA_tail)= (*AA_tail)-> nextLink;
  2363.       }
  2364.       (*AA_tail)->nextLink= *BB;
  2365.       (*BB)->prevLink= *AA_tail;
  2366.       *AA_tail= *BB;
  2367.       *BB= NULL;
  2368.      
  2369.     }
  2370.       }
  2371.     }
  2372.   }
  2373. }
  2374.  
  2375. void versusSort(numBook** BookToSort) {
  2376.  
  2377. /* */
  2378.   applyQsortTo(BookToSort);
  2379. /* */
  2380.  
  2381. /* * /
  2382.   slow_Sort(BookToSort);
  2383. / * */
  2384.  
  2385. }
  2386.  
  2387.  
  2388. /*------------------------------------------------------------------------------------------------------------------------------*/
  2389.  
  2390. void configure_modes(void) {
  2391.  
  2392.   __config_generator[__mode_ZERO][__TOKEN_TEMPLATE] = 0;
  2393.   __config_generator[__mode_ZERO][__UNIT]  = 0;
  2394.  
  2395.   __config_generator[__mode_EQUAL][__TOKEN_TEMPLATE] = 0;
  2396.   __config_generator[__mode_EQUAL][__UNIT]  = 1;
  2397.  
  2398.   __config_generator[__mode_DETERMINANT][__TOKEN_TEMPLATE] = 1;
  2399.   __config_generator[__mode_DETERMINANT][__UNIT]  = 0;
  2400.  
  2401. }
  2402.  
  2403. /*------------------------------------------------------------------------------------------------------------------------------*/
  2404.  
  2405. void run_my_SJT_implementation(int just_for_the_Nth_array, long N) {
  2406.  
  2407.   long k;
  2408.  
  2409.   digitsString *J0_head, *J0_tail;
  2410.  
  2411.   for (k=(just_for_the_Nth_array ? N : 1);k<=N;k++) {
  2412.     apply_SteinhausJohnsonTrotterAlgorithm(&J0_head, &J0_tail, k);
  2413.     print_matrix(&J0_head);
  2414.     finalizer_matrix(&J0_head, &J0_tail);
  2415.   }
  2416.  
  2417.   printf("\n\n\tSequencer execution completed successfully.\n");    
  2418.  
  2419. }
  2420.  
  2421. /*------------------------------------------------------------------------------------------------------------------------------*/
  2422.  
  2423. void finalizer_matrix(digitsString **stckPointerReference_HEAD, digitsString **stckPointerReference_TAIL) {
  2424.  
  2425.   while (*stckPointerReference_HEAD != NULL) {
  2426.  
  2427.     (*stckPointerReference_TAIL) = (*stckPointerReference_HEAD);
  2428.    
  2429.     (*stckPointerReference_HEAD) = (*stckPointerReference_HEAD)->nextString;
  2430.    
  2431.     free( *stckPointerReference_TAIL );
  2432.  
  2433.   }
  2434.  
  2435.     (*stckPointerReference_TAIL) = NULL;
  2436.  
  2437. }
  2438.  
  2439. /*------------------------------------------------------------------------------------------------------------------------------*/
  2440.  
  2441. void print_matrix(digitsString **stckPointerReference_HEAD) {
  2442.  
  2443.   digitsString* focus= *stckPointerReference_HEAD;
  2444.  
  2445.   if ( NULL == focus ) {
  2446.  
  2447.     printf(" Not initialized! ");
  2448.  
  2449.   } else {
  2450.  
  2451.     if ( -1 == (focus->integer_value) ) {
  2452.  
  2453.       focus = (*stckPointerReference_HEAD)->nextString;
  2454.  
  2455.       if ( NULL == focus ) {
  2456.  
  2457.     printf(" initialized but empty. ");
  2458.  
  2459.       } else {;}
  2460.  
  2461.     } else {
  2462.  
  2463.     }
  2464.  
  2465.     while ( NULL != focus ) {
  2466.  
  2467.       printf("\n%li", (focus->integer_value) );
  2468.  
  2469.       focus = focus->nextString;
  2470.     }
  2471.  
  2472.   }
  2473.  
  2474. }
  2475.  
  2476. /*------------------------------------------------------------------------------------------------------------------------------*/
  2477.  
  2478. void apply_SteinhausJohnsonTrotterAlgorithm(digitsString **J_head, digitsString **J_tail, long _SIZE) {
  2479.  
  2480.   digitsString *JJ_head;
  2481.   digitsString *JJ_tail;
  2482.   digitsString *P_head;
  2483.   digitsString *P_tail;
  2484.   digitsString *Q_head;
  2485.   digitsString *Q_tail;
  2486.  
  2487.   long diagonal;
  2488.  
  2489.   long _CASE = 1;
  2490.  
  2491.   mode_matrix_waterfall();
  2492.  
  2493.   initializer_matrix(J_head, J_tail);
  2494.   insert_new_node(1, J_tail);
  2495.  
  2496.   while (_CASE < _SIZE) {
  2497.  
  2498.     _CASE++;
  2499.     diagonal = -1;
  2500.     initializer_matrix(&JJ_head, &JJ_tail);
  2501.  
  2502.     while ( !only_initialized(J_head) ) {
  2503.  
  2504.       P_head = extract_first_nodes(J_head, &P_tail, (_CASE-1)) ;
  2505.       generate_square_matrix(&P_head, &P_tail, &Q_head, &Q_tail, _CASE*diagonal);
  2506.       concat_matrices_node_by_node(&JJ_tail, &Q_head, &Q_tail);
  2507.       finalizer_matrix(&P_head, &P_tail);
  2508.  
  2509.       diagonal *= -1;
  2510.  
  2511.     }
  2512.  
  2513.     finalizer_matrix(J_head, J_tail);
  2514.     *J_head = JJ_head;
  2515.     *J_tail   = JJ_tail;
  2516.     JJ_head = NULL;
  2517.     JJ_tail   = NULL;
  2518.  
  2519.   }
  2520.  
  2521. }
  2522.  
  2523. /*------------------------------------------------------------------------------------------------------------------------------*/
  2524.  
  2525. void  mode_matrix_waterfall(void) {
  2526.  
  2527.   generator_settings = __mode_DETERMINANT;
  2528.  
  2529. }
  2530.  
  2531. /*------------------------------------------------------------------------------------------------------------------------------*/
  2532.  
  2533. digitsString* generate_square_matrix(digitsString **baseRef_head, digitsString **baseRef_tail, digitsString **ref_answer_head, digitsString **ref_answer_tail, long TOKEN_TEMPLATE) {
  2534.  
  2535.   long counting_L = 1;
  2536.   long counting_X = 1;
  2537.   long counting_Y = 1;
  2538.   long matrix_size = absolute_value(TOKEN_TEMPLATE);
  2539.   long newValuetobeIncluded;
  2540.  
  2541.   digitsString* BASE_head = *baseRef_head;
  2542.   digitsString* BASE_tail = *baseRef_tail;
  2543.  
  2544.   close__the_ring(&BASE_head, &BASE_tail);
  2545.  
  2546.   initializer_matrix(ref_answer_head, ref_answer_tail);
  2547.  
  2548.   matrix_size = absolute_value(TOKEN_TEMPLATE);
  2549.  
  2550.   while (counting_L <= (matrix_size*matrix_size)) {
  2551.  
  2552.     if (BASE_head->integer_value == -1) { BASE_head = BASE_head->nextString; }
  2553.  
  2554.     newValuetobeIncluded = (TOKEN_TEMPLATE < 0) ? ( ( counting_X + counting_Y == (matrix_size+1) ) ? (matrix_size*__config_generator[generator_settings][__TOKEN_TEMPLATE] + __config_generator[generator_settings][__UNIT]) : (0) ) : ( (counting_X - counting_Y == 0) ? (matrix_size*__config_generator[generator_settings][__TOKEN_TEMPLATE] + __config_generator[generator_settings][__UNIT]) : (0) );
  2555.  
  2556.     if ( ( 0 == newValuetobeIncluded ) && ( __mode_ZERO != generator_settings) ) {
  2557.  
  2558.       newValuetobeIncluded+= (BASE_head->integer_value);
  2559.       BASE_head = BASE_head->nextString;
  2560.  
  2561.     }
  2562.  
  2563.     insert_new_node(newValuetobeIncluded, ref_answer_tail);
  2564.  
  2565.     counting_L++;
  2566.     counting_X++;
  2567.     if (counting_X > matrix_size) {
  2568.       counting_X = 1;
  2569.       counting_Y++;
  2570.     }
  2571.   }
  2572.  
  2573.   open_the_ring(&BASE_head, &BASE_tail);
  2574.  
  2575.   return (*ref_answer_head);
  2576. }
  2577.  
  2578. /*----------------------------------------------------------------------------------------------------------------------(OJO)---*/
  2579.  
  2580. void initializer_matrix(digitsString **stckPointerReference_HEAD, digitsString **stckPointerReference_TAIL) {
  2581.  
  2582.   (*stckPointerReference_HEAD) = NULL;
  2583.   (*stckPointerReference_TAIL) = insert_new_node( -1, stckPointerReference_HEAD );
  2584.  
  2585. }
  2586.  
  2587. /*------------------------------------------------------------------------------------------------------------------------------*/
  2588.  
  2589. digitsString* extract_first_nodes(digitsString** refSource, digitsString** refRemainderTail, long extractions) {
  2590.  
  2591.   digitsString* newRefSource= *refSource;
  2592.   digitsString* answer= (*refSource)->nextString;
  2593.   digitsString* cursor= (*refSource);
  2594.   long steps= (0 == extractions) ? 1 : extractions;
  2595.   long counter=0;
  2596.   while (counter < steps) {
  2597.     cursor= cursor->nextString;
  2598.     counter++;
  2599.   }
  2600.   newRefSource->nextString= cursor->nextString;
  2601.   cursor->nextString= NULL;
  2602.   (*refRemainderTail)= cursor;
  2603.   *refSource = newRefSource;
  2604.  
  2605.   return answer;
  2606.  
  2607. }
  2608.  
  2609. /*------------------------------------------------------------------------------------------------------------------------------*/
  2610.  
  2611. void concat_matrices_node_by_node(digitsString **operand_left_tail, digitsString **operand_right_head, digitsString **operand_right_tail) {
  2612.  
  2613.   digitsString *slayer = *operand_right_head;
  2614.   *operand_right_head = NULL;
  2615.   (*operand_left_tail)->nextString = slayer->nextString;
  2616.   slayer->nextString               = NULL;
  2617.   free(slayer);
  2618.   *operand_left_tail          = *operand_right_tail;
  2619.   *operand_right_tail         = NULL;
  2620.  
  2621. }
  2622.  
  2623. /*------------------------------------------------------------------------------------------------------------------------------*/
  2624.  
  2625. long  only_initialized(digitsString** stckPointerReference_HEAD) {
  2626.  
  2627.  
  2628.  
  2629.   if ( 0 != ( NULL != stckPointerReference_HEAD ) ) {
  2630.     if ( 0 != ( NULL != *stckPointerReference_HEAD ) ) {
  2631.       if ( 0 != ( NULL == (*stckPointerReference_HEAD)->nextString ) ) {
  2632.     if ( -1 == (*stckPointerReference_HEAD)->integer_value ) {
  2633.       return 1;
  2634.     }
  2635.       }
  2636.     }
  2637.   }
  2638.  
  2639.   return 0;
  2640.  
  2641. }
  2642.  
  2643. /*------------------------------------------------------------------------------------------------------------------------------*/
  2644.  
  2645. long absolute_value(long argument) {
  2646.  
  2647.   return ( argument * LeibnitzLaplaceSignature(argument) );
  2648.  
  2649. }
  2650.  
  2651. /*------------------------------------------------------------------------------------------------------------------------------*/
  2652.  
  2653. digitsString* insert_new_node(long value_assigned, digitsString **stckPointerReference_TAIL) {
  2654.  
  2655.   digitsString *focus;
  2656.   digitsString *inserto = (digitsString *)( malloc( sizeof( digitsString ) ) );
  2657.  
  2658.   digitsString *answer_ref = inserto;
  2659.   inserto->integer_value  = value_assigned;
  2660.   inserto->nextString = NULL;
  2661.  
  2662.   if (( NULL == (*stckPointerReference_TAIL) ) && (-1 == value_assigned)) {
  2663.  
  2664.     (*stckPointerReference_TAIL) = inserto;
  2665.  
  2666.   } else if ( NULL != (*stckPointerReference_TAIL) ) {
  2667.  
  2668.       focus = (*stckPointerReference_TAIL);
  2669.       while ( NULL != focus->nextString ) { focus = focus->nextString; }
  2670.       focus->nextString = inserto;
  2671.       if ( focus == (*stckPointerReference_TAIL) ) { (*stckPointerReference_TAIL) = inserto; }
  2672.  
  2673.   }
  2674.  
  2675.   return answer_ref;
  2676. }
  2677.  
  2678. /*------------------------------------------------------------------------------------------------------------------------------*/
  2679.  
  2680. void close__the_ring(digitsString** stckPointerReference_HEAD, digitsString** stckPointerReference_TAIL) {
  2681.  
  2682.   __theRing_HEAD = *stckPointerReference_HEAD;
  2683.   __theRing_TAIL   = *stckPointerReference_TAIL;
  2684.   (*stckPointerReference_TAIL)->nextString = *stckPointerReference_HEAD;
  2685.  
  2686. }
  2687.  
  2688. /*------------------------------------------------------------------------------------------------------------------------------*/
  2689.  
  2690. void open_the_ring(digitsString** stckPointerReference_HEAD, digitsString** stckPointerReference_TAIL) {
  2691.  
  2692.   (*stckPointerReference_HEAD)       = __theRing_HEAD;
  2693.   (*stckPointerReference_TAIL)         = __theRing_TAIL;
  2694.   (*stckPointerReference_TAIL)->nextString = NULL;
  2695.  
  2696. }
  2697.  
  2698. /*------------------------------------------------------------------------------------------------------------------------------*/
  2699.  
  2700. long LeibnitzLaplaceSignature(long argument) {
  2701.  
  2702.   long answer;
  2703.  
  2704.   answer = (argument < 0) ? (-1) : (1) ;
  2705.  
  2706.   return answer;
  2707.  
  2708. }
  2709.  
  2710. /*
  2711.  *
  2712. --------------------------------------------------------------------------------------------------------------------------------
  2713.  
  2714.   Sample unix shell command for compiling this sourcecode in Linux and profiling its memory usage with GNU memusage:
  2715.  
  2716. clear; cd /storage; g++ -x c -O2 -Wall -pedantic -lm -o ./seqNpadfix ./seqNpad_tails_fix.c && (echo "Compiled successfuly. Press ENTER.";read;clear; memusage ./seqNpad)
  2717.  
  2718. --------------------------------------------------------------------------------------------------------------------------------
  2719.  
  2720. ====================================================> APPENDIX <================================================================
  2721.  
  2722. Note: C comment delimiters were replaced with />* for opening, and *</ for closing. After placing the following code in a separate
  2723. file, retrieve the proper comment delimiters before compiling.
  2724.  
  2725. ---[ The following is a dump of the file: ClangHackingCanoExperiment001.c ]-------------------------------------------------------
  2726. ==================================================================================================================================
  2727.  
  2728. />*
  2729.  *
  2730.  * -rw-r--r-- 1 KirkJamesT Enterprise 4161 Oct 21 22:54 ClangHackingCanoExperiment001.c
  2731.  *
  2732.  * Experimental program about exploring the possibilities of storagement for memory addresses of
  2733.  * compound datatype objects. An affirmative answer obtained here for the author's hytpothesis on
  2734.  * the use of pointers would let to optimize the concatenation process implemented as part of
  2735.  * the author's C code for C.A.R.'s Quicksort.
  2736.  *
  2737.  *</
  2738.  
  2739. #include <stdio.h>
  2740. #include <stdlib.h>
  2741. #include <string.h>
  2742.  
  2743. typedef struct userDefinedDataType { char* textString; struct userDefinedDataType* link; } userDefinedDataType;
  2744.  
  2745. char* EmptyField= "Empty!";
  2746.  
  2747. int   main(void);
  2748. int   value(long);
  2749. char  evalSymbol(int, int);
  2750. char* printStringField(long);
  2751. int   runDemo(void);
  2752.  
  2753. int main(void) {
  2754.   return runDemo();
  2755. }
  2756.  
  2757. />* To be extremely careful about the value passed here will prevent painful crashes with "Segmentation fault" *</
  2758.  
  2759. int value(long addr) {
  2760.  
  2761.   int ans= 0;
  2762.  
  2763.     if ( ((userDefinedDataType**)(addr)) != NULL ) {
  2764.       if ( *((userDefinedDataType**)(addr)) != NULL ) {
  2765.     ans+= strlen((*((userDefinedDataType**)(addr)))->textString);
  2766.       } else {;}
  2767.     }
  2768.  
  2769.   return ans;
  2770. }
  2771.  
  2772. char evalSymbol(int arg1, int arg2) {
  2773.  
  2774.   char ans=' ';
  2775.  
  2776.   if (arg1 == arg2) { ans='='; }
  2777.   if (arg1 >  arg2) { ans='>'; }
  2778.   if (arg1 <  arg2) { ans='<'; }
  2779.  
  2780.   return ans;
  2781. }
  2782.  
  2783. char* printStringField(long atAddr) {
  2784.  
  2785.   char* ans= EmptyField;
  2786.   if ( ((userDefinedDataType**)(atAddr)) != NULL ) {
  2787.     if ( *((userDefinedDataType**)(atAddr)) != NULL ) {
  2788.       ans= (*((userDefinedDataType**)(atAddr)))->textString;
  2789.     } else {
  2790.       ;
  2791.     }
  2792.   } else {
  2793.     ;
  2794.   }
  2795.  
  2796.   return ans;
  2797. }
  2798.  
  2799. int runDemo(void) {
  2800.  
  2801.   long addr[]=             {0, 0, 0};
  2802.   long operand[]=          {0, 0, 0};
  2803.   int  howMany=             3;
  2804.   int  index_i=             0;
  2805.   int  index_j=             0;
  2806.   int  index_k=             0;
  2807.  
  2808.   int  UsefulPieceOf_A207324[]= {1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3};
  2809.  
  2810.   userDefinedDataType* testField00= NULL;
  2811.   userDefinedDataType* testField01= NULL;
  2812.   userDefinedDataType* testField02= NULL;
  2813.  
  2814.   testField00= (userDefinedDataType*) malloc(sizeof(userDefinedDataType));
  2815.   testField00->textString= "\"Zero\"";
  2816.   testField00->link= NULL;
  2817.  
  2818.   testField02= (userDefinedDataType*) malloc(sizeof(userDefinedDataType));
  2819.   testField02->textString= "\"Two \"";
  2820.   testField02->link= NULL;
  2821.  
  2822.   addr[0]= (long) &testField00;
  2823.   addr[1]= (long) &testField01;
  2824.   addr[2]= (long) &testField02;
  2825.  
  2826.   for (index_j=0; index_j <18; index_j++) {
  2827.     UsefulPieceOf_A207324[index_j]--;
  2828.   }
  2829.  
  2830.   printf("\n");
  2831.  
  2832.   printf("\nGiven the following character strings:\n");
  2833.  
  2834.   for (index_i=0; (index_i < howMany); index_i++) {
  2835.     if ( ((userDefinedDataType**)(addr[index_i])) != NULL ) {
  2836.       if ( *((userDefinedDataType**)(addr[index_i])) != NULL ) {
  2837.     printf("\n%s", (*((userDefinedDataType**)(addr[index_i])))->textString);
  2838.       } else {
  2839.     printf("\n%s", EmptyField);
  2840.       }
  2841.     }
  2842.   }
  2843.  
  2844.   printf("\n\nThere are six different ways of listing them:\n");
  2845.  
  2846.   index_j=-1;
  2847.  
  2848.   for (index_i=0; index_i < 6; index_i++) {
  2849.  
  2850.     for (index_k=0; index_k < howMany; index_k++) {
  2851.      
  2852.       operand[index_k]=  addr[UsefulPieceOf_A207324[++index_j]];
  2853.      
  2854.     }
  2855.  
  2856.     printf("\n %s %c %s %c %s", printStringField(operand[0]), evalSymbol(value(operand[0]),value(operand[1])), printStringField(operand[1]), evalSymbol(value(operand[1]),value(operand[2])),printStringField(operand[2]));
  2857.  
  2858.   }
  2859.  
  2860.   printf("\n\nThe property compared here is the lenght in chars of each string field \namong those belonging to declared copies of \"userDefinedDataType\".");
  2861.  
  2862. />*
  2863.  
  2864.     The same set of comparisons is got via a jungle of if/else statements.
  2865.  
  2866.     Using the information from A207324, specifically the terms correspondig to permutations for three objects
  2867.     (the permutations for the first 3 naturals in the A207324 context), we will be able to compare string lengths
  2868.     as shown below.
  2869.  
  2870.     In any case, since this code was devised for demonstrate the use of A207324, it would just a first glance.
  2871.    
  2872.  *</
  2873.  
  2874.   printf("\n\n");
  2875.  
  2876.   if (testField00 != NULL) { free(testField00); }
  2877.   if (testField01 != NULL) { free(testField01); }
  2878.   if (testField02 != NULL) { free(testField02); }
  2879.  
  2880. />*
  2881.     This code may be taken as evidence of the fact that under the proper assumptions and when enough
  2882.     security measures are implemented, it is safe to work directly with the memory addresses.
  2883.    
  2884.     The author hopes that such behavior of any pointer having the same sizeof() than long still being
  2885.     true in another architectures distinct than Intel 32 Bits, else this code might fail.
  2886.  *</
  2887.  
  2888.   return EXIT_SUCCESS;
  2889.  
  2890. }
  2891.  
  2892. =====(EOF)=====================================================================================================================
  2893.  
  2894. *
  2895. *  END of seqNpad_tails_fix.c (or a217626.c.txt)
  2896. *
  2897. *
  2898. *
  2899. *       << If people do not believe that mathematics is simple, it is only because
  2900. *           they do not realize how complicated life is. >>
  2901. *
  2902. *                   --John von Neumann
Add Comment
Please, Sign In to add comment