Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.9 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

struct Node {
	void* value;
	struct Node* next;
};

struct SortedList {
	int(*sortingFunction)(void* a, void* b);
	Node* head;
};

Node* head;

bool Compare(void* a, void* b) {
	bool ret;
	(*(int*)a > *(int*)b) ? ret = true : ret = false;
	return ret;
}

void Insert(void* x) {
	Node* new_node = (Node*)malloc(sizeof(Node));
	new_node->next = NULL;
	new_node->value = x;
	Node* point = NULL;
	Node* tmp;
	if (head == NULL || Compare(&head->value, &new_node->value)) {
		new_node->next = head;
		head = new_node;
	}
	else {
		tmp = head;
		point = tmp->next;
		while (tmp->next != NULL && !Compare(&tmp->next->value, &new_node->value)) {
			tmp = tmp->next;
			point = tmp->next;
		}
		new_node->next = point;
		tmp->next = new_node;
	}
}

void PrintInt(void* n) {
	printf("%d ", *(int*)n);
}

void PrintList(Node* head, void (*fptr)(void*)) {
	Node* tmp = head;
	while (tmp != NULL) {
		(*fptr)(tmp->value);
		tmp = tmp->next;
	}
	printf("\n");
}

int main() {
	SortedList* sl = (struct SortedList*)malloc(sizeof(struct SortedList));
	head = (struct Node*)malloc(sizeof(struct Node));
	head = NULL;

	int num0, num1, num2;
	num0 = 30;
	num1 = 20;
	num2 = 10;

	void* ptr0 = &num0;
	void* ptr1 = &num1;
	void* ptr2 = &num2;

	Insert(ptr1);
	PrintList(head, PrintInt);
	Insert(ptr1);
	PrintList(head, PrintInt);
	Insert(ptr1);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);
	Insert(ptr0);
	PrintList(head, PrintInt);
	Insert(ptr0);
	PrintList(head, PrintInt);
	Insert(ptr0);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);
	Insert(ptr0);
	PrintList(head, PrintInt);
	Insert(ptr1);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);

}