Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define BUFF 256
- typedef struct grd{
- char * title;
- char * color;
- int count;
- struct drd *next;
- } ngrd;
- ngrd * create_node();
- int free_list(ngrd *);
- void print_list(ngrd*);
- ngrd * add_inorder(ngrd *, ngrd *);
- char * input_string(char * message);
- int * input_digit(char *);
- int my_cmp(ngrd * , ngrd * );
- int main()
- {
- int n = 3;
- ngrd * head = create_node();
- ngrd * temp;
- for(int i=0; i<2; i++){
- temp = create_node();
- head = add_inorder(head, temp);
- }
- print_list(head);
- printf("FREE %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;
- }
- ngrd * create_node(){
- ngrd * result=(ngrd *)malloc(sizeof(ngrd));
- if(!result){
- printf("ERROR.CREATE NODE: Bad Allocation!\n");
- exit(1);
- }
- result->title = input_string("TITLE");
- result->color= input_string("COLOR");
- result->count= input_digit("DIGITAL");
- 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(ngrd * h){
- int count = 0;
- while(h){
- ngrd * t = h->next;
- free(h->color);
- free(h->title);
- free(h);
- h=t;
- count++;///vnutr ukaz osvobod, a potom sam ukaz i vse ost
- }
- }
- void print_list(ngrd * h){
- while(h){
- printf("\nTITLE:\t%s", h->title);
- printf("\n%s(%d)", h->color, h->count);
- h = h->next;
- }
- }
- ngrd * add_inorder(ngrd * h, ngrd * x){
- if(!my_cmp(h,x)){///v nachalo
- x->next = h;
- return x;
- }
- ngrd * t = h;
- while(t->next){///v hvost
- t=t->next;
- }
- if(my_cmp(t,x)){
- t->next = x;
- }
- while(t->next && my_cmp(t->next,x)){/// obshiy
- t=t->next;
- }
- x->next = t->next;
- t->next = x;
- return h;
- }
- int my_cmp(ngrd * a, ngrd * b){/// a<b
- if (strcmp(a->title,b->title)<0) return 1;
- else if(strcmp(a->title==0,b->title)==0){
- if(strcmp(a->color,b->color)<0) return 1;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment