OK this program runs but I need some help. I need to sorted array elements into a linked list and the print the data using the linked list. But I think that Im doing this the hard way any ideas
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define unit32 unsigned int
#define Max 50 //number to set array
typedef int (*CMPFUN)(int, int);
void ArraySort(int Num[], CMPFUN fun_prt, unit32 ub)
{ //start bubble array
unit32 indx;
unit32 indx2;
int temp;
int temp2;
int flipped;
if (ub <= 1)
return;
indx =1;
do
{
flipped = 0;
for (indx2 = ub-1; indx2 >= indx; --indx2)
{
temp = Num[indx2];
temp2 = Num[indx2 -1];
if ((*fun_prt)(temp2,temp)>0)
{
Num[indx2 -1] = temp;
Num[indx2] = temp2;
flipped =1;
}
}
}while ((++indx < ub) && flipped);
}
int cmpfun (int a, int b)
{
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
}
main ()
{
char text [10];
int Num[Max]; // numbers to be added to array
int i, c, count;
count=0;
for ( i = 0; i < Max != 0;
i++ )
{
fputs ("Please enter a nunber: ", stdout);
if ( fgets(text, sizeof text, stdin) )
{
if ( *text == '\n' )
{
break;
}
if ( sscanf(text, "%d", &Num[i]) != 1 )
{
break;
}
}
}
count = i;
ArraySort(Num, cmpfun, count);
for ( i = 0; i < count; i ++ )
{
printf ("%d\t", Num[i]);
}
printf (" \n");
printf ("HIT ENTER TO EXIT\n");
getchar();
getchar();
return 0;
}
this is how i tryed to put it into a linked lost
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define unit32 unsigned int
#define Max 50 //number to set array
typedef struct Num
{
int Num[Max];
struct numbers* next;
} Num;
typedef int (*CMPFUN)(int, int);
void print_Num (const Num* prt);
Num* get_Num( void );
void ArraySort(int Num[], CMPFUN fun_prt, unit32 ub)
{ //start bubble array
unit32 indx;
unit32 indx2;
int temp;
int temp2;
int flipped;
if (ub <= 1)
return;
indx =1;
do
{
flipped = 0;
for (indx2 = ub-1; indx2 >= indx; --indx2)
{
temp = Num[indx2];
temp2 = Num[indx2 -1];
if ((*fun_prt)(temp2,temp)>0)
{
Num[indx2 -1] = temp;
Num[indx2] = temp2;
flipped =1;
}
}
}while ((++indx < ub) && flipped);
}
int cmpfun (int a, int b)
{
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
}
main ()
{
char text [10];
int Num[Max]; // numbers to be added to array
int i, c, count;
count=0;
for ( i = 0; i < Max != 0;
i++ )
{
fputs ("Please enter a nunber: ", stdout);
if ( fgets(text, sizeof text, stdin) )
{
if ( *text == '\n' )
{
break;
}
if ( sscanf(text, "%d", &Num[i]) != 1 )
{
break;
}
}
}
count = i;
ArraySort(Num, cmpfun, count);
Num*;
start = get_Num ();
print_Num;
printf (" \n");
printf ("HIT ENTER TO EXIT\n");
getchar();
getchar();
return 0;
}
Num* get_Num( void )
{
Num* *current, *first;
int responce;
current = first = malloc(sizeof ( Num ));
while (responce){
if ((current -> next)=
malloc (sizeof (Num))) == Null){
printf ( "OUut of memory\n Can't add more Nums\n");
return first;
}
current = current -> next;
}
current -> next = NULL;
return first;
}
void print_Num(const Num* ptr)
{
int count 1;
printf (\n\n\n\);
while ptr != NULL){
printf ("%d\t", Num);
ptr = ptr -> next;
}