/* Exercise 4.1.5 Refine Exercise 4.1.4 and write the inverse algorithm to the one in Exercise 4.1.2 that the receiver has to perform to get the initial binary data representation. After the bit-stuffing and bit-unstuffing compare your list with the original one using the equality operator==. If the lists are equal, you did a fine job. Note: It is advisable to include a plausibility test in your unstuff algorithm. After a sequence of five consecutive ones there must be a zero, otherwise something went wrong in the stuffing algorithm. */ #include using namespace std; void main() { list l; int input=0; list::iterator begin = l.begin(); list::iterator end = l.end(); cout<<"\nEnter a series of 0's and 1's. Terminate with any other integer\n"; while(input==1 || input==0) { cin>>input; if(input==1 || input==0) l.push_back(input); } cout<<"Initial values are: "; // print values in list begin = l.begin(); end = l.end(); while(begin!=end) { cout<<*begin; begin++; } int unstuffed_sz= l.size(); // bit stuffing algorithm begin = l.begin(); end = l.end(); int count_1 =0; while(end!=begin) { if( *begin == 1 ) count_1++; if( *begin == 0 ) count_1 =0; if( count_1 ==6 ) { l.insert(begin,0); count_1 =1; } begin++; } // print bit stuffed list cout<<"\nStuffed list is\n"; begin = l.begin(); end = l.end(); while(begin!=end) { cout<<*begin; begin++; } float absEx =(l.size() - unstuffed_sz); cout<<"\nAbsolute expansion is :"< inv; begin = l.begin(); end = l.end(); count_1=0; while(begin!=end) { if ( count_1 < 5 ) { if ( *begin == 1 ) count_1++; inv.push_back ( *begin ); begin++; } if ( count_1 == 5 ) { count_1 =0; if ( *(begin++) ) // plausibility test see Note: above { cerr<<"\nError, bits not stuffed correctly"; exit(0); } } } // print inverted list cout<<"\ninverted list is\n"; begin = inv.begin(); end = inv.end(); while(begin!=end) { cout<<*begin; begin++; } inv.size() == unstuffed_sz ? cout<<"\ncorrect size" : cout<<"\nincorrect size"; cin.ignore(); cout<<"\nPress any key to exit"; while(!cin.get()) ; }