Misipuk

KP_4

Dec 9th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define BUFF 256
  4.  
  5. typedef struct grd{
  6.     char * title;
  7.     char * color;
  8.     int count;
  9.     struct drd *next;
  10. } ngrd;
  11.  
  12. ngrd * create_node();
  13. int free_list(ngrd *);
  14. void print_list(ngrd*);
  15. ngrd * add_inorder(ngrd *, ngrd *);
  16. char * input_string(char * message);
  17. int * input_digit(char *);
  18. int my_cmp(ngrd * , ngrd * );
  19.  
  20. int main()
  21. {
  22.     int n = 3;
  23.     ngrd * head = create_node();
  24.     ngrd * temp;
  25.     for(int i=0; i<2; i++){
  26.         temp = create_node();
  27.         head = add_inorder(head, temp);
  28.     }
  29.     print_list(head);
  30.     printf("FREE %d\n", free_list(head));
  31.     return 0;
  32. }
  33.  
  34. char * input_string(char * message){
  35.     char * buff = (char *) malloc(BUFF*sizeof(char));
  36.     printf("input %s:\t", message);
  37.     scanf("%s", buff);
  38.     char * result = (char *)malloc((strlen(buff)+1)*sizeof(char));///zap 0
  39.     strcpy(result, buff);/// если прировнять и free, то будет призрак Чубаки
  40.     free(buff);
  41.     return result;
  42. }
  43.  
  44. ngrd * create_node(){
  45.     ngrd * result=(ngrd *)malloc(sizeof(ngrd));
  46.     if(!result){
  47.         printf("ERROR.CREATE NODE: Bad Allocation!\n");
  48.         exit(1);
  49.     }
  50.     result->title = input_string("TITLE");
  51.     result->color= input_string("COLOR");
  52.     result->count= input_digit("DIGITAL");
  53.     result->next=0;
  54.     return result;
  55. }
  56.  
  57. int * input_digit(char * m){
  58.     int result = 0;
  59.     do{
  60.         do{
  61.             fflush(stdin);
  62.             printf("Input %s\t", m);
  63.         }while(scanf("%d", &result)!=1);
  64.     }while(result<0);
  65.     return result;
  66. }
  67.  
  68. int free_list(ngrd * h){
  69.     int count = 0;
  70.     while(h){
  71.         ngrd * t = h->next;
  72.         free(h->color);
  73.         free(h->title);
  74.         free(h);
  75.         h=t;
  76.         count++;///vnutr ukaz osvobod, a potom sam ukaz i vse ost
  77.     }
  78. }
  79.  
  80. void print_list(ngrd * h){
  81.     while(h){
  82.         printf("\nTITLE:\t%s", h->title);
  83.         printf("\n%s(%d)", h->color, h->count);
  84.         h = h->next;
  85.     }
  86. }
  87.  
  88. ngrd * add_inorder(ngrd * h, ngrd * x){
  89.     if(!my_cmp(h,x)){///v nachalo
  90.         x->next = h;
  91.         return x;
  92.     }
  93.     ngrd * t = h;
  94.  
  95.     while(t->next){///v hvost
  96.         t=t->next;
  97.     }
  98.     if(my_cmp(t,x)){
  99.         t->next = x;
  100.     }
  101.  
  102.     while(t->next && my_cmp(t->next,x)){/// obshiy
  103.         t=t->next;
  104.     }
  105.     x->next = t->next;
  106.     t->next = x;
  107.     return h;
  108. }
  109.  
  110. int my_cmp(ngrd * a, ngrd * b){/// a<b
  111.     if (strcmp(a->title,b->title)<0) return 1;
  112.     else if(strcmp(a->title==0,b->title)==0){
  113.         if(strcmp(a->color,b->color)<0) return 1;
  114.     }
  115.     return 0;
  116. }
Add Comment
Please, Sign In to add comment