Advertisement
Misipuk

Data_structures

Dec 5th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define BUFF 255
  5.  
  6. typedef struct book_list{
  7.     char * title;
  8.     int year;
  9.     struct bookList * next;
  10. } bookList;/// struct book_list = bookList
  11.  
  12. bookList * create_node(char *, int);
  13. void add(bookList *, bookList *);
  14. void clear_tail(bookList *);
  15. void print_list(bookList *);
  16. void print_phv(bookList *);
  17. char * input_string(char * message);
  18.  
  19. bookList * head_list;
  20.  
  21. int main(){
  22.     char * strarray[10];
  23.     int i=0, n=0;
  24.     printf("n = ");
  25.     scanf("%d", &n);
  26.     do{
  27.         strarray[i] = input_string("NAME");
  28.         i++;
  29.     }while(i<n);
  30.     head_list = create_node(strarray[0], 2020);
  31.     bookList * temp;
  32.     for(i=1; i<n; i++){
  33.         temp = (bookList *) malloc(sizeof(bookList));
  34.         temp->title = strarray[i];
  35.         temp->year = 2010-i;
  36.         add(head_list, temp);
  37.     }
  38.     print_list(head_list);
  39.     printf("\nPredposl:\n");
  40.     print_phv(head_list);
  41.   //  for(i=0; i<n; i++){
  42.   //      free(strarray[i]);///только то, что выделяли malloc
  43.   //  }
  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(char * title, int year){
  58.     bookList * result = (bookList *) malloc(sizeof(bookList));
  59.     result->title = title;
  60.     result->year = year;
  61.     result->next = 0;
  62.     return result;
  63. }
  64.  
  65. void add(bookList * head, bookList * x){
  66.     int k=0;
  67.     bookList * t = head;
  68.     bookList * temp = head;
  69.     bookList * max = head;
  70.     while(t->next!=0){
  71.         if(t->year>x->year){
  72.             max = t;
  73.         }
  74.         t = t->next;
  75.     }
  76.     if(head->year<x->year){
  77.             x->next = head;
  78.             head_list = x;
  79.             head = x;
  80.     }else{
  81.         temp = max->next;
  82.         max->next = x;
  83.         x->next = temp;
  84.     }
  85. }
  86.  
  87. void clear_tail(bookList * head){
  88.     bookList * x = head;
  89.     bookList * temp = head;
  90.     while((x->next)!=0){
  91.         temp = x;
  92.         x = x->next;
  93.     }
  94.     bookList * tail = x;
  95.     temp->next = 0;
  96.     free(tail);
  97. }
  98.  
  99. void print_list(bookList * head){
  100.     bookList * t = head;
  101.     while(t!=0){
  102.         printf("%s (%d)\n", t->title, t->year);
  103.         t = t->next;
  104.     }
  105. }
  106.  
  107. void print_phv(bookList * head){
  108.     bookList * tail;
  109.     bookList * t = head;
  110.     bookList * pr = head;
  111.     while(t->next!=0){
  112.         pr = t;
  113.         t = t->next;
  114.     }
  115.     printf("%s (%d)\n", pr->title, pr->year);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement