알고리즘과 데이터 구조(c언어)-선형 단일 체인 테이블 기본 조작

#include 
#include 

typedef int Element;
typedef char(*Status)[10];

#define ERROR "error"
#define OK "ok";

//            
typedef struct Node{
    Element data;
    struct Node *next;
}Node,*LinkedList;

//           
LinkedList init(){
    Node *l;
    l = (Node *)malloc(sizeof(Node));
    l->next = NULL;

    return l;
}

//          
void create(LinkedList l){
    for(int i = 2; i < 5; i++){
        LinkedList p;
        p = (LinkedList)malloc(sizeof(Node));
        p->data = i;
        p->next = l->next;
        l->next = p;
    }
}

//            (   )
void insertHead(LinkedList l,Element e){
        LinkedList p;
        p = (LinkedList)malloc(sizeof(Node));
        p->data = e;
        p->next = l->next;
        l->next = p;
}

//            (   )
void insertTail(LinkedList L,Element e){
    LinkedList p,r;
    //r        ,        
    r = L;

    //                
    while(r->next){
        r = r->next;
    }
    //      
    p = (LinkedList)malloc(sizeof(Node));
    p->data = e;

    //                
    r->next = p;

    //                 
    r = p;

    //        
    r->next = NULL;
}

//                 (   0  )
Status LinkedListGet(LinkedList l,int i,Element *e){
    LinkedList p = l->next;
    int j = 0;
    while(p&&jnext;
        j++;
    }

    if(!p||j>i){
        return ERROR;
    }
    *e = p->data;
    return OK;
}

//               
Status LinkedListInsert(LinkedList L,int index,Element e){
    LinkedList pre,p;                      //pre     
    pre = L->next;
    int tempi = 1;

    while(pre&&tempinext;
        ++tempi;
    }
    if(!pre||tempi>index){
        //  index      
        return ERROR;
    }
    p = (LinkedList)malloc(sizeof(Node));
    p->data = e;
    p->next = pre->next;
    pre->next = p;

    return OK;
}

//               
Status LinkedListRemove(LinkedList L,int index,Element *e){

    LinkedList pre,p;
    //pre    

    pre = L->next;
    int tempi = 1;

    //       i-1   
    while(pre&&tempinext;
        ++tempi;
    }

    if(!(pre->next)||tempi>index){
        //  index      
        return ERROR;
    }
    p = pre->next;
    pre->next = p->next;
    *e = p->data;
    // p       
    free(p);

    return OK;
}

//             ,            (        ),     NULL;
Node * LinkedListIndexOf(LinkedList L,Element e){
    LinkedList p;
    if(!L){
        return ERROR;
    }
    p = L;
    while(p&&p->data!=e){
        p = p->next;
    }
    return p;
}

//      
Status LinkedListClear(LinkedList L){
    LinkedList p,q;
    p = L->next;
    while(p){
        q = p->next;
        free(p);
        p = q;
    }
    //            。
    L->next = NULL;
    return OK;
}


//                
void toString(LinkedList l){
    printf("[");
    l = l->next;
    while(l){
        printf("%d",l->data);
        l=l->next;
        if(l){
            printf(",");
        }
    }
    printf("]
"); } int main(){ LinkedList L = init(); create(L); //insertHead(L,5); insertTail(L,1); Element a = -1; // Element *a = &1; , ,c 。 // Element *a = 1; *a 1。 // ! ! !( , 0, )。 // , ( ) ! , ( ), // ( ), ! Element *intPoint=&a; // *intPoint=a; :int *intPoint; *intPoint = a; // , , // 0 ( 0 ) Status res = LinkedListGet(L,0,intPoint); printf("Get() res is :%s,element is %d
",res,*intPoint); LinkedListInsert(L,2,5); res = LinkedListRemove(L,3,intPoint); printf("remove status :%s,remove Element is %d
",res,*intPoint); Node *indexRes = LinkedListIndexOf(L,7); printf("..index Memory address :%d
",indexRes); //LinkedListClear(L); toString(L); return 0; }

부족한 점과 잘못된 점을 많이 지적하여 주시기 바랍니다. 감사합니다.

좋은 웹페이지 즐겨찾기