Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,130 people online right now. Registration is fast and FREE... Join Now!
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
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; }
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; } }
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; }
The print method is fine, there is not an easier way to go about is as you must traverse the elements. as far as sorting, bubble does take a while, try puting in a catch if you traverse all elements without trading then the sorting is done.
The print method is fine, there is not an easier way to go about is as you must traverse the elements. as far as sorting, bubble does take a while, try puting in a catch if you traverse all elements without trading then the sorting is done.
sirry. but you are talking a little about my head So are you saying to add my linked list in while i doing the sort. how do i do that
A function should be written for a specific purpose only so that it can be reused at later stages and in programs. So, No...Let the sort array be used to sort the array ONLY.
Instead call the sort function in the add()/delete() functions of the Linked List.
A function should be written for a specific purpose only so that it can be reused at later stages and in programs. So, No...Let the sort array be used to sort the array ONLY.
Instead call the sort function in the add()/delete() functions of the Linked List.
with the second code that i posted How would i do that. Or would I have to rewrite. I know that me code to sort works. So why can't i just add teh funcation to but it into a linked list after it has been sorted. That is what i was trying to dowith the second code that i wrote. Or show me where I'm going wrong. please.
int main(void) { int x;char text [10]; int Num[Max]; // numbers to be added to array int i, c, count; struct listNode *start = NULL; struct listNode *last = NULL; struct listNode *data = NULL; struct listNode *tempnode = NULL; srand( (unsigned)time( NULL ) ); 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< 5;i++) { data = (struct listNode *) malloc(sizeof(listNode)); if(!data) return -1; data->data= rand(); insert(data, &start, &last); } tempnode = start; printf("From beginning to end\n"); while(tempnode) // print out the list from beginning to end { printf("data = %d\n",tempnode->data); tempnode = tempnode->nextPtr; } tempnode = last; printf("\n\nFrom end to beginning\n"); while(tempnode) // print out the list from end to beginning { printf("data = %d\n",tempnode->data); tempnode = tempnode->priorPtr; } return 0; }
This post has been edited by mitchelltab: 27 Sep, 2006 - 11:02 AM