Мне нужно отсортировать список либо в порядке возрастания, либо в порядке убывания в соответствии с порядком предпочтения, вычисленным любой функцией, передаваемой в качестве второго аргумента.
В значительной степени алгоритм находит минимальное значение, заменяет его на значение в первой или последней позиции в зависимости от вычисления функции, переданной в качестве второго аргумента вызова функции sort list ().
Вот мой код, я просто не знаю, как реализовать функцию, передающую его по возрастанию или по убыванию. Мой только идет в одну сторону:
#include <stdio.h>
#include <stdlib.h>
typedef struct iorb {
int base_pri;
struct iorb *link;
char filler[100];
} IORB;
int push(struct iorb **h, int x)
{
struct iorb *temp = (struct iorb*)malloc(sizeof(struct iorb));
temp->base_pri = x;
temp->link = *h;
*h = temp;
return 0;
}
void print(struct iorb *head)
{
struct iorb *temp = head;
while(temp != NULL)
{
printf("%d ",temp->base_pri);
temp = temp->link;
}
printf("\n");
}
void sort(struct iorb **h)
{
int a;
struct iorb *temp1;
struct iorb *temp2;
for(temp1=*h;temp1!=NULL;temp1=temp1->link)
{
for(temp2=temp1->link;temp2!=NULL;temp2=temp2->link)
{
if(temp2->base_pri < temp1->base_pri)
{
a = temp1->base_pri;
temp1->base_pri = temp2->base_pri;
temp2->base_pri = a;
}
}
}
}
int main()
{
struct iorb * head = NULL;
push(&head,5);
push(&head,4);
push(&head,6);
push(&head,2);
push(&head,9);
printf("List is : ");
print(head);
sort(&head);
printf("after sorting list is : ");
print(head);
return 0;
}