Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define BUFF 255
- typedef struct book_list{
- char * title;
- int year;
- struct bookList * next;
- } bookList;/// struct book_list = bookList
- bookList * create_node(char *, int);
- void add(bookList *, bookList *);
- void clear_tail(bookList *);
- void print_list(bookList *);
- void print_phv(bookList *);
- char * input_string(char * message);
- bookList * head_list;
- int main(){
- char * strarray[10];
- int i=0, n=0;
- printf("n = ");
- scanf("%d", &n);
- do{
- strarray[i] = input_string("NAME");
- i++;
- }while(i<n);
- head_list = create_node(strarray[0], 2020);
- bookList * temp;
- for(i=1; i<n; i++){
- temp = (bookList *) malloc(sizeof(bookList));
- temp->title = strarray[i];
- temp->year = 2010-i;
- add(head_list, temp);
- }
- print_list(head_list);
- printf("\nPredposl:\n");
- print_phv(head_list);
- // for(i=0; i<n; i++){
- // free(strarray[i]);///только то, что выделяли malloc
- // }
- 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(char * title, int year){
- bookList * result = (bookList *) malloc(sizeof(bookList));
- result->title = title;
- result->year = year;
- result->next = 0;
- return result;
- }
- void add(bookList * head, bookList * x){
- int k=0;
- bookList * t = head;
- bookList * temp = head;
- bookList * max = head;
- while(t->next!=0){
- if(t->year>x->year){
- max = t;
- }
- t = t->next;
- }
- if(head->year<x->year){
- x->next = head;
- head_list = x;
- head = x;
- }else{
- temp = max->next;
- max->next = x;
- x->next = temp;
- }
- }
- void 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);
- }
- void print_list(bookList * head){
- bookList * t = head;
- while(t!=0){
- printf("%s (%d)\n", t->title, t->year);
- t = t->next;
- }
- }
- void print_phv(bookList * head){
- bookList * tail;
- bookList * t = head;
- bookList * pr = head;
- while(t->next!=0){
- pr = t;
- t = t->next;
- }
- printf("%s (%d)\n", pr->title, pr->year);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement