Untitled

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

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

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

struct sortedList* SL;
struct Node* header;

int compareInts(void* a, void* b) {
	if (*(int*)a > * (int*)b)
		return 1;

	else if (*(int*)a == *(int*)b) 
		return 0;

	else 
		return -1;
}

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

struct Node* GetNewNode(void* x) {
	struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = x;
	newNode->next = NULL;
	return newNode;
}

void Insert(void* x) {
	struct Node* newNode = GetNewNode(x);
	if (header->next == NULL) {
		header->next = newNode;
		newNode->next = NULL;
		return;
	}
	newNode->next = header;
	header = newNode;
}

void printList(struct Node* node, void (*fptr)(void*)) {
	while (node != NULL) {
		(*fptr)(node->data);
		node = node->next;
	}
}

void Print(struct Node* node) {
	while (node != NULL) {
		node = node->next;
	}
}

int main() {
	SL = (struct sortedList*)malloc(sizeof(struct sortedList));
	header = (struct Node*)malloc(sizeof(struct Node));
	SL->sortingFunction = compareInts;
	SL->head = header;

	int number1, number2;
	number1 = 10;
	number2 = 20;

	void* ptr1 = &number1;
	void* ptr2 = &number2;

	Insert(ptr1);
	Insert(ptr2);
	printList(header, printInt);
}