mirror of
https://github.com/SuperBFG7/ympd
synced 2025-06-26 15:12:54 +00:00
Feat: implement list sorting
This commit is contained in:
parent
e371453b7b
commit
a81dd7e9d1
31
src/list.c
31
src/list.c
@ -2,6 +2,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
int list_init(struct list *l) {
|
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 list_get_value(const struct list *l, char *data) {
|
||||||
int value = 0;
|
int value = -1;
|
||||||
struct node *current = l->list;
|
struct node *current = l->list;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
if (strcmp(current->data, data) == 0) {
|
if (strcmp(current->data, data) == 0) {
|
||||||
@ -78,6 +79,34 @@ int list_shuffle(struct list *l) {
|
|||||||
return 0;
|
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 list_replace(struct list *l, int pos, char *data, int value) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -17,5 +17,6 @@ int list_replace(struct list *l, int pos, char *data, int value);
|
|||||||
int list_free(struct list *l);
|
int list_free(struct list *l);
|
||||||
int list_get_value(const struct list *l, char *data);
|
int list_get_value(const struct list *l, char *data);
|
||||||
int list_shuffle(struct list *l);
|
int list_shuffle(struct list *l);
|
||||||
|
int list_order(struct list *l, bool order);
|
||||||
int list_swap_item(struct node *n1, struct node *n2);
|
int list_swap_item(struct node *n1, struct node *n2);
|
||||||
struct node *list_node_at(const struct list * l, unsigned index);
|
struct node *list_node_at(const struct list * l, unsigned index);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user