Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define N 10
- typedef struct list
- {
- char *s;
- struct list *next;
- } list;
- list * add_node(list *node, char *stringNew)
- {
- list *newNode;
- if (node == NULL)
- {
- newNode = malloc(sizeof(list));
- newNode->s = stringNew;
- newNode->next = NULL;
- node = newNode;
- return newNode;
- }
- else
- {
- if (strcmp(stringNew, node->s) > 0)
- {
- node->next = add_node(node->next, stringNew);
- }
- else
- {
- newNode = malloc(sizeof(list));
- newNode->s = stringNew;
- newNode->next = node;
- node = newNode;
- return newNode;
- }
- }
- return node;
- }
- void print_list (list *node)
- {
- while (node != NULL)
- {
- printf("%s\n", node->s);
- node = node->next;
- }
- }
- char* input_str(int *status)
- {
- char symb, *str;
- int lenStr = 0, borderFlag = N;
- str = malloc(N);
- symb = getchar();
- while (symb != '.')
- {
- if (symb != '\n')
- {
- if (lenStr == borderFlag - 1)
- {
- borderFlag = borderFlag + N;
- if (malloc(borderFlag) == NULL)
- {
- printf("You don't have enough memory\n");
- exit(0);
- }
- str = realloc(str, borderFlag);
- }
- str[lenStr] = symb;
- lenStr++;
- }
- else
- {
- str[lenStr] = '\0';
- return str;
- }
- symb = getchar();
- }
- *status = 1;
- return NULL;
- }
- list * free_list(list *spisok)
- {
- list *nextNode;
- while (spisok != NULL)
- {
- free(spisok->s);
- nextNode = spisok->next;
- free(spisok);
- spisok = nextNode;
- }
- return spisok;
- }
- int main(void)
- {
- int status = 0;
- char *string = NULL;
- list *spisok = NULL;
- int userRespond = 1;
- while (userRespond == 1)
- {
- while (status == 0)
- {
- string = input_str(&status);
- if (status == 0)
- {
- spisok = add_node(spisok, string);
- }
- }
- print_list(spisok);
- printf("Do you want to create a new list?\n");
- printf("0/1\n");
- scanf("%d", &userRespond);
- if (userRespond == 1)
- {
- spisok = free_list(spisok);
- free(spisok);
- status = 0;
- spisok = NULL;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement