Sto cercando di modificare il quicksort per fargli ordinare i numeri contenuti in una lista doppiamente collegata...qualcuno sa dirmi come modificare il seguente codice in tal senso?
Scusatemi se ho scritto qualche corbelleria.
Grazie
#include <stdio.h>
#include <stdlib.h>
struct node
{
int value;
struct node * prev_ptr;
struct node * next_ptr;
};
struct list
{
struct node * head;
struct node * tail;
};
void swap_elements( struct node **ptr1, struct node **ptr2 );
struct list *quicksort_r( struct list *ptr );
struct node *partition( struct list *ptr );
int main( )
{
struct node *ptr3, *ptrptr ;
struct list *ptr;
int i;
int n;
printf("\nSpecifica il numero di elementi : ");
scanf("%d",&n);
if(n==0)
ptr3=NULL;
else
{
ptr3=(struct node *)malloc(sizeof(struct node));
printf( "\nInserisci il 1o elemento : ");
scanf("%d",&ptr3->value);
ptrptr=ptr3;
ptr3->prev_ptr=NULL;
for(i=2;i<=n;i++)
{
ptrptr->next_ptr=(struct node *)malloc(sizeof(struct node));
ptrptr->next_ptr->prev_ptr=ptrptr;
ptrptr=ptrptr->next_ptr;
printf("\nInserisci il %do elemento : ",i);
scanf("%d", &ptrptr->value);
};
ptrptr->next_ptr=NULL;
};
ptr->head=ptr3;
ptr->tail=ptrptr;
quicksort_r( ptr );
printf( "\nLista ordinata : " );
while(ptr->head!=NULL){
printf( "%d ",ptr->head->value );
ptr->head=ptr->head->next_ptr;
}
printf("\n\n");
system( "PAUSE" );
return 0;
}
struct list *quicksort_r( struct list * ptr )
{
struct node *tmp_ptr , *x , *y ;
tmp_ptr = partition( ptr );
x=tmp_ptr->prev_ptr;
y=tmp_ptr->next_ptr;
{while (x->value<=tmp_ptr->value && x->prev_ptr != NULL){
x=x->prev_ptr;
ptr->head=x;};
ptr->tail=tmp_ptr->prev_ptr;
quicksort_r( ptr );}
{while (y->value>tmp_ptr->value && y->next_ptr != NULL){
y=y->next_ptr;
ptr->tail=y;};
ptr->head=tmp_ptr->next_ptr;
quicksort_r( ptr );}
return (ptr);
}
struct node *partition( struct list *ptr )
{
int pivot;
struct node **ptr1;
struct node **ptr2;
pivot = ptr->head->value;
*ptr1=ptr->head;
*ptr2=ptr->tail;
while ( *ptr1!=*ptr2 )
{
while ( (*ptr2)->value > pivot && *ptr1!=*ptr2 )
{
(*ptr2)=(*ptr2)->prev_ptr;
}
if ( *ptr1!=*ptr2 )
{
do
{
*ptr1=(*ptr1)->next_ptr;
}
while ( (*ptr1)->value <= pivot && *ptr1!=*ptr2);
swap_elements(ptr1 , ptr2);
}
}
*ptr1=ptr->head;
swap_elements(ptr1,ptr2);
return (*ptr1);
}
void swap_elements( struct node **ptr1, struct node **ptr2 )
{
int tmp;
tmp = (*ptr1)->value;
(*ptr1)->value = (*ptr2)->value;
(*ptr2)->value = tmp;
}