1
0
mirror of https://github.com/SuperBFG7/ympd synced 2025-03-04 02:28:19 +00:00

Feat: implement list sorting

This commit is contained in:
jcorporation 2018-10-16 22:36:53 +01:00
parent e371453b7b
commit a81dd7e9d1
2 changed files with 31 additions and 1 deletions

View File

@ -2,6 +2,7 @@
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdbool.h>
#include "list.h"
int list_init(struct list *l) {
@ -11,7 +12,7 @@ int list_init(struct list *l) {
}
int list_get_value(const struct list *l, char *data) {
int value = 0;
int value = -1;
struct node *current = l->list;
while (current != NULL) {
if (strcmp(current->data, data) == 0) {
@ -78,6 +79,34 @@ int list_shuffle(struct list *l) {
return 0;
}
int list_order(struct list *l, bool order) {
int swapped;
struct node *ptr1;
struct node *lptr = NULL;
if (l->list == NULL)
return 1;
do {
swapped = 0;
ptr1 = l->list;
while (ptr1->next != lptr) {
if (order == true && ptr1->value > ptr1->next->value) {
list_swap_item(ptr1, ptr1->next);
swapped = 1;
}
else if (order == false && ptr1->value < ptr1->next->value) {
list_swap_item(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
return 0;
}
int list_replace(struct list *l, int pos, char *data, int value) {
int i = 0;

View File

@ -17,5 +17,6 @@ int list_replace(struct list *l, int pos, char *data, int value);
int list_free(struct list *l);
int list_get_value(const struct list *l, char *data);
int list_shuffle(struct list *l);
int list_order(struct list *l, bool order);
int list_swap_item(struct node *n1, struct node *n2);
struct node *list_node_at(const struct list * l, unsigned index);