Untitled

 avatar
unknown
plain_text
3 years ago
1.3 kB
2
Indexable
#include<stdio.h>
#include<stdlib.h>
#include <iostream>

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);
}

void Insert(void* x) {
	struct Node* newNode;
	newNode = (struct Node*)malloc(sizeof(struct Node));
	newNode->data = x;
	newNode->next = header;
	header = newNode;
}

void printList(struct Node* node, void (*fptr)(void*)) {
	while (node->next != 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));
	header->next = NULL;
	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);
}