2010年10月21日 星期四

101



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum __bool {
    false = 0,
    true = 1,
} bool;

bool debug=false;
bool debug_searchNode=false;
struct Node{
    int  n;
    struct Node *next;
};

struct Node **input_array;

struct Node *createNode(int n){
    struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
    temp->n=n;
    temp->next=NULL;
    return temp;
}

void initial(int n){
    int i;
    for(i=0;i<n;i++){
        input_array[i] = createNode(-1);
        input_array[i]->next = createNode(i);
    }
}

void printArray(int n){
    int i=0;
    for(i=0;i<n;i++){
        struct Node *temp;
        temp = input_array[i]->next;
        printf("%d:",i);
        while(temp!=NULL){
            printf(" %d",temp->n);
            temp = temp->next;
        };
        printf("\n");
    }
}

void returns(struct Node* p){
    if(debug) {
        printf("returns ");
        if(p)printf(" %d\n",p->n);
    }
    /*printf("returns");*/  
    struct Node *temp = p->next;  
    p->next=NULL;
    p=temp;
      
    while(p!=NULL){
        int n = p->n;
        if(debug)printf("p->n %d\n",p->n);
        temp = p->next;
        if(debug)if(input_array[n]==NULL)printf("input_array[n]==NULL");
        p->next = input_array[n]->next;
        input_array[n]->next = p;
        if(debug)if(p->next)printf("p->next->n %d\n",p->next->n);
        p=temp;
    }
    if(debug) {
        printf("returns end");
    }
}
/*
search the node which value is n
total_n: array size
int n  : the value for search
int an : where it is position at input_array
return : the node before the value node
*/
struct Node* searchNode(int total_n,int n,int *an){
    if(debug) printf("searchNode begin\n");
    if(debug) printf("the value for search %d\n",n);
    int i;
    for(i=0;i<total_n;i++){
        struct Node *temp,*temp_before;
        temp_before = temp = input_array[i];
        /*if(debug)printf("i = %d temp->n = %d\n",i,temp->n);*/
        *an=i;  
        while(temp!=NULL){
            if(temp->n==n){              
                if(debug)printf("target = %d,position %d\n",n,*an);
                if(debug)printf("searchNode end\n");
                return temp_before;
            }
            temp_before = temp;
            temp = temp->next;          
        };      
    }
    if(debug) printf("searchNode return NULL \n");
    return NULL;
}

/*
when move comment,remove node from original position
return : the target node to move
*/
struct Node* removeByMove(struct Node* p){
    if(debug) printf("removeByMove begin\n");
    if(p==NULL){
        printf("p==NULL");
        return NULL;
    }
    struct Node *target = p->next;
  
    if(target==NULL)printf("target==NULL");
    p->next = target->next;
    if(debug) printf("removeByMove end\n");
    return target;
}

/*
when pile comment,remove node from original position
return : the target node to move
*/
struct Node* removeByPile(struct Node* p){
    if(p==NULL)if(debug)printf("p==NULL");
    struct Node *target = p->next;
  
    if(target==NULL)if(debug)printf("target==NULL");
    p->next = NULL;
    return target;
}

/*
move onto comment
*/
void moveOnto(struct Node* a,struct Node* b){

    /*after returning any blocks that are stacked on top of blocks a and b to their initial positions.*/
    /*
    struct Node* temp = a->next;
    while(temp!=NULL && temp->next!=NULL) temp=temp->next;
    returns(temp);
  
    temp = b->next;
    while(temp!=NULL && temp->next!=NULL) temp=temp->next;
    returns(temp);*/
  
  
    a->next = b->next;
    b->next = a;
}

/*
move over comment
*/
void moveOver(struct Node* a,struct Node* b){
    /*printf("a->n %d, b->n %d",a->n,b->n);*/
    /*
    struct Node* temp = a->next;
    while(temp!=NULL && temp->next!=NULL) temp=temp->next;
    returns(temp);*/
  
  
  
    while(b->next!=NULL){
        b=b->next;
    };
    b->next = a;
}

/*
pile onto comment
*/
void pileOnto(struct Node* a,struct Node* b){
    if(debug) printf("pileOnto begin\n");
    struct Node* b_next=b->next;
    b->next = a;
    while(a->next!=NULL) a=a->next;
    a->next=b_next;
    if(debug) printf("pileOnto end\n");
}

/*
pile over comment
*/
void pileOver(struct Node* a,struct Node* b){  
    if(debug) printf("pileOver begin\n");
    if(debug) printf("a:%d,b:%d\n",a->n,b->n);
    while(b->next!=NULL) {
        b=b->next;
        if(debug)    printf("b:%d",b->n);
    }
    b->next = a;
    if(debug) printf("pileOver end\n");
}

void freeArray(int n){
    if(debug) printf("freeArray begin\n");
    int i=0;
    for(i=0;i<n;i++){
        if(debug)printf("i %d\n",i);
        struct Node *temp,*tempFree;
        temp = input_array[i]->next;
        if(debug)printf("input_array[i] %d\n",input_array[i]->n);
        free(input_array[i]);
        if(debug)printf("free input_array %d\n",i);
        while(temp!=NULL){
            if(debug)printf("temp!=NULL\n");
            tempFree = temp;    
            if(debug)printf("tempFree = temp\n");  
            temp = temp->next;          
            if(debug)printf("temp = temp->next\n");
            free(tempFree);
            if(debug)printf("free(tempFree)\n");
        };      
    }
    free(input_array);
    if(debug) printf("freeArray end\n");
}
/*
void analizyComment(char *comment, char *comment_,int *a,int *b){

    char temp1[5],temp2[5];
    int ia,ib;
    sscanf (comment,"%s %d %s %d",temp1,a,temp2,b);

    strcat(temp1,temp2);
    strcpy(comment_,temp1);//死在這一行
    printf("%s %d %s %d\n",temp1,*a,temp2,*b);
}*/
int main(int argc, char *argv[])
{
    int input_n;
    scanf("%d",&input_n);
    /*char readc = getchar();/*read newline of scanf*/
    /*input_array = (struct Node**)malloc(sizeof(struct Node*));*/
    input_array = (struct Node**)malloc(sizeof(struct Node*)*input_n);
    initial(input_n);
    /*printArray(input_n);*/
    char input_c[25];
    /*fgets ( input_c, 25, stdin );*/
    int aa,bb,nna,nnb;
    char comment_[9],temp2[5];
    /*analizyComment(input_c,comment_,&aa,&bb);*/
    /*char quit[5]="quit";
  
    //do{*/
  
  
    while(scanf("%s",comment_))
    /*while(scanf("%s", comment_) && comment_[0] != 'q')*/
    /*while(cin>>comment_)*/
    {
        if(comment_[0] == 'q') break;
        scanf("%d %s %d",&aa,temp2,&bb);
        /*
        if(comment_=="quit") break;
        cin>>aa>>temp2>>bb;*/

        /*
        scanf("%d", &aa);
        scanf("%s", temp2);
        scanf("%d", &bb);*/
         /*//fscanf("%s",input_c);          
         //gets(input_c);        
         */
        
         if(debug)printf("debug:%s %d %s %d\n",comment_,aa,temp2,bb);
         struct Node *aNode,*bNode;
         /*//analizyComment(input_c,comment_,aa,bb);
         //sscanf (input_c,"%s %d %s %d",comment_,&aa,temp2,&bb);
         //strcat(comment_,temp2);*/
         aNode = searchNode(input_n,aa,&nna);              
         bNode = searchNode(input_n,bb,&nnb);
         /*//printf("nna %d,nnb %d",nna,nnb);*/
         if(nna==nnb){
            continue;
         }
         if(strcmp(comment_,"move")==0){
            returns(aNode->next);
            aNode = removeByMove(aNode);
            if(strcmp(temp2,"over")==0){
                                              
                moveOver(aNode,bNode->next);
            }else if(strcmp(temp2,"onto")==0){
                returns(bNode->next);
                moveOnto(aNode,bNode->next);
            }
         }else if(strcmp(comment_,"pile")==0){
            aNode = removeByPile(aNode);
            if(strcmp(temp2,"over")==0){  
                                          
                pileOver(aNode,bNode->next);
            }else if(strcmp(temp2,"onto")==0){
                returns(bNode->next);
                pileOnto(aNode,bNode->next);
            }
         }
         /*printArray(input_n);      
         /*fgets ( input_c, 25, stdin );*/
         /*sleep(2000);*/
         /*printf("%s %d\n",input_c,strcmp(input_c,"quit"));*/
    };
    /*//while(strcmp(input_c,quit)!=0);*/
        
    /*//printf("%d,%s",input_n,input_c);*/
    printArray(input_n);  
    freeArray(input_n);
    /*system("PAUSE");*/
 
    return 0;
}

沒有留言:

張貼留言