Programmer account notes (20)

xiaoxiao2021-03-06  102

Today, I told a question, and this question is the problem that the same color beads I have said in two-way linked lists. So it can be seen again that our programmer's test is inseparable from the list and pointer. The pointer is of course the most important, because the linked list is the pointer, must be familiar with the pointer. Let's see you: Read the following programs and C code, you should fill in the words of __ (n) __written in the corresponding column of the answer sheet. [Program Description] A bead having different colors numbered No. 0 to N-1 (the color of the bead color is represented by the n-granulus color by the alphabetic color, indicated by the input character string. In the ring, the two-piece beads are broken, and the continuously colored beads are removed from the breakpoint, and the other side of the breakpoint will take the resection of the colored beads in the counterclockwise direction. The sum is the number of grains of the beads for the breakpoint. Moving breakpoints, the number of beads that can be taken is endless. This program finds that the number of beads and breakpoints that can be taken away. The string is stored in a two-way linked list. For example, the string of 10-capped colors numbered 0-9 is "aaabbbadcc", corresponding to the list: Figure 20

If it is a breakpoint between the 2nd and 3 beads, a total of 6 beads can be taken, and the number of beads taken away. [Program] #include #include #include type {char d; struct node * fpt; / * Backrope pointer * / struct node * bpt; / * Turning pointer * /} node;

Node * building (char * s) / * Generate two-way circular list * / {node * p = null, * q; while (* s) {q = (node ​​*) malloc (sizeof (node)); Q -> ch = * S ; if (p = null) P = q -> fpt = q -> bt = q; Else {P -> BPT -> fpt = q; q -> fpt = p; q -> bpt = __ ( 1) __; __ (2) __;}} return (}} int count (node ​​* start, int mapn, int steel) * / {Int Color, C; Node * P COLOR = -1; c = 0; for (p = start; c

O? P -> fpt; p -> bpt) {

IF (color == -1) color = p -> ch;

ELSE IF (__ (3) __) Break;

C

}

Return?;

}

INT FIND (CHAR * S, INT * CUTPOS) / * Look for the number of breakpoints and particles with the maximum number of beads * /

{

INT I, C, CUT, MAXC = 0, 1EN = Strlen (s);

Node * P;

IF ((p = building (s)) = null) {* cu1tpos = -1; return -1;}

I = 0;

DO

{

C = count (p, 1en, 1);

C = C __ (4) __;

IF (c> maxc) {maxc = C; cut = i;}

__ (5) __;

i ;

WHILE (I

* cutpos = cut;

Return Maxc;

}

Void main () {

INT CUT, MAX;

Char s [120];

Scanf (,% s', s);

Max = find (s, & cut);

Printf ("Cut position =% D, Number =% D. / N", CUT, MAX);

}

However, in this question, I only want to establish the part of the establishment of the two-way linked list, and other algorithms are similar to the title of the last initial, everyone can do it. Also because I didn't tried the experience of establishing a two-way loop linked list, today we will not do it, but this is normal, because the algorithm established here is really uniform.

Node * building (char * s) / * Build two-way circular list * /

{

Node * p = null, * q;

While (* s)

{

Q = (node ​​*) malloc (sizeof (node));

Q -> CH = * S ;

IF (p = null) p = q -> fpt = q -> b t = q;

Else

{

P -> BPT -> fpt = q;

Q -> fpt = P;

Q -> BPT = __ (1) __;

__(2)__ ;

}

}

Return (Q)

}

Here is also a bPT pointing forward a node, and the FPT is a node pointing. Ok, continue to see the program

转载请注明原文地址:https://www.9cbs.com/read-124117.html

New Post(0)