Advertisement
Misipuk

data_st

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