Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define BUFF 256
- typedef struct book_list{
- char * title;
- int year;
- struct book_list * next;
- } bookList;
- bookList * create_node();
- bookList * delete_head();
- int free_list(bookList *);
- void print_list(bookList *);
- bookList * add_inorder(bookList *, bookList *);
- char * input_string(char * message);
- int * input_digit(char *);
- int my_cmp(bookList * , bookList * );
- void print_phv(bookList *);
- bookList * clear_tail(bookList *);
- void year_group(bookList *);
- int main()
- {
- int n = 0;
- printf("n = ");
- scanf("%d", &n);
- bookList * head = create_node();
- bookList * temp;
- for(int i=0; i<n-1; i++){
- temp = create_node();
- head = add_inorder(head, temp);
- }
- printf("\n\nYour :");
- print_list(head);
- printf("\n");
- year_group(head);
- printf("\n\nBefore tail:");
- print_phv(head);
- printf("\n\nClear tail:");
- clear_tail(head);
- print_list(head);
- printf("\n\nFREE %d\n", free_list(head));
- return 0;
- }
- char * input_string(char * message){
- char * buff = (char *) malloc(BUFF*sizeof(char));
- printf("input %s:\t", message);
- scanf("%s", buff);
- char * result = (char *)malloc((strlen(buff)+1)*sizeof(char));///zap 0
- strcpy(result, buff);/// если прировнять и free, то будет призрак Чубаки
- free(buff);
- return result;
- }
- bookList * create_node(){
- bookList * result=(bookList *)malloc(sizeof(bookList));
- if(!result){
- printf("ERROR.CREATE NODE: Bad Allocation!\n");
- exit(1);
- }
- result->title = input_string("TITLE");
- result->year= input_digit("YEAR");
- result->next=0;
- return result;
- }
- int * input_digit(char * m){
- int result = 0;
- do{
- do{
- fflush(stdin);
- printf("Input %s\t", m);
- }while(scanf("%d", &result)!=1);
- }while(result<0);
- return result;
- }
- int free_list(bookList * h){
- int count = 0;
- bookList * t;
- while(h){
- t = h->next;
- free(h->title);
- free(h);
- h=t;
- count++;///vnutr ukaz osvobod, a potom sam ukaz i vse ost
- }
- return count;
- }
- void print_list(bookList * h){
- while(h){
- printf("\n%d: %s", h->year, h->title);
- h = h->next;
- }
- }
- bookList * add_inorder(bookList * h, bookList * x){
- if(!my_cmp(h,x)){///v nachalo
- x->next = h;
- return x;
- }
- bookList * t = h;
- while(t->next){///v hvost
- t=t->next;
- }
- if(my_cmp(t,x)){
- t->next = x;
- return h;
- }
- t=h;
- while(t->next && my_cmp(t->next,x)){/// obshiy
- t=t->next;
- }
- x->next = t->next;
- t->next = x;
- return h;
- }
- int my_cmp(bookList * a, bookList * b){/// a<b
- if ((a->year)>(b->year)) return 1;
- else if(a->year==b->year){
- if(strcmp(a->title,b->title)<0) return 1;
- }
- return 0;
- }
- bookList * delete_head(bookList * h){
- bookList * t = h->next;
- free(h->title);
- free(h);
- return t;
- }
- void print_phv(bookList * head){
- bookList * tail;
- bookList * t = head;
- bookList * pr = head;
- while(t->next!=0){
- pr = t;
- t = t->next;
- }
- printf("\n%d: %s", pr->year, pr->title);
- }
- bookList * clear_tail(bookList * head){
- bookList * x = head;
- bookList * temp = head;
- while((x->next)!=0){
- temp = x;
- x = x->next;
- }
- bookList * tail = x;
- temp->next = 0;
- free(tail);
- return head;
- }
- void year_group(bookList * head){
- int cnt=0, yr=0;
- yr=head->year;
- while(head!=0){
- if (head->year==yr){
- cnt++;
- }else{
- printf("\nCount of (%d) is %d", yr, cnt);
- yr = head->year;
- cnt=1;
- }
- head = head->next;
- }
- printf("\nCount of (%d) is %d", yr, cnt);
- }
Add Comment
Please, Sign In to add comment