// your code goes here ... //
/* mergeTwoArysStep1.c */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#include <stdio.h>
#define MAX_SIZE 5 /* keep this number small while still testing/debugging */
void printAry( const int ary[], int num )
{
int i;
for( i = 0; i < num; ++i )
printf( "%d ", ary[i] );
}
int main()
{
/* create some arrays to hold your test data and the merged unique data */
int ary1[MAX_SIZE], ary2[MAX_SIZE], ary3[2*MAX_SIZE];
int i;
for( i = 0; i < MAX_SIZE; ++i )
{
printf( "For ary1, enter integer %d: ", i+1 ); fflush( stdout );
scanf( "%d", &ary1[i] );
printf( "For ary2, enter integer %d: ", i+1 ); fflush( stdout );
scanf( "%d", &ary2[i] );
ary3[i] = ary1[i];
ary3[MAX_SIZE+i] = ary2[i];
}
while( getchar() != '\n' ) ; /* flush stdin ... */
printf( "\nary1 is: " );
printAry( ary1, MAX_SIZE );
printf( "\nary2 is: " );
printAry( ary2, MAX_SIZE );
printf( "\nary3 is: " );
printAry( ary3, 2*MAX_SIZE );
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
}
while( getchar() != '\n' ) ; /* flush stdin ... */
/* mergeTwoArys2.c */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#include <stdio.h>
#define MAX_SIZE 5 /* keep small while testing/debugging ... */
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;
}
/* returns the entered array ('by reference') and the size of the array entered,
size can be in range 0..max */
int enterAry( const char prompt[], int ary[], int max )
{
int i = 0, numGoodVals;
printf( "%s (max size %d)\n", prompt, max ); fflush( stdout );
do
{
printf( "enter integer %d: ", i+1 ); fflush( stdout );
numGoodVals = scanf( "%d", &ary[i] );
while( getchar() != '\n' ) ; /* flush stdin ... */
if( numGoodVals != 1 )
{
printf( "Please enter only integers ...\n" );
continue; /* jump to test at end of do..while loop right NOW! */
}
/* else ... */
++i;
if( i == max ) break;
}while( more() );
return i; /* the number of int's entered ... 'i' is in range 0..max */
}
void printAry( const int ary[], int num )
{
int i;
for( i = 0; i < num; ++i )
printf( "%d ", ary[i] );
}
int exist( const int ary[], int size, int val )
{
int i;
for( i = 0; i < size; ++i )
if( ary[i] == val ) return 1;
return 0;
}
int main()
{
int ary1[MAX_SIZE], ary2[MAX_SIZE], ary3[2*MAX_SIZE],
i, size1, size2, size3;
size1 = enterAry( "Enter ary1 ...", ary1, MAX_SIZE );
size2 = enterAry( "Enter ary2 ...", ary2, MAX_SIZE );
printf( "\nary1 is: " ); printAry( ary1, size1 );
printf( "\nary2 is: " ); printAry( ary2, size2 );
size3 = 0;
for( i = 0; i < size1; ++i )
{
if( !exist(ary3, size3, ary1[i]) )
{
ary3[size3] = ary1[i];
++ size3;
}
}
for( i = 0; i < size2; ++i )
{
if( !exist(ary3, size3, ary2[i]) )
{
ary3[size3] = ary2[i];
++ size3;
}
}
printf( "\nary3 is: " ); printAry( ary3, size3 );
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
}
/* mergeTwoArysStep2_a.c */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#include <stdio.h>
#define SIZE 5
void printAry( const int ary[], int num )
{
int i;
for( i = 0; i < num; ++i )
printf( "%d ", ary[i] );
}
void bubblesort( int ary[], int size )
{
int j, swap, tmp;
do
{
swap = 0; // if array is sorted ... then DONE after the next pass
// only have to check, each loop, up to to size-1 places
for( j = 1; j < size; ++j )
{
if( ary[j-1] > ary[j] ) // swap value at j-1 with value at j
{
tmp = ary[j];
ary[j] = ary[j-1];
ary[j-1] = tmp;
swap = 1;
}
}
--size;
}while( swap );
}
int getUnique( int ary[], int size )
{
int i = 0, j =1;
for( ; j < size ; ++j )
{
if( ary[i] != ary[j] ) if( ++i != j ) ary[i] = ary[j];
}
return i+1; // return updated size, now of unique elements
}
int main()
{
int i, unique, ary1[SIZE], ary2[SIZE], ary3[2*SIZE];
for( i = 0; i < SIZE; ++i )
{
printf( "For ary1, enter integer %d: ", i+1 ); fflush( stdout );
scanf( "%d", &ary1[i] );
printf( "For ary2, enter integer %d: ", i+1 ); fflush( stdout );
scanf( "%d", &ary2[i] );
ary3[i] = ary1[i];
ary3[SIZE+i] = ary2[i];
}
while( getchar() != '\n' ) ; /* flush stdin ... */
printf( "\nary1 is: " );
printAry( ary1, SIZE );
printf( "\nary2 is: " );
printAry( ary2, SIZE );
printf( "\nary3 is: " );
printAry( ary3, 2*SIZE );
bubblesort( ary3, 2*SIZE );
printf( "\nary3 sorted is: " );
printAry( ary3, 2*SIZE );
unique = getUnique( ary3, 2*SIZE );
printf( "\nunique ary3 size is %d and ary3 is: ", unique );
printAry( ary3, unique );
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
}
/* mergeTwoArysStep3.c */ /* using dynamic arrays, sort and unique functions */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#include <stdio.h>
#include <stdlib.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;
}
int getValidInt( const char prompt[] )
{
for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
{
int numGood, testInt;
fputs( prompt, stdout ); fflush( stdout );
numGood = fscanf( stdin, "%d", &testInt );
while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
if( numGood == 1 ) return testInt;
/* else ... */
puts( "Invalid input! Integers only please ..." );
}
}
/* get memory to hold an array of size int's .. and return pointer to memory */
int* getIntAryMemory( int size )
{
int* ary = malloc( sizeof(int)*size );
if( ary == NULL )
{
fprintf( stderr,
"malloc failed in 'getIntAryMemory' for size = %d\n", size );
fputs( "Press 'Enter' to continue/exit ... ", stderr );
getchar();
exit(1);
}
else return ary; // returns pointer to new array
}
/* Note:
any unused array memory in function below is NOT accessable after exiting
the function unless the 'max' value is returned, (NOTE: 'size' returned here)
*/
/* returns filled new array and the size of the new array entered ... */
int enterAry( const char prompt[], int** ary ) /* passing in pointer to pointer */
{
int i = 0;
int max = getValidInt( "Enter max size for this array of integers: " );
*ary = getIntAryMemory( max ); /* de-reference address to get to pointer */
do
{
printf( "Enter integer %d: ", i+1 );
(*ary)[i++] = getValidInt( "" );
if( i == max )
{
printf( "You have reached the maximum size of %d elements "
"for this array.\n", max );
break;
}
}while( more() );
return i; /* i hold the number of int's entered ... i is in range 0..max */
}
void printAry( const int ary[], int num )
{
int i;
for( i = 0; i < num; ++i )
printf( "%d ", ary[i] );
}
void bubblesort( int ary[], int size )
{
int j, swap, tmp;
do
{
swap = 0; /* if array is sorted ... then DONE after the next pass */
/* only have to check, each loop, up to to size-1 places */
for( j = 1; j < size; ++j )
{
if( ary[j-1] > ary[j] ) /* swap value at j-1 with value at j */
{
tmp = ary[j];
ary[j] = ary[j-1];
ary[j-1] = tmp;
swap = 1;
}
}
--size;
}while( swap );
}
int getUnique( int ary[], int size )
{
int i = 0, j = 1;
for( ; j < size ; ++j )
{
if( ary[i] != ary[j] ) if( ++i != j ) ary[i] = ary[j];
}
return i+1; // return updated size, now of unique elements
}
int main()
{
/* Note: below ... ary1, ary2, ary3 are pointers to int,
i.e. variables to hold the address of an int,
or the address to the first int in a block (an array) of int's */
int *ary1, *ary2, *ary3, i, size1, size2, size3, uniqueSize;
size1 = enterAry( "Enter ary1 ...", &ary1 ); /* passing in a 'reference' */
putchar( '\n' );
size2 = enterAry( "Enter ary2 ...", &ary2 ); /* i.e. pass in pointer address */
printf( "\nary1 is: " ); printAry( ary1, size1 );
printf( "\nary2 is: " ); printAry( ary2, size2 );
size3 = size1 + size2;
ary3 = getIntAryMemory( size3 ); /* get memory to hold array 3 ... */
/* copy ary1 into first part of ary3 */
for( i = 0; i < size1; ++i )
ary3[i] = ary1[i];
/* copy ary2 into last part of ary3 */
for( i = 0; i < size2; ++i )
ary3[size1+i] = ary2[i];
printf( "\nary3 is: " );
printAry( ary3, size3 );
bubblesort( ary3, size3 );
printf( "\nary3 sorted is: " );
printAry( ary3, size3 );
uniqueSize = getUnique( ary3, size3 );
printf( "\nunique ary3 size is %d and unique ary3 is ", uniqueSize );
printAry( ary3, uniqueSize );
/* free all dynamic memory when done with it ...*/
free( ary3 );
free( ary2 );
free( ary1 );
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
}
/* mergeTwoCvecOfInt.c */ /* 2016-10-09 */
/* using CvecOfInt.h that emulates some of the C++ STL vector */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#define myType double /* not needed here if using default #define myType int */
#include "CvecOfInt.h" /* changed to CvecOfDouble by above pre-define of myType */
/*
"CvecOfInt.h" also includes <stdio.h>, <stdlib.h>, <string.h> and myAssert(..)
and also includes the file "Cvec.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 enterCvec( const char prompt[], Cvec* cv )
{
int i = 0, numGoodVals;
Rec r;
puts( prompt ); fflush( stdout );
do
{
printf( "enter number %d: ", i+1 ); fflush( stdout );
numGoodVals = scanf( "%lf", &r.val );
while( getchar() != '\n' ) ; /* flush stdin ... */
if( numGoodVals != 1 )
{
printf( "Please enter only integers ...\n" );
continue;
}
++i;
push_backCvec( cv, &r );
}while( more() );
}
void printCvec( Cvec* cv )
{
int i;
for( i = 0; i < cv->size; ++i )
printf( "%.2f ", cv->ary[i].val );
printf( "cap = %d, size = %d\n", cv->cap, cv->size );
}
/*
int exist( Cvec* cv, myType val )
{
int i;
for( i = 0; i < cv->size; ++i )
{
if( cv->ary[i].val == val ) return 1;
}
return 0;
}
*/
int main()
{
int i;
myType testVal;
Cvec cv1, cv2, cv3;
initCvec( &cv1 ); /* NOTE: MUST initial before using for Cvec to work ... */
initCvec( &cv2 );
initCvec( &cv3 );
enterCvec( "Enter Cvec1 ...", &cv1 ); /* Note: passing a 'reference' ... */
enterCvec( "\nEnter Cvec2 ...", &cv2 );
/* method ONE ... adding ONLY elements, NOT already present, to new cv3 */
for( i = 0; i < cv1.size; ++i )
{
/* if( !exist( &cv3, cv1.ary[i].val) ) */
if( findCvec( &cv3, cv1.ary[i].val) == -1 )
{
push_backCvec( &cv3, &cv1.ary[i] );
}
}
for( i = 0; i < cv2.size; ++i )
{
/* if( !exist( &cv3, cv2.ary[i].val) ) */
if( findCvec( &cv3, cv2.ary[i].val ) == -1 )
{
push_backCvec( &cv3, &cv2.ary[i] );
}
}
printf( "\ncv1 is: " ); printCvec( &cv1 );
printf( "cv2 is: " ); printCvec( &cv2 );
printf( "cv3 is: " ); printCvec( &cv3 );
msortCvec( &cv3 );
printf( "\nAfter msort( &cv3 ) ...\n" );
printf( "cv3 is: " );printCvec( &cv3 );
/* method TWO ... append cv2 to cv1, then sort cv1 and get unique cv1 */
puts( "\nTesting uniqueCvec ... but first, cv2 appended to cv1:" );
/*
for( i = 0; i < cv2.size; ++i )
{
push_backCvec( &cv1, &cv2.ary[i] );
printf( "cv1.cap = %d, sv1.size = %d\n", cv1.cap, cv1.size );
}
*/
reserveCvec( &cv1, cv1.size + cv2.size ); /* get just enough memory ... */
for( i = 0; i < cv2.size; ++i )
{
cv1.ary[cv1.size ++] = cv2.ary[i]; /* increment size AFTER assignment */
printf( "cv1.cap = %d, cv1.size = %d\n", cv1.cap, cv1.size );
}
puts( "After appending cv2 to cv1 ..." );
printf( "cv1 is: " ); printCvec( &cv1 );
testVal = cv1.ary[cv1.size-1].val; /* get copy of last val for future use */
puts( "Testing supplied isortCvec( &cv1 ) ...\n"
"(NOT really needed here because uniqueCvec will call msort if !isSorted)" );
isortCvec( &cv1 );
printf( "After isortCvec( &cv1 ) ...\n" );
printf( "cv1 is: " ); printCvec( &cv1 );
printf( "\nTesting find %.2f and erase ...\n", testVal );
i = findCvec( &cv1, testVal );
if( i != -1 ) eraseCvec( &cv1, i );
printf( "cv1 is: " ); printCvec( &cv1 );
uniqueCvec( &cv1 );
puts( "After unique ... " );
printf( "cv1 is: " ); printCvec( &cv1 );
clearCvec( &cv3 );
clearCvec( &cv2 );
clearCvec( &cv1 );
fputs( "\nPress 'Enter' to continue/exit ... ", stdout); fflush( stdout );
getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */
return 0;
}
/* 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;
}
/* mergeTwoCvecOfString.c */ /* 2016-10-09 */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#include "CvecOfString.h" /* also includes "readLine.h" that includes ...
stdio.h, stdlib.h, string.h,
myAssert, newCopy
also includes "Cvec.h"
*/
void enterCvec( const char prompt[], Cvec* cv )
{
int i = 0;
Rec r;
puts( prompt ); fflush( stdout );
do
{
printf( "enter string %d: ", i+1 ); fflush( stdout );
r.str = readLine( stdin ); /* readLine gets new dynamic memory C string */
++i;
push_backCvec( cv, &r ); /* copies C string pointer into ary of pointers */
}while( more() );
}
/*
void printCvec( Cvec* cv )
{
int i;
for( i = 0; i < cv->size; ++i )
printf( "%s ", cv->ary[i].str );
}
int exist( Cvec* cv, char* s )
{
int i;
for( i = 0; i < cv->size; ++i )
{
if( strcmp(cv->ary[i].str, s) == 0 ) return 1;
}
return 0;
}
*/
int main()
{
int i;
char* testStr;
Cvec cv1, cv2, cv3;
initCvec( &cv1 ); /* NOTE! MUST initial Cvec for it to work ... */
initCvec( &cv2 );
initCvec( &cv3 );
enterCvec( "Enter Cvec1 ...", &cv1 ); /* passing in a 'reference' to cv1 */
enterCvec( "\nEnter Cvec2 ...", &cv2 ); /* i.e. passing in address ... */
/* method ONE ... append only unique elements to cv3 ... */
for( i = 0; i < cv1.size; ++i ) /* if element 'i' not in cv3, add it */
{
/* if( !exist( &cv3, cv1.ary[i].str) ) */
if( findCvec( &cv3, cv1.ary[i].str ) == -1 )
{
/* NOTE!!! cv3 holds ONLY copies of pointers to C strings ...
i.e. holds only a 'shallow copy' of the C strings */
push_backCvec( &cv3, &cv1.ary[i] );
}
}
for( i = 0; i < cv2.size; ++i )
{
/* if( !exist( &cv3, cv2.ary[i].str) ) */
if( findCvec( &cv3, cv2.ary[i].str ) == -1 )
{ /* ONLY 'shallow copy' made of C string made below ... */
push_backCvec( &cv3, &cv2.ary[i] );
}
}
printf( "\ncv1 is: " );
showCvec( &cv1 ); printf( " cv1.size = %d\n", cv1.size );
printf( "cv2 is: " );
showCvec( &cv2 ); printf( " cv2.size = %d\n", cv2.size );
printf( "cv3 is: " );
showCvec( &cv3 ); printf( " cv3.size = %d\n", cv3.size );
msortCvec( &cv3 );
printf( "After msort( &cv3 ) ... " );
showCvec( &cv3 ); printf( " cv3.size = %d\n", cv3.size );
/* method TWO ... get cv1 and cv2 into a large enough cv3, then call unique */
reserveCvec( &cv3, cv1.size+cv2.size );
for( i = 0; i < cv1.size; ++i )
cv3.ary[i] = cv1.ary[i];
for( i = 0; i < cv2.size; ++i )
cv3.ary[cv1.size + i] = cv2.ary[i];
cv3.size = cv1.size+cv2.size;
cv3.isSorted = 0;
puts( "\nAfter copying pointers in cv1 and cv2 to cv3 ..." );
printf( "cv3 is: " );
showCvec( &cv3 ); printf( " cv3.size = %d\n", cv3.size );
testStr = newCopy( cv3.ary[cv3.size-1].str ); /* get new copy for future use */
puts( "\nTesting supplied isortCvec( &cv3 ) ...\n"
"(NOT really needed here because uniqueCvec will call msort if !isSorted)" );
isortCvec( &cv3 );
puts( "After isortCvec( &cv3 ) ..." );
printf( "cv3 is: " ); showCvec( &cv3 );
printf( "cv3.cap = %d, cv3.size = %d\n", cv3.cap, cv3.size );
i = findCvec( &cv3, testStr );
if( i != -1 ) eraseCvec( &cv3, i );
printf( "\nTesting find %s and erase ...\n", testStr );
printf( "cv3 is: " ); showCvec( &cv3 );
printf( "cv3.cap = %d, cv3.size = %d\n", cv3.cap, cv3.size );
uniqueCvec( &cv3 ); /* also frees dynamic memory for redundant C strings */
puts( "\nAfter uniqueCvec( &cv3 )... " );
printf( "cv3 is: " ); showCvec( &cv3 );
printf( " cv3.size = %d\n", cv3.size );
clearCvec( &cv3 ); /* when done, rest of dynamic C string memory is free */
/* now ... just free ary memory for cv1 and cv2 ... */
free( cv2.ary ); /* initCvec( &cv2 ); */
free( cv1.ary ); /* initCvec( &cv1 ); // if need to reuse in program // */
free( testStr ); /* and free testStr ... */
takeInChar( "\nPress 'Enter' to continue/exit ... " );
return 0;
}
/* mergeTwoClistOfString.c */ /* 2016-10-09 */
/* http://developers-heaven.net/forum/index.php/topic,46.0.html */
#define DELIMITS ", "
#include "split.h" /* which includes "ClistOfString.h" that includes ...
"readLine.h" which also includes ...
stdio.h, stdlib.h, string.h and ...
functions myAssert(..) and newCopy(..)
Note: "ClistOfString.h" also includes "Clist.h"
*/
void enterClist( const char prompt[], Clist* cl )
{
int i = 0;
Node node;
puts( prompt ); fflush( stdout );
do
{
printf( "enter string %d: ", i+1 ); fflush( stdout );
node.str = readLine( stdin ); /* readLine gets a new dynamic C string */
++i;
push_backClist( cl, &node ); /* copies new C string pointer into pointer ary */
}while( more() );
}
/*
void printClist( Clist* cl )
{
pNode cur = cl->start;
for( ; cur != NULL; cur = cur->next )
printf( "%s ", cur->str );
}
int exist( Clist* cl, char* s )
{
pNode cur = cl->start;
for( ; cur != NULL; cur = cur->next )
{
if( strcmp(cur->str, s) == 0 ) return 1;
}
return 0;
}
*/
int main()
{
char* testStr;
pNode p, tmp;
Clist c1, c2, c3;
initClist( &c1 ); /* NOTE! MUST initial Clist for it to work ... */
initClist( &c2 );
initClist( &c3 );
enterClist( "Enter Clist1 ...", &c1 ); /* passing in a 'reference' to cv1 */
enterClist( "\nEnter Clist2 ...", &c2 ); /* i.e. passing in address ... */
/* method ONE: append to new list if NOT present there already ... */
for( p = c1.start; p != NULL; p = p->next )
{
/* if( !exist( &c3, p->str )) */
if( !findClist( &c3, p->str )) /* if element not in c3, add it */
{
/* NOTE!!! cv3 holds ONLY copies of pointers to C strings ...
i.e. holds only a 'shallow copy' of the C strings */
push_backClist( &c3, p );
}
}
for( p = c2.start; p != NULL; p = p->next )
{
/* if( !exist( &c3, p->str ) ) */
if( !findClist( &c3, p->str ) )
{ /* ONLY 'shallow copy' made of C string made below ... */
push_backClist( &c3, p );
}
}
printf( "\nc1 is: " );
showClist( &c1 ); printf( " c1.size = %d\n", c1.size );
printf( "c2 is: " );
showClist( &c2 ); printf( " c2.size = %d\n", c2.size );
printf( "c3 is: " );
showClist( &c3 ); printf( " c3.size = %d\n", c3.size );
msortClist( &c3 );
printf( "After msort( &c3 ) ...\nc3 is: " );
showClist( &c3 ); printf( " c3.size = %d\n", c3.size );
/* Method TWO ... append list 2 to end of list 1 and then call unique */
c1.end->next = c2.start;
c1.end = c2.end;
c1.size = c1.size + c2.size;
c1.isSorted = 0;
printf( "\nAfter appending c2 to the end of c1:\nc1 is: " );
showClist( &c1 ); printf( " c1.size = %d\n", c1.size );
testStr = newCopy( c1.end->str );
isortClist( &c1 );
printf( "\nAfter isortClist:\nc1 is: " );
showClist( &c1 ); printf( " c1.size = %d\n", c1.size );
printf( "\nAfter calling find %s and erase:\nc1 is: ", testStr );
p = findClist( &c1, testStr );
if( p ) eraseClist( &c1, p ); /* clears dynamic C string AND list memory */
showClist( &c1 ); printf( "c1.size = %d\n", c1.size );
uniqueClist( &c1 ); /* clears redundant dynamic C string AND list memory */
printf( "\nAfter calling unique, new list:\nc1 is: " );
showClist( &c1 ); printf( " c1.size = %d\n", c1.size );
printf( "\nAfter calling joinClist( &c1, \", \" ),\n" );
testStr = joinClist( &c1, ", " ); /* testStr is in NEW dynamic memory */
printf( "The joined testStr is: \n%s\n", testStr );
clearClist( &c1 ); /* also frees all dynamic memory that was in list 2 */
/* NOW ... JUST free here the list memory for c3 ... */
for( p = c3.start; p != NULL; p = tmp )
{
tmp = p->next;
free( p );
}
/* Note: if need to use c3 again, then need to first call initClist( &c3 ) */
printf( "\nAfter calling split( &c1, testStr ),\nc1 is: " );
split( &c1, testStr ); /* makes a list of NEW dynamnic strings */
showClist( &c1 ); printf( " c1.size = %d", c1.size );
clearClist( &c1 );
free( testStr );
takeInChar( "\nPress 'Enter' to continue/exit ... " );
return 0;
}
Update:
you can contact me via this link ...
https://sites.google.com/site/andeveryeyeshallseehim/
(my/editor gmail link is on the 2nd page)
Ok students beginning C or C++ ... this page is for you to get help with your beginning computer programming problems in C or C++.
Please include your code inside code tages ...
After you click on the Reply icon below, on the new page presented to you to write about your problem and submit your code ... click on the above # icon toCode: [Select]// your code goes here ... //
insert your code between these two start and stop code tags ...[ code ] // your code goes here ... // [ /code ] Only don't add spaces inside the tags as I did here, so that you could SEE something of what code tags look like. The code tages will preserve the indentation of your C/C++ code.
Please include a statement of the problem given and the problem(s) you are having ... and allow some time for a reply, as this help is being offered on a trial experimental gratuitous basis.
BEFORE you click on Post/Save at the bottom of your page, please click on Preview ...
and make sure you correct any typos ...
and that the problem is stated clearly ...
using good grammar and helpful punctuation.
Shalom shalom,
David