Misipuk

codeee_

Dec 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define BUFF 256
  4.  
  5. typedef struct book_list{
  6.     char * title;
  7.     int year;
  8.     struct book_list * next;
  9. } bookList;
  10.  
  11. bookList * create_node();
  12. bookList * delete_head();
  13. int free_list(bookList *);
  14. void print_list(bookList *);
  15. bookList * add_inorder(bookList *, bookList *);
  16. char * input_string(char * message);
  17. int * input_digit(char *);
  18. int my_cmp(bookList * , bookList * );
  19. void print_phv(bookList *);
  20. bookList *  clear_tail(bookList *);
  21. void year_group(bookList *);
  22.  
  23. int main()
  24. {
  25.     int n = 0;
  26.     printf("n = ");
  27.     scanf("%d", &n);
  28.     bookList * head = create_node();
  29.     bookList * temp;
  30.     for(int i=0; i<n-1; i++){
  31.         temp = create_node();
  32.         head = add_inorder(head, temp);
  33.     }
  34.     printf("\n\nYour :");
  35.     print_list(head);
  36.     printf("\n");
  37.     year_group(head);
  38.     printf("\n\nBefore tail:");
  39.     print_phv(head);
  40.     printf("\n\nClear tail:");
  41.     clear_tail(head);
  42.     print_list(head);
  43.     printf("\n\nFREE %d\n", free_list(head));
  44.     return 0;
  45. }
  46.  
  47. char * input_string(char * message){
  48.     char * buff = (char *) malloc(BUFF*sizeof(char));
  49.     printf("input %s:\t", message);
  50.     scanf("%s", buff);
  51.     char * result = (char *)malloc((strlen(buff)+1)*sizeof(char));///zap 0
  52.     strcpy(result, buff);/// если прировнять и free, то будет призрак Чубаки
  53.     free(buff);
  54.     return result;
  55. }
  56.  
  57. bookList * create_node(){
  58.     bookList * result=(bookList *)malloc(sizeof(bookList));
  59.     if(!result){
  60.         printf("ERROR.CREATE NODE: Bad Allocation!\n");
  61.         exit(1);
  62.     }
  63.     result->title = input_string("TITLE");
  64.     result->year= input_digit("YEAR");
  65.     result->next=0;
  66.     return result;
  67. }
  68.  
  69. int * input_digit(char * m){
  70.     int result = 0;
  71.     do{
  72.         do{
  73.             fflush(stdin);
  74.             printf("Input %s\t", m);
  75.         }while(scanf("%d", &result)!=1);
  76.     }while(result<0);
  77.     return result;
  78. }
  79.  
  80. int free_list(bookList * h){
  81.     int count = 0;
  82.     bookList * t;
  83.     while(h){
  84.         t = h->next;
  85.         free(h->title);
  86.         free(h);
  87.         h=t;
  88.         count++;///vnutr ukaz osvobod, a potom sam ukaz i vse ost
  89.     }
  90.     return count;
  91. }
  92.  
  93. void print_list(bookList * h){
  94.     while(h){
  95.         printf("\n%d: %s", h->year, h->title);
  96.         h = h->next;
  97.     }
  98. }
  99.  
  100. bookList * add_inorder(bookList * h, bookList * x){
  101.     if(!my_cmp(h,x)){///v nachalo
  102.         x->next = h;
  103.         return x;
  104.     }
  105.     bookList * t = h;
  106.  
  107.     while(t->next){///v hvost
  108.         t=t->next;
  109.     }
  110.     if(my_cmp(t,x)){
  111.         t->next = x;
  112.         return h;
  113.     }
  114.     t=h;
  115.     while(t->next && my_cmp(t->next,x)){/// obshiy
  116.         t=t->next;
  117.     }
  118.     x->next = t->next;
  119.     t->next = x;
  120.     return h;
  121. }
  122.  
  123. int my_cmp(bookList * a, bookList * b){/// a<b
  124.     if ((a->year)>(b->year)) return 1;
  125.     else if(a->year==b->year){
  126.         if(strcmp(a->title,b->title)<0) return 1;
  127.     }
  128.     return 0;
  129. }
  130.  
  131. bookList * delete_head(bookList * h){
  132.     bookList * t = h->next;
  133.     free(h->title);
  134.     free(h);
  135.     return t;
  136. }
  137.  
  138. void print_phv(bookList * head){
  139.     bookList * tail;
  140.     bookList * t = head;
  141.     bookList * pr = head;
  142.     while(t->next!=0){
  143.         pr = t;
  144.         t = t->next;
  145.     }
  146.      printf("\n%d: %s", pr->year, pr->title);
  147. }
  148.  
  149. bookList * clear_tail(bookList * head){
  150.     bookList * x = head;
  151.     bookList * temp = head;
  152.     while((x->next)!=0){
  153.         temp = x;
  154.         x = x->next;
  155.     }
  156.     bookList * tail = x;
  157.     temp->next = 0;
  158.     free(tail);
  159.     return head;
  160. }
  161.  
  162. void year_group(bookList * head){
  163.     int cnt=0, yr=0;
  164.     yr=head->year;
  165.     while(head!=0){
  166.         if (head->year==yr){
  167.             cnt++;
  168.         }else{
  169.             printf("\nCount of (%d) is %d", yr, cnt);
  170.             yr = head->year;
  171.             cnt=1;
  172.         }
  173.         head = head->next;
  174.     }
  175.     printf("\nCount of (%d) is %d", yr, cnt);
  176. }
Add Comment
Please, Sign In to add comment