Created
August 31, 2019 20:37
-
-
Save DrMetallius/ed5cc28cc3e9b532be9628ac8aac7004 to your computer and use it in GitHub Desktop.
Linked list sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
struct Entry { | |
char name[20]; | |
unsigned int price; | |
Entry *nextEntry; | |
}; | |
Entry *readAlphabeticList(); | |
Entry *sortByPrice(Entry *list); | |
void printList(Entry *list); | |
void insertIntoList(Entry **list, Entry *newEntry, bool (*comparisonFn)(Entry *, Entry *)); | |
void removeFromList(Entry **list, char *name, unsigned int price); | |
bool compareAlphabetically(Entry *entry, Entry *otherEntry); | |
bool compareByPrice(Entry *entry, Entry *otherEntry); | |
Entry *readEntry(); | |
int main() { | |
Entry *list = readAlphabeticList(); | |
std::cout << "Alphabetically ordered list" << std::endl; | |
printList(list); | |
auto priceSortedList = sortByPrice(list); | |
std::cout << "Price ordered list:" << std::endl; | |
printList(priceSortedList); | |
std::cout << "Enter the name of toy which you want to delete and its price or \"end\":" << std::endl; | |
auto deletedEntry = readEntry(); | |
if (deletedEntry) { | |
removeFromList(&priceSortedList, deletedEntry->name, deletedEntry->price); | |
} | |
printList(priceSortedList); | |
std::cout << "Enter the name of toy which you want to add and its price or \"end\":" << std::endl; | |
auto addedEntry = readEntry(); | |
if (addedEntry) { | |
insertIntoList(&priceSortedList, addedEntry, compareByPrice); | |
} | |
printList(priceSortedList); | |
return 0; | |
} | |
Entry *readEntry() { | |
char input[20]; | |
std::cin >> input; | |
if (std::strcmp(input, "end") == 0) return NULL; | |
auto entry = new Entry(); | |
std::strncpy(entry->name, input, sizeof(entry->name)); | |
std::cin >> entry->price; | |
return entry; | |
} | |
void printList(Entry *list) { | |
auto currEntry = list; | |
while (currEntry) { | |
std::cout << currEntry->name << ": " << currEntry->price << std::endl; | |
currEntry = currEntry->nextEntry; | |
} | |
} | |
Entry *readAlphabeticList() { | |
Entry *list = NULL; | |
std::cout << "Enter the name of toy and its price or \"end\"" << std::endl; | |
while (true) { | |
auto newEntry = readEntry(); | |
if (!newEntry) break; | |
insertIntoList(&list, newEntry, compareAlphabetically); | |
} | |
return list; | |
} | |
void insertIntoList(Entry **list, Entry *newEntry, bool (*comparisonFn)(Entry *, Entry *)) { | |
Entry *prevEntry = NULL; | |
Entry *currEntry = *list; | |
while (currEntry != NULL && comparisonFn(newEntry, currEntry)) { | |
prevEntry = currEntry; | |
currEntry = currEntry->nextEntry; | |
} | |
if (prevEntry != NULL) { | |
prevEntry->nextEntry = newEntry; | |
} else { | |
*list = newEntry; | |
} | |
newEntry->nextEntry = currEntry; | |
} | |
bool compareAlphabetically(Entry *entry, Entry *otherEntry) { | |
return std::strcmp(entry->name, otherEntry->name) > 0; | |
} | |
bool compareByPrice(Entry *entry, Entry *otherEntry) { | |
return entry->price > otherEntry->price; | |
} | |
Entry *sortByPrice(Entry *list) { | |
Entry *newList = NULL; | |
auto entry = list; | |
while (entry) { | |
auto newEntry = new Entry; | |
*newEntry = *entry; | |
insertIntoList(&newList, newEntry, compareByPrice); | |
entry = entry->nextEntry; | |
} | |
return newList; | |
} | |
void removeFromList(Entry **list, char *name, unsigned int price) { | |
Entry *prevEntry = NULL; | |
auto entry = *list; | |
while (entry) { | |
if (std::strcmp(entry->name, name) == 0 && entry->price == price) { | |
if (prevEntry) { | |
prevEntry->nextEntry = entry->nextEntry; | |
} else { | |
*list = entry->nextEntry; | |
} | |
delete entry; | |
break; | |
} | |
prevEntry = entry; | |
entry = entry->nextEntry; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment