Desktop Programming > C/C++ & Visual C++

Send in your C++ or C student coding problem, with code inside code tags (#)

<< < (2/4) > >>

David:
Here is the first file, "CvecOfInt.h" ...
http://developers-heaven.net/forum/index.php/topic,2580.msg2863.html#msg2863

Note: CvecOfInt.h needs Cvec.h  ... see link below

David:
And the second of the two files that are used above, "Cvec.h" ...
http://developers-heaven.net/forum/index.php/topic,2580.msg2862.html#msg2862

David:
You may find that using a linked-list structure better suits some data handling ... so here is a solution to the above problem, to merge two lists into one list of unique elements, providing here, both unsorted and sorted list versions.  Note: Clist is a single link linked-list structure that emulates the C++ STL list container ... Clist tracks the head and tail, (front and end), size, if it isSorted ... and is expandable to as much memory that is available in your PC.  It also provides several ready to use functions like merge sort, insertion sort, insert in sorted order, find, erase, and unique - which is mainly what we want here.

Note: The following program needs the two files (below) named "ClistOfInt.h" and "Clist.h" in the same folder/directory ... before you compile it.


--- Code: ---/* mergeTwoClistOfInt.c */  /* 2016-10-09 */


/* http://developers-heaven.net/forum/index.php/topic,46.0.html */

/* includes <stdio.h>, <stdlib.h>, <string.h> & myAssert and then "Clist.h" */


#include "ClistOfInt.h"


int more()/* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered ... */
{
    int c, reply;
    fputs( "More (y/n) ? ", stdout ); fflush( stdout );
    reply = c = getchar();
    while( c != '\n' ) c = getchar(); /* 'flush' stdin buffer ...  */
    if( reply == 'n' || reply == 'N' ) return 0;
    /* else ... */
    return 1;
}

void enterClist( const char prompt[], Clist* cl )
{
    int i = 0, numGoodVals;
    Node myLst;
    puts( prompt ); fflush( stdout );
    do
    {
        printf( "enter integer %d: ", i+1 ); fflush( stdout );
        numGoodVals = scanf( "%d", &myLst.val );
        while( getchar() != '\n' ) ; /* flush stdin ... */
        if( numGoodVals != 1 )
        {
            printf( "Please enter only integers ...\n" );
            continue;
        }
        ++i;
        push_backClist( cl, &myLst );
    }while( more() );
}
/*
int exist( Clist* cv, int val )
{
    pNode p;
    for( p = cv->start; p != NULL; p = p->next )
    {
        if( p->val == val ) return 1;
    }
    return 0;
}
*/

void printClist( Clist* c )
{
    pNode cur = c->start;
    for( ; cur != NULL; cur = cur->next ) printf( "%d ", cur->val );
    printf( "size is %d\n", c->size );
}



int main()
{
    pNode p;
    int testVal;
    Clist c1, c2, c3;
    initClist( &c1 );
    initClist( &c2 );
    initClist( &c3 );
   
    enterClist( "Enter Clist1 ...", &c1 );
    enterClist( "\nEnter Clist2 ...", &c2 );
   
   
    /* using method ONE ... append to new list if not there already ... */
   
    for( p = c1.start; p != NULL; p = p->next )
    {
        /* if( !exist( &c3, p->val )) */
        if( !findClist( &c3, p->val ))
        {
            push_backClist( &c3, p );
        }
    }

    for( p = c2.start; p != NULL; p = p->next )
    {
        /* if( !exist( &c3, p->val )) */
        if( !findClist( &c3, p->val ))
        {
            push_backClist( &c3, p );
        }
    }

    printf( "\nc1 is: " ); printClist( &c1 );
    printf( "c2 is: " ); printClist( &c2 );
    printf( "c3 is: " ); printClist( &c3 );
   
    msortClist( &c3 );
    printf( "\nAfter msort( &c3 ) ...\n" );
    printf( "c3 is: " ); printClist( &c3 );
   
   
    /* using method TWO ... append list 2 to end of list 1, then call unique */

    c1.end->next = c2.start; /* link end of list 1 to start of list 2 ... */
    c1.end = c2.end; /* now ... update c1 end pointer */
    c1.size += c2.size; /* update size of new list 1 ... */
    c1.isSorted = 0; /* re-set isSorted flag to 0, i.e. NOT sorted ... */

    printf( "\nBefore unique, the appended list ...\n" );
    printf( "c1 is: " ); printClist( &c1 );
   
    testVal = c1.end->val; /* get a copy ... */
   
    printf( "\nTesting isortClist( &c1 ); ...\n" );
    isortClist( &c1 );
    printf( "c1 is: " ); printClist( &c1 );
   
    printf( "\nTesting find and erase %d ...\n", testVal );
    p = findClist( &c1, testVal );
    if( p ) eraseClist( &c1, p );
    printf( "c1 is: " ); printClist( &c1 );
   
   
    uniqueClist( &c1 ); /* unique will first call msort (if needed) ... */
    printf( "\nAfter unique ...\n" );
    printf( "c1 is: " ); printClist( &c1 );


    /* Note: list 2 was appended to list 1, so is cleared when list 1 cleared */
    clearClist( &c1 );
    clearClist( &c3 );
   
    fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
    getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
    return 0;
}
--- End code ---

David:
Here is the first file, "ClistOfInt.h" ...
http://developers-heaven.net/forum/index.php/topic,2582.msg2880.html#msg2880

Note: ClistOfInt.h loads in Clist.h

David:
And the second of the two files needed, "Clist.h" ...
http://developers-heaven.net/forum/index.php/topic,2582.msg2877.html#msg2877

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version