Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.7 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;
	if (head == NULL) {
		head = new_node;
	}
	else {
		Node* tmp = head;
		while (tmp->next != NULL) {
			if (!Compare(&x, &tmp->next->value)) {
				point = tmp->next;
				break;
			}
			tmp = tmp->next;
		}
		tmp->next = new_node;
		tmp->next->next = point;
	}
}

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(ptr2);
	PrintList(head, PrintInt);
	Insert(ptr0);
	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(ptr0);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);
	Insert(ptr2);
	PrintList(head, PrintInt);

}