Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- struct WordList
- {
- char *word;
- struct WordList *next;
- };
- void loadData(struct WordList **wordListPointer)
- {
- char newWord[32];
- (*wordListPointer)->next = NULL;
- printf("Wprowadz wyraz: ");
- scanf("%s", newWord);
- (*wordListPointer)->word = (char*)malloc(sizeof(char) * strlen(newWord));
- strcpy((*wordListPointer)->word, newWord);
- }
- void addWord(struct WordList **headPointer)
- {
- struct WordList *pointer;
- pointer = *headPointer;
- if (pointer == NULL)
- {
- pointer = (struct WordList*)malloc(sizeof(struct WordList));
- loadData(&pointer);
- *headPointer = pointer;
- }
- else
- {
- while(pointer->next != NULL)
- {
- pointer = pointer->next;
- }
- pointer->next = (struct WordList*)malloc(sizeof(struct WordList));
- pointer = pointer->next;
- loadData(&pointer);
- }
- }
- char *convertToUpper(char *string)
- {
- int stringLength, i;
- char *newString;
- stringLength = strlen(string);
- newString = (char*)malloc(sizeof(char)*(stringLength + 1));
- for(i = 0; i < stringLength; i++)
- {
- newString[i] = toupper((int)string[i]);
- }
- return newString;
- }
- void findWords(struct WordList *headPointer, struct WordList **firstAlphabetically, struct WordList **lastAlphabetically)
- {
- *firstAlphabetically = NULL;
- *lastAlphabetically = NULL;
- if (headPointer != NULL)
- {
- *firstAlphabetically = headPointer;
- *lastAlphabetically = headPointer;
- while (headPointer->next != NULL)
- {
- headPointer = headPointer->next;
- if (strcmp(convertToUpper((*firstAlphabetically)->word), convertToUpper(headPointer->word)) > 0)
- {
- *firstAlphabetically = headPointer;
- }
- if (strcmp(convertToUpper((*lastAlphabetically)->word), convertToUpper(headPointer->word)) < 0)
- {
- *lastAlphabetically = headPointer;
- }
- }
- }
- }
- int main()
- {
- struct WordList *head, *firstAlphabetically, *lastAlphabetically;
- int numberOfWords, i;
- head = NULL;
- printf("Wprowadz liczbe wyrazow: ");
- scanf("%d", &numberOfWords);
- for (i = 0; i < numberOfWords; i++)
- {
- addWord(&head);
- }
- findWords(head, &firstAlphabetically, &lastAlphabetically);
- printf("%s\n%s", firstAlphabetically->word, lastAlphabetically->word);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement