Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <string.h>
- typedef struct Elem {
- struct Elem *next;
- char *word;
- int lenght;
- }Elem;
- Elem *last;
- typedef Elem *SLL;
- SLL Insert(SLL list, char *w, int lenght);
- SLL Delete(Elem *l);
- int BubbleSort(SLL l);
- void Print(SLL list);
- int main() {
- SLL l = NULL; // init
- char ch, w[1024];
- unsigned int lenght = 0;
- do {
- scanf("%c", &ch);
- if (ch == ' ' || ch == 10 || ch == 13) {
- if (lenght == lenght) {
- w[lenght] = '\0';
- l = Insert(l, w, lenght);
- lenght = 0;
- }
- }
- else w[lenght++] = ch;
- } while (ch != 10 && ch != 13);
- while (BubbleSort(l));
- Print(l);
- while (!(l == NULL)) l = Delete(l);
- return 0;
- }
- SLL Insert(SLL l, char * w, int lenght) {
- Elem * elem = malloc(sizeof(Elem));
- lenght += 1;
- elem->word = malloc(lenght);
- strcpy(elem->word, w);
- elem->lenght = lenght;
- elem->next = NULL;
- if (l == NULL) {
- last = elem;
- return elem;
- }
- else {
- last->next = elem;
- last = elem;
- return l;
- }
- }
- SLL Delete(Elem * l) {
- Elem * elem = l->next;
- free(l->word);
- free(l);
- return elem;
- }
- int BubbleSort(SLL l) {
- Elem *elem;
- unsigned int rev = 0, lenght;
- char *w;
- while (elem = l->next) {
- if (elem->lenght < l->lenght) {
- w = l->word;
- l->word = elem->word;
- elem->word = w;
- lenght = l->lenght;
- l->lenght = elem->lenght;
- elem->lenght = lenght;
- rev++;
- }
- l = elem;
- }
- return rev;
- }
- void Print(SLL l) {
- if (l == NULL) return;
- printf("%s ", l->word);
- Print(l->next);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement