close

include、public variable


#include <stdio.h>
#include <stdlib.h>
#define RANGE 3
struct student{
        int sdata[3];
        struct student *link;
};


Main:


int main(int argc, char *argv[]){
    struct student *head[10] = {NULL},*ptr;
  int ch = 0;
  do{
     printf("input your choice(1 to 6)\n1)add node\n2)del node\n3)search node\n4)show node\n5)insert node\n6)bye\n");
     scanf("%d",&ch);
     switch(ch){
         case 1:
              printf("\n=====you choice add student data=====\n");
              add_node(head);
              break;
         case 2:
              printf("\n=====you choice del student data=====\n");
              del_node(head,ptr);
              break;
         case 3:
              printf("\n====you choice search student data=====\n");
              sr_node(head,ptr);
              break;
         case 4:
              printf("\n=====you choice show student data=====\n");
              show_node(head,ptr);
              break;
        case 5:
            printf("\n=====insert data =====\n");
            insert(head,ptr);
            break;
         case 6:
              printf("\n=====bye=====\n");
              break;
         default:
              printf("\n=====please input num(1 to 6)=====\n");
              break;
      }
  }while(ch!=6);
  return 0;
}

 

add data:




void add_node(struct student **h){
    struct student *n, *p;
    int *rdata  =  0, i = 0;
    rdata = input_data();
    //printf("%d %d %d\n",rdata[0],rdata[1],rdata[2]);
    int hash_h = hash_fun(rdata[0]);
    if (!check(h,p,rdata[0])){


        n = ( struct student *) malloc( sizeof( struct student));
        for (i = 0; i < RANGE; i++) {
            n->sdata[i] = rdata[i];
        }
                n->link = NULL;


        p = h[hash_h];
                //add at head
        if (p == NULL){
            h[hash_h] = n;
                        //add link to NULL
        } else {
                        //add at tail
            while(p->link != NULL){
                p = p->link;
            }
            p->link = n;
                        //add link to NULL
        }
    } else {
         printf("num is deuplex\n");
    }
}

Delete data


void del_node(struct student **h,struct student *p){


     int del_v = 0,get = 0,node_count = 0,round = 0;
     struct student *ptr2;
     printf("input delete num!!\n");
     scanf ("%d", &del_v);
     int hash_h = hash_fun( del_v);
     //not found
     if(!check( h , p , del_v )){
         printf ("The number= %d not found\n",del_v);
     }else{
                node_count = check_count (h,p,del_v);
                printf ("node_count= %d \n",node_count);
                //point the node to delete
        p = h[hash_h];
                while ( p->sdata[0] != del_v){
            p = p -> link;
        }
        //printf("p= %d \n",p->sdata[0]);
                if (node_count > 1){
                        printf ("node >1");
                        //use ptr2 point node of the front of delete node
                        ptr2 = h[hash_h];
                        //the node at the head of list
                        if( ptr2 -> sdata[0] == del_v && p -> sdata[0] == del_v){
                                h[hash_h] = p->link;
                        } else{
                while ( ptr2 -> link != p){
                                        ptr2 = ptr2 -> link;
                                }
                        }
                        printf ("ptr2= %d \n",ptr2->sdata[0]);
                        if( p -> link == NULL){
                        //if p(delete node) is end node
                                //printf ("delete node of tail node\n");
                                free ( p);
                                ptr2 -> link = NULL;
                } else{
                                //ptr2 link node of p link
                                //printf ("delete node of center node\n");
                                free ( p);
                                ptr2->link = p -> link;
                        }
                }else{
                        //the list only one node
                        //printf("node <1");
                        free(p);
                        h[hash_h] = NULL;
                }
     }
}

insert data


void insert(struct student **h,struct student *p){
    struct student *n,*ptr;
    int loc = 0,go = 0,i = 0,*rdata;
    n = (struct student *)malloc(sizeof(struct student));
    rdata = input_data();
    int hash_h = hash_fun(rdata[0]);
    if(!check(h,p,rdata[0])){
        for(i = 0; i<RANGE ; i++){
            n->sdata[i] = rdata[i];
        }
        do{
           printf ("input the loc (0 to %d)\n",check_count(h,p,rdata[0]));
           scanf ("%d", &loc);
        }while( loc > check_count(h,p,rdata[0]));
        p = h[hash_h];
        //insert head
        if(loc == 0)
        {
            h[hash_h] = n;
            n->link = p;
        }else{
            //insert in list
            while( go<loc ){
                go++;
                p = p -> link;
               }
            ptr = h[hash_h];
            while ( ptr->link != p){
                ptr = ptr->link;
            }
            ptr->link = n;
            n->link = p;
        }
    } else {
        printf("\nnumber is dumplex\n");
    }
}

search data


void sr_node(struct student **h,struct student *p){


    int sr_val = 0;
    printf("input the search num\n");
    scanf( "%d", &sr_val);
    int hash_h = hash_fun(sr_val);
     p = h[hash_h];
     while(p != NULL){
        if(p->sdata[0] == sr_val){
            printf( "The result=id %d %dcm %dkg\n",p->sdata[0],p->sdata[1],p->sdata[2]);
            break;
        }else{
            p = p -> link;
        }
     }
}

 

show data


void show_node(struct student **h,struct student *p){
    int i = 0;
    for(i = 0; i < 10 ; i++){
            printf("id:%d ",i);
            p = h[i];    /* 設定存取指標從頭開始 */
            //putchar('\n');
            while (p != NULL)
            {
                printf(" {%d %dcm %dkg}",p->sdata[0],p->sdata[1],p->sdata[2]);
                p = p -> link;    /* 將ptr移往下一元素 */
            }
            putchar('\n');
    }
}

feature funtion



int *input_data(){
    static int data[RANGE];
    printf("input student data(number height weight)");
    scanf("%d %d %d",&data[0],&data[1],&data[2]);
    return data;
}


int hash_fun(int val){
    return val % 10;
}


int check(struct student **h,struct student *p,int val){
    int re = 0;
    int    hash_h = hash_fun(val);
    p = h[hash_h];
    while(p != NULL)
    {
        if(p->sdata[0] == val){
            re = 1;
            break;
        }else{
            p = p->link;
        }
    }
    return re;
}
//for insert function
int check_count(struct student **h,struct student *p,int val){
    int count = 0;
    int hash_h = hash_fun(val);
    p = h[hash_h];
    while(p != NULL)
    {
         p = p->link;
         count++;
    }
    return count;
 }

 

compiler sh file


cc hash_list.c
./a.out

end

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 = = 的頭像
    = =

    逗點大的雨滴

    = = 發表在 痞客邦 留言(0) 人氣()