Advertisement
Guest User

Homework

a guest
Oct 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define N 10
  5.  
  6. typedef struct list
  7. {
  8.     char *s;
  9.     struct list *next;
  10. } list;
  11.  
  12.  
  13. list * add_node(list *node, char *stringNew)
  14. {
  15.     list *newNode;
  16.     if (node == NULL)
  17.     {
  18.         newNode = malloc(sizeof(list));
  19.         newNode->s = stringNew;
  20.         newNode->next = NULL;
  21.         node = newNode;
  22.         return newNode;
  23.     }
  24.     else
  25.     {
  26.         if (strcmp(stringNew, node->s) > 0)
  27.         {
  28.             node->next = add_node(node->next, stringNew);
  29.         }
  30.         else
  31.         {
  32.             newNode = malloc(sizeof(list));
  33.             newNode->s = stringNew;
  34.             newNode->next = node;
  35.             node = newNode;
  36.             return newNode;
  37.         }
  38.     }
  39.     return node;
  40. }
  41.  
  42. void print_list (list *node)
  43. {
  44.     while (node != NULL)
  45.     {
  46.         printf("%s\n", node->s);
  47.         node = node->next;
  48.     }
  49. }
  50.  
  51. char* input_str(int *status)
  52. {
  53.     char symb, *str;
  54.     int lenStr = 0, borderFlag = N;
  55.     str = malloc(N);
  56.     symb = getchar();
  57.     while (symb != '.')
  58.     {
  59.         if (symb != '\n')
  60.         {
  61.             if (lenStr == borderFlag - 1)
  62.             {
  63.                 borderFlag = borderFlag + N;
  64.                 if (malloc(borderFlag) == NULL)
  65.                 {
  66.                     printf("You don't have enough memory\n");
  67.                     exit(0);
  68.                 }
  69.                 str = realloc(str, borderFlag);
  70.             }
  71.             str[lenStr] = symb;
  72.             lenStr++;
  73.         }
  74.         else
  75.         {
  76.             str[lenStr] = '\0';
  77.             return str;
  78.         }
  79.         symb = getchar();
  80.     }
  81.     *status = 1;
  82.     return NULL;
  83. }
  84.  
  85. list * free_list(list *spisok)
  86. {
  87.     list *nextNode;
  88.     while (spisok != NULL)
  89.     {
  90.         free(spisok->s);
  91.         nextNode = spisok->next;
  92.         free(spisok);
  93.         spisok = nextNode;
  94.     }
  95.     return spisok;
  96. }
  97.  
  98.  
  99.  
  100. int main(void)
  101. {
  102.     int status = 0;
  103.     char *string = NULL;
  104.     list *spisok = NULL;
  105.     int userRespond = 1;
  106.     while (userRespond == 1)
  107.     {
  108.         while (status == 0)
  109.         {
  110.             string = input_str(&status);
  111.             if (status == 0)
  112.             {
  113.                 spisok = add_node(spisok, string);
  114.             }
  115.         }
  116.         print_list(spisok);
  117.         printf("Do you want to create a new list?\n");
  118.         printf("0/1\n");
  119.         scanf("%d", &userRespond);
  120.         if (userRespond == 1)
  121.         {
  122.             spisok = free_list(spisok);
  123.             free(spisok);
  124.             status = 0;
  125.             spisok = NULL;
  126.         }
  127.        
  128.        
  129.     }
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement