### Merge and Sort.

How do you Merge a list of numbers in two files and input them in a third file witch has them ordered from smallest to largest.. I haven't learned about vectors in my class yet and this is chapter six in my c++ book (which I read) and no vectors included. Is there another way to do this without using a vector?
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````typedef struct node { int data; struct node *next; } Slink; void merge(Slink *A, Slink *B, Slink *C) { Slink *pa = NULL; Slink *qa = NULL; Slink *pb = NULL; Slink *qb = NULL; C = A; C->next = NULL; pa = A->next; pb = B->next; free(B); B = NULL; while (pa && pb) { if (pa->data data) { qa = pa; pa = pa->next; _insert(qa, C); } else { qb = pb; pb = pb->next; _insert(qb, C); } } while (pa) { qa = pa; pa = pa->next; _insert(qa, C); } while (pb) { qb = pb; pb = pb->next; _insert(qb, C); } } void _insert(Slink *qNode, Slink *C) { if(!(C->next) || C->next->data < qNode->data) { qNode->next = C->next; C->next = qNode; } else { free(qNode); qNode = NULL; } }``````
the C is ordered from largest to smallest. i did't notice your claim.
Assuming the two input files are sorted:

 ``123456789101112131415161718`` ``````Open input files. (A & B) Open output file. (C) read in number A (a) read in number B (b) while ( there are numbers in file A and file B ) { if ( a > b ) write b to C read number in from B else write a to C read number in from A } read in numbers from whichever file (A or B) which still has numbers in it. write them out to C.``````

That should be enough to get you started.
I figured it out thank you i used if-else statements and boolean expressions like "Cire" did above me ..
Topic archived. No new replies allowed.