✔ 最佳答案
/* Not the best and there are rooms to optimize and should have more
error checking. Developed on Linux with gcc. */
#include
typedef struct C {
char ch;
struct C * next;
struct C * prev;
} cell;
cell *head=NULL;
char input[ 256 ];
void enqueue( char c )
{
cell *ptr = (cell *) malloc( sizeof( cell ));
cell *tmp;
ptr->ch = c;
if ( head == NULL ) {
head = ptr;
ptr->next = ptr;
ptr->prev = ptr;
} else {
tmp = head;
while ( tmp->next != head ) {
tmp = tmp->next;
}
tmp->next = ptr;
ptr->prev = tmp;
ptr->next = head;
head->prev = ptr;
}
}
int dequeue( int i )
{
cell * tmp=head;
if ( tmp == NULL ) {
return 0;
}
while ( --i != 0 ) {
tmp = tmp->next;
}
if ( tmp->ch == 'X' ) {
return 0;
}
if (( tmp == head ) && ( tmp->next == head )) {
head = NULL;
} else {
head = tmp->next;
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
}
(void) free( tmp );
return 1;
}
int is_done()
{
cell *tmp=head;
if ( tmp == NULL ) {
return 1;
} else if ( tmp->ch != 'X' ) {
return 0;
} else {
tmp = tmp->next;
while (( tmp->ch == 'X' ) && ( tmp != head )) {
tmp = tmp->next;
}
if ( tmp != head ) {
return 0;
} else {
return 1;
}
}
}
int check( int i )
{
while ( !is_done() && dequeue(i)) {
}
if ( is_done()) {
return 1;
} else {
return 0;
}
}
void reset()
{
int i;
cell *tmp, *ptr;
head->prev->next = NULL;
tmp = head->next;
while ( tmp != NULL ) {
ptr = tmp->next;
(void) free( tmp );
tmp = ptr;
}
head = NULL;
for ( i=0; i < strlen( input ); i++ ) {
enqueue( input[ i ]);
}
}
main()
{
int i;
printf("prompt: ");
scanf( "%s", input );
for ( i=0; i < strlen( input ); i++ ) {
enqueue( input[ i ]);
}
i = 1;
while ( !check(i) ) {
reset();
i++;
}
printf("%d\n", i);
}
2010-11-25 13:14:40 補充:
The first line should be:
#include
The output should look like:
prompt: OOXXOXXX
10
2010-11-25 13:29:39 補充:
#<include>
2010-11-25 13:31:07 補充:
I am giving up on this feature of removing special characters. Your account does not allow me to send you the source code so I can't send that to you. However, you should notice that the include line is missing stdio.h.
2010-11-25 13:53:53 補充:
If you are using windows, you will need to put a system("pause") statement towards the end of the main() function.